The SelectKey of the Mybatis example, the mysql database setting auto-increment also needs to use the production primary key

SelectKey is used in Mybatis to solve the problem that the primary key is not automatically generated when inserting data. He can freely set the way to generate the primary key.

No matter how good the SelectKey is, try not to encounter this situation, after all, it is very troublesome. ( bulk insert AFTER )

 

selectKey Attributes attribute description
keyProperty The target property to which the result of the selectKey statement should be set.
resultType The type of result. MyBatis can usually be calculated, but there is no problem in writing. MyBatis allows any simple type to be used as a primary key type, including strings.
order This can be set to BEFORE or AFTER. If set to BEFORE, then it will first select the primary key, set the keyProperty and then execute the insert statement. If set to AFTER, the insert statement is executed first, followed by the selectKey element - this is similar to, for example, Oracle databases, where sequence calls can be embedded in the insert statement.
statementType As before, MyBatis supports the mapping types of STATEMENT, PREPARED and CALLABLE statements, representing PreparedStatement and CallableStatement types respectively.

 

SelectKey needs to pay attention to the order attribute. In a database that supports automatic growth such as Mysql, the order needs to be set to after to get the correct value.

In the case of taking a sequence like Oracle, it needs to be set to before, otherwise an error will be reported.

 

 

The following is an example of xml and annotations, SelectKey is very simple, two examples are enough:

 

[html]  view plain copy  
 
  1. <insert id="insert" parameterType="map">  
  2.     insert into table1 (name) values (#{name})  
  3.     <selectKey resultType="java.lang.Integer" keyProperty="id">  
  4.       CALL IDENTITY()  
  5.     </selectKey>  
  6.   </insert>  


上面xml的传入参数是map,selectKey会将结果放到入参数map中。用POJO的情况一样,但是有一点需要注意的是,keyProperty对应的字段在POJO中必须有相应的setter方法,setter的参数类型还要一致,否则会报错。

 

 

 

[java]  view plain  copy
 
  1. @Insert("insert into table2 (name) values(#{name})")  
  2. @SelectKey(statement="call identity()", keyProperty="nameId", before=false, resultType=int.class)  
  3. int insertTable2(Name name);  

上面是注解的形式。

 

mysql,1数据设置自增,2,用selectKey,否则自增无效导致连续多次插入失效,只有第一次可以

 

<insert id="insert" parameterType="com.houbank.incoming.dao.dataObject.FinancialSalesOrderDO" >
  <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
    SELECT LAST_INSERT_ID()
  </selectKey>
  insert into financial_sales_order (ID, BATCH_NAME, ORDERNO, 
    ORDER_TYPE, REGISTER_SRC, CUSTOMER_NAME, 
    MOBILE, ISINVEST, REGISTER_TIME, 
    CITY, TEM_ID, EMP_ID, 
    ASSIGN_TIME, STATUS, FIRSTDAIL_TIME, 
    LASTDAIL_TIME, NEXTCONTRACT_TIME, DIAL_RESULT, 
    DAIL_COUNT, CREATED_TIME, UPDATED_TIME, 
    INVEST_LEAVEL, CREATED_BY, UPDATED_DY
    )
  values (#{id,jdbcType=BIGINT}, #{batchName,jdbcType=VARCHAR}, #{orderno,jdbcType=VARCHAR}, 
    #{orderType,jdbcType=VARCHAR}, #{registerSrc,jdbcType=VARCHAR}, #{customerName,jdbcType=VARCHAR}, 
    #{mobile,jdbcType=VARCHAR}, #{isinvest,jdbcType=CHAR}, #{registerTime,jdbcType=TIMESTAMP}, 
    #{city,jdbcType=VARCHAR}, #{temId,jdbcType=VARCHAR}, #{empId,jdbcType=VARCHAR}, 
    #{assignTime,jdbcType=TIMESTAMP}, #{status,jdbcType=CHAR}, #{firstdailTime,jdbcType=TIMESTAMP},
    #{lastdailTime,jdbcType=TIMESTAMP}, #{nextcontractTime,jdbcType=TIMESTAMP}, #{dialResult,jdbcType=VARCHAR},
    #{dailCount,jdbcType=VARCHAR}, #{createdTime,jdbcType=TIMESTAMP}, #{updatedTime,jdbcType=TIMESTAMP},
    #{investLeavel,jdbcType=CHAR}, #{createdBy,jdbcType=VARCHAR}, #{updatedDy,jdbcType=VARCHAR}
    )
</insert>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326073457&siteId=291194637