mybatis + mysql insert the return data is added after the primary key data --- id (rpm)

1. The return value acquisition useGeneratedKeys, database does not support portion

Modify mybatis xml

 

<insert id= "insertUser"  useGeneratedKeys="true" keyProperty="id"  parameterType= "com.entity.user" >
     insert into test (name) values (#{name})
  </insert>

  

= useGeneratedKeys "to true"  : setting whether to use the JDBC getGenereatedKeys method of acquiring and assigned to the primary key field keyProperty model attribute settings. (Suitable for mysql, sqlserver databases, oracle can not be used, use selectkey child nodes do)

keyProperty: attribute name assignment object .

After complete addition, according to the object attribute values ​​directly.

user u=new user();

u.setName("测试");

System.out.println(u.getId()+"取值前");

int num = this.dao.getSqlSession().insert("insertUser",u);

System.out.println(u.getId()+"取值后");

 

2. Get in accordance selectkey

 

<insert id= "insertUser"   parameterType= "com.entity.user" >
      insert into test (name) values (#{name})
       <selectKey keyProperty= "id"  resultType= "java.lang.Integer" >
       select LAST_INSERT_ID() as id
       </selectKey>
     </insert>

  

Background code unchanged.

Obtaining each database is not the same, according to the present embodiment mysql Example. Please inquire as needed each other.

Guess you like

Origin www.cnblogs.com/mh-study/p/11899451.html