在我发现的 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/