The fourth chapter of Mybatis: addition, deletion and modification

  Mainly include the type of return value added and deleted and the way of obtaining the primary key

First, jdbc performs addition, deletion and modification. The default return type is int. The return value type supported by Mybatis is more powerful than jdbc.

int
Integer
long 
Long
boolean
Boolean
void

  The processing source code for adding, deleting, and modifying the return value for Mybatis is as follows.

private Object rowCountResult(int rowCount) {
final Object result;
if (method.returnsVoid()) {
result = null;
} else if (Integer.class.equals(method.getReturnType()) || Integer.TYPE.equals(method.getReturnType())) {
result = rowCount;
} else if (Long.class.equals(method.getReturnType()) || Long.TYPE.equals(method.getReturnType())) {
result = (long)rowCount;
} else if (Boolean.class.equals(method.getReturnType()) || Boolean.TYPE.equals(method.getReturnType())) {
result = rowCount > 0;
} else {
throw new BindingException("Mapper method '" + command.getName() + "' has an unsupported return type: " + method.getReturnType());
}
return result;
}

  The return value of Mybatis is actually the conversion processing of the return value of the int type of jdbc.

 Second, the Mybatis primary key acquisition method

  1. Internally use the built-in method of jdbc: set it in the XML file, add the useGeneratedKeys property to true to the select tag, set keyProperty: the property name in the parameter object, and after the successful insertion, mybatis will increase the value by reflection Set the property specified for keyProperty.

  2. Query to obtain the primary key after insertion: The key code is the rewriting of the sql of the xml file, mainly to add the part of the code of the selectKey tag, where the keyProperty configures the field mapped in the bean object, and the order configuration after means that after insert operating.

  SELECT LAST_INSERT_ID (): get the value of the primary key just inserted into the record, only applicable to self-incrementing primary key ;

<insert id="insert" parameterType="entity.TUser">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into t_user (`name`, age, salary, 
      sex)
    values (#{name,jdbcType=VARCHAR}, #{age,jdbcType=SMALLINT}, #{salary,jdbcType=DECIMAL}, 
      #{sex,jdbcType=TINYINT})
  </insert>

  3. Query to get the primary key before inserting: The SQL statement of xml is similar to the second case, the key code is also the statement in the selectKey part, and the select part of which needs to be replaced with a custom function. Last_insert_id cannot be used.

  last_insert_id can only be used when there is a self-increasing primary key. Querying the primary key before inserting can be applied to the case where it is not a self-increasing primary key. Both have problems under high concurrency.

Guess you like

Origin www.cnblogs.com/8593l/p/12705088.html