Mybatis常用標(biāo)簽整理
日常開(kāi)發(fā)中,MyBatis中標(biāo)簽有著舉足輕重的重要性。以下是一些MyBatis框架中常見(jiàn)的標(biāo)簽及案例:
1. <select> 標(biāo)簽
<!-- 查詢(xún)語(yǔ)句:resultType="com.example.User" -->
<select id="selectUserById" parameterType="int" resultType="com.example.User">
SELECT * FROM T_Users WHERE id = #{id}
</select>
<!-- 查詢(xún)語(yǔ)句:resultType="java.lang.String -->
<select id="queryAliyunPictureName" resultType="java.lang.String">
SELECT * FROM T_User WHERE USER_ID=#{userId,jdbcType=VARCHAR}
</select>
<!-- 查詢(xún)語(yǔ)句:parameterType="long" -->
<select id="queryPrizeNum" parameterType="long" resultType="java.lang.Integer">
SELECT count(*) FROM T_User_RECORD where user_id= #{userId,jdbcType=BIGINT}
</select>
<!-- 查詢(xún)語(yǔ)句:resultMap="templateMap" -->
<select id="selectUserById" parameterType="int" resultMap="templateMap">
SELECT * FROM T_Users WHERE id = #{id}
</select>
<!-- 查詢(xún)語(yǔ)句:resultType="java.lang.Integer -->
<select id="queryAllUserNum" resultType="java.lang.Integer">
SELECT count(*) FROM T_USER_RECORD
</select>
<!-- 查詢(xún)語(yǔ)句:添加<if> 標(biāo)簽,union語(yǔ)法,foreach語(yǔ)法 -->
<select id="queryUser" parameterType="com.example.UserVO" resultType="com.example.User">
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 list.size > 0">
AND
<foreach collection="list" index="index" item="item" open="(" separator="OR" close=")">
T.USER_ID = #{item,jdbcType=VARCHAR}
</foreach>
</if>)
</select>
<!-- 執(zhí)行存儲(chǔ)過(guò)程 -->
<select id="callPprocedure" parameterType="String" useCache="false" statementType="CALLABLE">
<![CDATA[
{call P_PROCEDURE(
#{name,mode=IN,jdbcType=VARCHAR},
#{age,mode=OUT,jdbcType=VARCHAR}
)}
]]>
</select>解釋?zhuān)哼@是用于執(zhí)行SQL查詢(xún)的最基本標(biāo)簽,根據(jù)傳入的id參數(shù)從users表中檢索用戶(hù)信息。
2. <insert> 標(biāo)簽
<!-- 新增user -->
<insert id="insertUser" parameterType="com.example.User">
INSERT INTO users(name, email, created_at)
VALUES (#{name,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, SYSDATE)
</insert>
<!-- 從序列中獲取key -->
<insert id="addUser" parameterType="com.example.User">
<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 INTO語(yǔ)句 -->
<insert id="addOrUpdateUser" parameterType="com.example.User">
MERGE INTO T_USER_WISHS W
USING (SELECT
#{id,jdbcType=BIGINT} ID,
#{name,jdbcType=VARCHAR} name,
FROM DUAL ) D
ON (W.ID = D.ID)
WHEN MATCHED THEN UPDATE SET
W.NAME= D.NAME,
W.CREATE_TIME = D.CREATE_TIME,
W.UPDATE_TIME = SYSDATE
WHEN NOT MATCHED THEN
INSERT (
W.ID,
W.NAME,
W.CREATE_TIME
)
VALUES(
D.ID,
D.NAME,
SYSDATE
)
</insert>
<!-- 批量新增 -->
<insert id="addSUserList" parameterType="java.util.List">
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
#{item.userId,jdbcType=VARCHAR} USER_ID,
#{item.userName,jdbcType=VARCHAR} USER_NAME,
SYSDATE CREATE_TIME
FROM DUAL
</foreach>) T
</insert>
<!-- 批量merge into -->
<insert id="batchInsertUser">
MERGE INTO T_USER_SCORE S
USING (
<foreach collection="list" item="item" index="index" separator="UNION ALL">
SELECT
#{item.userId,jdbcType=VARCHAR} USER_ID,
#{item.userName,jdbcType=VARCHAR} USER_NAME,
#{item.score,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.SCORE = D.SCORE,
S.UPDATE_TIME = SYSDATE
WHEN NOT MATCHED THEN
INSERT (
S.ID,
S.USER_ID,
S.USER_NAME,
S.SCORE,
S.CREATE_TIME)
VALUES(
SEQ_USER_SCORE.NEXTVAL,
D.USER_ID,
D.USER_NAME,
D.SCORE,
SYSDATE)
</insert>3. <update> 標(biāo)簽
<!-- 更新user -->
<update id="updateUser" parameterType="com.example.User">
UPDATE users
<set>
name = #{name},
email = #{email}
</set>
WHERE id = #{id}
</update>
<!-- 更新user:使用<if> -->
<update id="updUserByUserId">
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>
<!-- 更新user:在where后面使用<if> -->
<update id="updateProtocalFlagExt" parameterType="java.lang.String">
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>
<!-- 更新user:使用set標(biāo)簽 -->
<update id="updateUser" parameterType="com.example.User">
UPDATE T_USER
<set>
<if test="status != null and status != ''">
STATUS = #{status,jdbcType=VARCHAR},
</if>
UPDATE_DATE = SYSDATE
</set>
WHERE USER_ID = #{userId}
</update>
<!-- 批量更新user -->
<update id="updateUser" >
<foreach collection="list" item="item" open="begin" close=";end;" separator=";" index="index">
UPDATE T_USER
SET age= #{item.age, jdbcType=VARCHAR}
WHERE USER_ID = #{item.userId, jdbcType=VARCHAR}
</foreach>
</update>
<!-- 批量更新user -->
<update id="updateUser">
<foreach collection="items" item="item" separator=";" index="index">
UPDATE ${item.tablesName} SET ${item.columnsName} = #{item.age}
WHERE ${item.columnsUser} = #{item.userId}
</foreach>
</update>解釋?zhuān)焊鶕?jù)id更新用戶(hù)記錄,只有name和email非空時(shí)才會(huì)更新相應(yīng)的字段。
4. <delete> 標(biāo)簽
<!-- 刪除user -->
<delete id="deleteUser" parameterType="int">
DELETE FROM users WHERE id = #{id}
</delete>解釋?zhuān)焊鶕?jù)提供的id刪除users表中的某條記錄。
5. 動(dòng)態(tài)SQL標(biāo)簽
a. <if> 標(biāo)簽
<update id="conditionalUpdate">
UPDATE users
<set>
<if test="name != null">name = #{name},</if>
<if test="email != null">email = #{email}</if>
</set>
WHERE id = #{id}
</update>解釋?zhuān)簝H當(dāng)傳入的參數(shù)name或email不為null時(shí),才會(huì)更新相應(yīng)的字段。
b. <choose>, <when>, <otherwise> 標(biāo)簽
<update id="chooseUpdate">
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>解釋?zhuān)焊鶕?jù)傳入的操作類(lèi)型(operation)選擇更新不同的字段。
c. <where> 標(biāo)簽
<update id="complexUpdate">
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>解釋?zhuān)?code><where>標(biāo)簽確保生成的SQL中WHERE條件句語(yǔ)法正確,即使條件為空也不會(huì)出現(xiàn)多余的AND。
d. <foreach> 標(biāo)簽(循環(huán)遍歷集合)
<insert id="batchInsertUsers">
INSERT INTO users(id, name)
VALUES
<foreach item="item" index="index" collection="list" separator=",">
(#{item.id}, #{item.name})
</foreach>
</insert>解釋?zhuān)号坎迦胗脩?hù),list參數(shù)是一個(gè)包含多個(gè)用戶(hù)對(duì)象的集合。
6. 引用其他SQL片段
<sql id="userColumns">id, name, email</sql>
<select id="selectAllUsers" resultType="com.example.User">
SELECT
<include refid="userColumns"/>
FROM users
</select>解釋?zhuān)憾x可重用的SQL片段并通過(guò)<include>標(biāo)簽引入。
7. <resultMap> 標(biāo)簽
<resultMap id="userResultMap" type="com.example.User">
<id property="id" column="user_id"/>
<result property="name" column="username"/>
<association property="address" javaType="com.example.Address">
<id property="addressId" column="address_id"/>
<result property="street" column="street"/>
</association>
</resultMap>解釋?zhuān)河糜谟成洳樵?xún)結(jié)果到Java對(duì)象,支持一對(duì)一、一對(duì)多等復(fù)雜映射關(guān)系。
上面整理了MyBatis中一些非常實(shí)用和常用的標(biāo)簽。希望能幫助大家。
聲明
本內(nèi)容版權(quán)歸屬于CSDN-小小野豬,任何未經(jīng)授權(quán)的復(fù)制、轉(zhuǎn)載、傳播、販賣(mài)、轉(zhuǎn)贈(zèng)等均屬違法行為,必將追究法律責(zé)任?。?!
到此這篇關(guān)于Mybatis常用標(biāo)簽整理的文章就介紹到這了,更多相關(guān)Mybatis常用標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis動(dòng)態(tài)<if>標(biāo)簽的使用
- Mybatis映射文件之常用標(biāo)簽及特殊字符的處理方法
- MyBatis動(dòng)態(tài)<if>標(biāo)簽使用避坑指南
- Mybatis中where標(biāo)簽與if標(biāo)簽結(jié)合使用詳細(xì)說(shuō)明
- MyBatis-Plus標(biāo)簽@TableField之fill自動(dòng)填充方式
- Mybatis的mapper.xml中if標(biāo)簽test判斷的用法說(shuō)明
- Mybatis的xml中使用if/else標(biāo)簽的具體使用
- mybatis中foreach嵌套if標(biāo)簽方式
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)和算法中哈希表知識(shí)點(diǎn)詳解
在本篇文章里小編給大家分享了關(guān)于java數(shù)據(jù)結(jié)構(gòu)和算法中哈希表的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-06-06
java 注解實(shí)現(xiàn)一個(gè)可配置線程池的方法示例
這篇文章主要介紹了java 注解實(shí)現(xiàn)一個(gè)可配置線程池的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
SpringBoot集成極光推送完整實(shí)現(xiàn)代碼
本文主要介紹了SpringBoot集成極光推送完整實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Java實(shí)現(xiàn)中文字符串與unicode互轉(zhuǎn)工具類(lèi)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)中文字符串與unicode互轉(zhuǎn)的工具類(lèi),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Java 數(shù)據(jù)庫(kù)連接(JDBC)的相關(guān)總結(jié)
這篇文章主要介紹了Java 數(shù)據(jù)庫(kù)連接(JDBC)的相關(guān)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-03-03
利用Spring Boot創(chuàng)建docker image的完整步驟
這篇文章主要給大家介紹了關(guān)于如何利用Spring Boot創(chuàng)建docker image的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java編程線程同步工具Exchanger的使用實(shí)例解析
這篇文章主要介紹了Java編程線程同步工具Exchanger的使用實(shí)例解析,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02

