In daily development, tags in MyBatis play an important role. The following are some common tags and cases in the MyBatis framework:
1. <select> tag
<!-- Query statement:resultType="" --> <select parameterType="int" resultType=""> SELECT * FROM T_Users WHERE id = #{id} </select> <!-- Query statement:resultType=" --> <select resultType=""> SELECT * FROM T_User WHERE USER_ID=#{userId,jdbcType=VARCHAR} </select> <!-- Query statement:parameterType="long" --> <select parameterType="long" resultType=""> SELECT count(*) FROM T_User_RECORD where user_id= #{userId,jdbcType=BIGINT} </select> <!-- Query statement:resultMap="templateMap" --> <select parameterType="int" resultMap="templateMap"> SELECT * FROM T_Users WHERE id = #{id} </select> <!-- Query statement:resultType=" --> <select resultType=""> SELECT count(*) FROM T_USER_RECORD </select> <!-- Query statement:Add to<if> Label,uniongrammar,foreachgrammar --> <select parameterType="" resultType=""> SELECT USER_ID,USER_NAME,DEPT_CODE,DEPT_NAME FROM T_USER where STATUS = 'Y' <if test="(flag_column== 'N'.toString()) "> AND dept_leader = 'Y' </if> UNION SELECT USER_ID,USER_NAME,DEPT_CODE,DEPT_NAME FROM T_USER where STATUS = 'Y' <if test="list!= null and > 0"> AND <foreach collection="list" index="index" item="item" open="(" separator="OR" close=")"> T.USER_ID = #{item,jdbcType=VARCHAR} </foreach> </if>) </select> <!-- Execute stored procedures --> <select parameterType="String" useCache="false" statementType="CALLABLE"> <![CDATA[ {call P_PROCEDURE( #{name,mode=IN,jdbcType=VARCHAR}, #{age,mode=OUT,jdbcType=VARCHAR} )} ]]> </select>
Explanation: This is the most basic tag used to execute SQL queries, according to the incomingid
Parameters retrieve user information from the users table.
2. <insert> tag
<!-- Newuser --> <insert parameterType=""> INSERT INTO users(name, email, created_at) VALUES (#{name,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, SYSDATE) </insert> <!-- Get from the sequencekey --> <insert parameterType=""> <selectKey keyProperty="id" resultType="long" order="BEFORE" > SELECT SEQ_USER.NEXTVAL FROM DUAL </selectKey> INSERT INTO T_USER(ID,NAME,EMAIL,CREATE_TIME) VALUES(#{id,jdbcType=BIGINT},#{name,jdbcType=VARCHAR},#{email,jdbcType=VARCHAR},SYSDATE) </insert> <!-- MERGE INTOStatement --> <insert parameterType=""> MERGE INTO T_USER_WISHS W USING (SELECT #{id,jdbcType=BIGINT} ID, #{name,jdbcType=VARCHAR} name, FROM DUAL ) D ON ( = ) WHEN MATCHED THEN UPDATE SET = , W.CREATE_TIME = D.CREATE_TIME, W.UPDATE_TIME = SYSDATE WHEN NOT MATCHED THEN INSERT ( , , W.CREATE_TIME ) VALUES( , , SYSDATE ) </insert> <!-- batchNew --> <insert parameterType=""> INSERT INTO T_USER(ID,USER_ID,USER_NAME,CREATE_TIME) SELECT SEQ_USER.NEXTVAL,T.* FROM ( <foreach collection="list" item="item" index="index" separator="union all"> SELECT #{,jdbcType=VARCHAR} USER_ID, #{,jdbcType=VARCHAR} USER_NAME, SYSDATE CREATE_TIME FROM DUAL </foreach>) T </insert> <!-- batchmerge into --> <insert > MERGE INTO T_USER_SCORE S USING ( <foreach collection="list" item="item" index="index" separator="UNION ALL"> SELECT #{,jdbcType=VARCHAR} USER_ID, #{,jdbcType=VARCHAR} USER_NAME, #{,jdbcType=VARCHAR} SCORE from dual </foreach> ) D ON (S.STAFF_ID = D.STAFF_ID AND S.USER_NAME= D.USER_NAME) WHEN MATCHED THEN UPDATE SET = , S.UPDATE_TIME = SYSDATE WHEN NOT MATCHED THEN INSERT ( , S.USER_ID, S.USER_NAME, , S.CREATE_TIME) VALUES( SEQ_USER_SCORE.NEXTVAL, D.USER_ID, D.USER_NAME, , SYSDATE) </insert>
3. <update> tag
<!-- renewuser --> <update parameterType=""> UPDATE users <set> name = #{name}, email = #{email} </set> WHERE id = #{id} </update> <!-- renewuser:use<if> --> <update > UPDATE T_USER_INFO SET AGE= #{age,jdbcType=VARCHAR}, <if test="finishFlag != null"> FINISH_FLAG = #{age,jdbcType=VARCHAR}, </if> STATUS = #{status,jdbcType=VARCHAR} WHERE USER_ID = #{userId,jdbcType=VARCHAR} </update> <!-- renewuser:existwhere后面use<if> --> <update parameterType=""> UPDATE T_USER SET UPDATE_TIME = SYSDATE, WHERE USER_FLAG='Y' <if test="isFr != null and isFr== 'Y'.toString()"> AND USER_ID = #{userId,jdbcType=VARCHAR} </if> </update> <!-- renewuser:usesetLabel --> <update parameterType=""> UPDATE T_USER <set> <if test="status != null and status != ''"> STATUS = #{status,jdbcType=VARCHAR}, </if> UPDATE_DATE = SYSDATE </set> WHERE USER_ID = #{userId} </update> <!-- 批量renewuser --> <update > <foreach collection="list" item="item" open="begin" close=";end;" separator=";" index="index"> UPDATE T_USER SET age= #{, jdbcType=VARCHAR} WHERE USER_ID = #{, jdbcType=VARCHAR} </foreach> </update> <!-- 批量renewuser --> <update > <foreach collection="items" item="item" separator=";" index="index"> UPDATE ${} SET ${} = #{} WHERE ${} = #{} </foreach> </update>
Explanation: According toid
Update user records, onlyname
andemail
The corresponding field will be updated only when it is not empty.
4. <delete> tag
<!-- deleteuser --> <delete parameterType="int"> DELETE FROM users WHERE id = #{id} </delete>
Explanation: According to the providedid
Delete a record in the users table.
5. Dynamic SQL tags
a. <if>
Label
<update > UPDATE users <set> <if test="name != null">name = #{name},</if> <if test="email != null">email = #{email}</if> </set> WHERE id = #{id} </update>
Explanation: Only if the parameters passed inname
oremail
Not fornull
The corresponding fields will be updated only when .
b. <choose>
, <when>
, <otherwise>
Label
<update > UPDATE users <set> <choose> <when test="operation == 'updateName'"> name = #{newName} </when> <when test="operation == 'updateEmail'"> email = #{newEmail} </when> <otherwise> status = #{newStatus} </otherwise> </choose> </set> WHERE id = #{userId} </update>
Explanation: According to the type of operation passed in (operation
) Select to update different fields.
c. <where>
Label
<update > UPDATE users <set> last_login = NOW() </set> <where> <if test="id != null">id = #{id}</if> <if test="email != null">AND email = #{email}</if> </where> </update>
explain:<where>
Tags ensure that the WHERE conditional sentence syntax in the generated SQL is correct, and even if the condition is empty, there will be no unnecessary ones.AND
。
d. <foreach>
Tags (looping through the collection)
<insert > INSERT INTO users(id, name) VALUES <foreach item="item" index="index" collection="list" separator=","> (#{}, #{}) </foreach> </insert>
Explanation: Batch insertion of users,list
A parameter is a collection of multiple user objects.
6. Reference other SQL fragments
<sql >id, name, email</sql> <select resultType=""> SELECT <include ref/> FROM users </select>
Explanation: Define reusable SQL fragments and pass<include>
Label introduction.
7. <resultMap> tag
<resultMap type=""> <id property="id" column="user_id"/> <result property="name" column="username"/> <association property="address" javaType=""> <id property="addressId" column="address_id"/> <result property="street" column="street"/> </association> </resultMap>
Explanation: Used to map query results to Java objects, supporting complex mapping relationships such as one-to-one and one-to-many.
The above has compiled some very practical and commonly used tags in MyBatis. Hope it can help everyone.
statement
The copyright of this content belongs to CSDN-Little Wild Boar. Any unauthorized copying, reprinting, dissemination, sales, and gifting are illegal and legal liability will be pursued! ! !
This is the end of this article about the commonly used tags of Mybatis. For more related content of commonly used tags of Mybatis, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!