简体   繁体   中英

Mybatis insert custom object

In almost all example i found for mybatis, people specify every column for insertion.I dont want to specify each and every column in the table. Is there any way that, when i call addProfile() with user object. All the properties mapped and inserted directly into all the columns of the given table ? I want it this way...

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

You can pass the User object as a parameter in your addProfile() method. But the String you specify inside the @Insert annotation is a plain SQL statement it does not know the user object you pass into the statement.

This answer points it clearly,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM