java - Mybatis插入自定义对象

标签 java mybatis

在我发现的 mybatis 的几乎所有示例中,人们都指定了每一列 插入。我不想指定表中的每一列。有没有 无论如何,当我使用用户对象调用 addProfile() 时。所有属性 映射并直接插入到给定表的所有列中? 我想要这样...

@Insert("insert into user values #{user}")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
public int addProfile(User user);

最佳答案

您可以将 User 对象作为参数传递到 addProfile() 方法中。但是您在 @Insert 注释中指定的字符串是一个普通的 SQL 语句,它不知道您传递到该语句中的用户对象。

这个answer明确指出,

MyBatis has auto mapping when reading data from database but doesn't have option to automatically map fields on insertion.

It is possible to have implicit fields in SQL select statement (select * from table) so there is automatic mapping to POJO in this case but it is not possible to have implicit fields in update or insert hence no auto-mapping.

关于java - Mybatis插入自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33395762/

相关文章:

mybatis - 使用 my-batis 将返回类型转换为 bool 值?

java - 即使场景中 VBox 的宽度大于 0,它的宽度仍然为 0

java - 如何保证删除或更新成功而不需要额外请求?

java - 需要澄清java垃圾收集复制收集器

java - Google Play 服务查找我的位置很慢

java - MyBatis 是如何处理空结果集的?

java - MyBatis 有序集

Mybatis 生成器 : How to generate all tables for a specified schema

java - 用于 Java 桌面应用程序的最佳数据库是什么

java - renameTo() 不起作用