Use mybatis to add a piece of data and return the id of the new data

the first method

<insert id="insertAndGetId" useGeneratedKeys="true" keyProperty="userId" parameterType="com.icc.domain.User">
    insert into user(userName,password)
    values(#{userName},#{password})
</insert>

Precautions:

useGeneratedKeys="true" means to set self-growth for the primary key, and keyProperty="userId" means to assign the self-growth Id to the userId field in the entity class.

parameterType="com.icc.domain.User" This attribute points to the passed parameter entity class

Here's a reminder, there is no resultType attribute in <insert></insert>, don't add it randomly.

The uerId in the entity class must have getter() and setter(); method

The second way:

    <insert id="insertProduct" parameterType="com.icc.domain.ProductBean" >
       <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="productId">
          SELECT LAST_INSERT_ID()
      </selectKey>
        INSERT INTO t_product(productName,productDesrcible,merchantId)values(#{productName},#{productDesrcible},#{merchantId});

Precautions:

There is no resultType attribute in <insert></insert>, but there are <selectKey></selectKey> tags.

order="AFTER" means to execute the insert statement first, and then execute the query statement.

Can be set to BEFORE or AFTER.

If set to BEFORE, then it will first select the primary key, set the keyProperty it will be executed before the insert statement.

If set to AFTER, execute after the insert statement is executed.

keyProperty="userId" indicates that the self-increased Id is assigned to the userId field in the entity class.

SELECT LAST_INSERT_ID() indicates that the self-incrementing Id of the record just inserted is queried in MySQL syntax.

The uerId in the entity class must have getter() and setter(); method

Take out method

After Mybatis executes the insert statement, it automatically assigns the self-increasing value to the attribute id of the object ProductBean. Therefore, it can be obtained through the corresponding getter method of systemBean!

int count = systemService.insert(productBean);    
        
int id = productBean.getproductId(); //获取到的即为新插入记录的ID 

If you use the following sequence.nextval to set the id, you can get it directly through the get method of the entity class

<insert id="insertDept" parameterType="SysDept">
        <selectKey keyProperty="deptId" order="BEFORE" resultType="Long">
            select seq_sys_dept.nextval as deptId from DUAL
        </selectKey>
         insert into sys_dept(
             <if test="deptId != null and deptId != 0">dept_id,</if>
             <if test="deptName != null and deptName != ''">dept_name,</if>
             <if test="leader != null and leader != ''">leader</if>
          
         )values(
             <if test="deptId != null and deptId != 0">#{deptId},</if>
             <if test="deptName != null and deptName != ''">#{deptName},</if>
             <if test="leader != null and leader != ''">#{leader}</if>
        
         )
    </insert>
deptMapper.insertDept(dept);
        System.out.println("==============================="+dept.getDeptId());

Guess you like

Origin blog.csdn.net/weixin_55823910/article/details/129532516