insert, update, delete and sql tags

In this section we talk about the <insert>, <update>, <delete> and <sql> elements in MyBatis.

<insert> element

The <insert> element is used to map the insert statement. After MyBatis executes an insert statement, it will return an integer indicating the number of rows it affects. Most of its attributes are the same as those of the <select> element. In this section, several of its unique attributes are explained.

  • keyProperty: The function of this attribute is to assign the return value of the insert or update operation to a certain attribute of the PO class, which is usually set to the attribute corresponding to the primary key. If it is a joint primary key, multiple values ​​can be separated by commas.
  • keyColumn: This attribute is used to set which column is the primary key, and it needs to be set when the primary key column is not the first column in the table. If it is a joint primary key, multiple values ​​can be separated by commas.
  • useGeneratedKeys: This attribute will enable MyBatis to use JDBC's getGeneratedKeys() method to obtain the primary key generated inside the database, such as  MySQL , SQL Server and other auto-incrementing fields, and its default value is false.

1) Primary key (auto-increment) backfill

Tables in databases such as MySQL and SQL Server can use auto-incrementing fields as primary keys, and sometimes you may need to use this newly generated primary key to associate other businesses.

First, add the keyProperty and useGeneratedKeys attributes to the <insert> element whose id is addUser in the SQL mapping file UserMapper.xml in the com.mybatis package. The specific code is as follows:

<!--Add a user, and return the primary key value to uid (po attribute) after success-->
<insert id="addUser" parameterType="com.po.MyUser" keyProperty="uid" useGeneratedKeys=" true">
    insert into user (uname,usex) values(#{uname},#{usex})
</insert>

Then call it in the UserController class of the com.controller package, the specific code is as follows:

// add a user
MyUser addmu = new MyUser();
addmu.setUname("Chen Heng");
addmu.setUsex("男");
int add = userDao.addUser(addmu);
System.out.println("Added" + add + "records");
System.out.println("The primary key of the added record is " + addmu.getUid());

2) Custom primary key

If the database used in the actual project does not support the auto-increment of the primary key (such as Oracle), or cancels the rules of the auto-increment of the primary key, you can use the <selectKey> element of MyBatis to customize the primary key. The specific configuration sample code is as follows:

<!-- Add a user, #{uname} is the property value of com.mybatis.po.MyUser -->
<insert id="insertUser" parameterType="com.po.MyUser">
    <!-- First use the selectKey element to define the primary key, and then define the SQL statement -->
    <selectKey keyProperty="uid" resultType="Integer" order="BEFORE">
    select if(max(uid) is null,1,max(uid)+1) as newUid from user)
    </selectKey>
    insert into user (uid,uname,usex) values(#{uid},#{uname},#{usex})
</insert>

When the above sample code is executed, the <selectKey> element is executed first, which sets the primary key of the data table through a custom statement, and then executes the insert statement.

The keyProperty attribute of the <selectKey> element specifies which property of the PO class (com.po.MyUser) the nascent primary key value is returned to.

  • The order attribute can be set to BEFORE or AFTER.
  • BEFORE means to execute the <selectKey> element first and then execute the insert statement.
  • AFTER means to execute the insert statement first and then execute the <selectKey> element.

<update> and <delete> elements

The <update> and <delete> elements are relatively simple, and their attributes are similar to those of the <insert> element and <select> element. After execution, they also return an integer, indicating the number of rows affected by the database. The configuration sample code is as follows:

<!-- modify a user -->
<update id="updateUser" parameterType="com.po.MyUser">
    update user set uname = #{uname},usex = #{usex} where uid = #{uid}
</update>
<!-- delete a user -->
<delete id="deleteUser" parameterType="Integer">
    delete from user where uid = #{uid}
</delete>

<sql> element

The role of the <sql> element is to define a part of the SQL statement (code fragment), so that it can be referenced by subsequent SQL statements, such as the column name used repeatedly.

Write it once in MyBatis using the <sql> element and reference it in other elements. The configuration sample code is as follows:

<sql id="comColumns">id,uname,usex</sql>
<select id="selectUser" resultType="com.po.MyUser">
    select <include refid="comColumns"> from user
</select>

The custom code fragment is referenced in the above code using the refid attribute of the <include> element.

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/132358729