MyBatis注解實現(xiàn)動態(tài)SQL問題
MyBatis注解實現(xiàn)動態(tài)SQL
在 Mybatis 中,使用注解可以很方便的進(jìn)行sql操作,但很多動態(tài) SQL 都是由 xml 配置實現(xiàn)的。
而隨著 SpringBoot的逐漸發(fā)展,越來越多的配置由配置文件轉(zhuǎn)成注解的形式。其中包括動態(tài) SQL 。
Mybatis 的注解中,使用太過長的sql語句看起來很不美觀。
@Select("SELECT title from book_tbl where id = #{id}")如果想要在 mapper 中進(jìn)行非必要關(guān)鍵字的查詢時就需要使用動態(tài) SQL,與 xml 配置不同的是,@Select注解中 SQL 語句必須以 <script> 標(biāo)簽包裹。
@Select("<script>"+
"SELECT id " +
"from book_tbl" +
"<where>" +
"<if test 'company_id != null'>" +
"and company_id = #{company_id}" +
"</if>" +
"<if test 'title != null'>" +
"and title like CONCAT('%',#{title},'%')" +
"</if>" +
"</where>" +
"ORDER BY create_time desc,rank desc" +
"</script>")
MyBatis動態(tài)拼接 SQL參數(shù)
之前用JPA可以在@Query注解上拼接sql,實現(xiàn)動態(tài)查詢;現(xiàn)在用mybatis,如何實現(xiàn)sql的動態(tài)拼接參數(shù)
舉例,在JPA中可以實現(xiàn)類似于下面的sql拼接
plateNumber2 = plateNumber2 + "," + plateNumber;
String[] split = plateNumber2.split(",");
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("SELECT plate_number from p_park_record where park_key = '"+parkDO.getParkKey()+"'");
stringBuffer.append(" and (");
for (int i = 0; i < split.length; i++) {
? ? stringBuffer.append(" plate_number = "+split[i]);
? ? if (i != split.length-1){
? ? ? ? stringBuffer.append(" OR ");
? ? }
}
stringBuffer.append(" )");
stringBuffer.append(" order by sensor_exit_time desc");
stringBuffer.append(" limit 1");此代碼中,核心問題是for循環(huán),將不確定數(shù)量的同一個參數(shù)做拼接
那么在mybatis中該如何實現(xiàn)
mybatis實現(xiàn):
plateNumber2 = plateNumber2 + "," + plateNumber; ParkRecordDO parkRecordDO = new ParkRecordDO(); parkRecordDO.setPlateNumber(plateNumber2); parkRecordDO.setParkKey(parkDO.getParkKey()); String onlyPlateNum = parkRecordMapper.getOnlyPlateNum(parkRecordDO);
? ? <select id="getOnlyPlateNum" parameterType="ParkRecordDO" resultType="java.lang.String">
? ? ? ? SELECT plate_number from p_park_record where park_key = #{parkKey} and sensor_exit_time is not null and
? ? ? ? <foreach collection="plateNumber.split(',')" index="index" item="id" open="(" separator=" or " close=")">
? ? ? ? ? ? plate_number = #{id}
? ? ? ? </foreach>
? ? ? ? order by sensor_exit_time desc
? ? ? ? limit 1
? ? </select>如此實現(xiàn),與JPA拼接效果相同,重點在于“<foreach”標(biāo)簽中separator屬性,該屬性為元素之間的分隔符,將分隔符設(shè)置為“or”,以此來實現(xiàn)該程序邏輯的拼接
最后補充幾個知識點
foreach標(biāo)簽的使用
<!--
? ? ? ? foreach
? ? ? ? collection:指定要遍歷的集合
? ? ? ? ? ? ? ? ? ? list類型的參數(shù)會特殊處理封裝在map中,map的key就叫l(wèi)ist
? ? ? ? ? ? ? item:將當(dāng)前遍歷出的元素賦值給指定的變量
? ? ? ? ? ? ? separator:元素之間的分割符
? ? ? ? ? ? ? open: 遍歷出的所有結(jié)果拼接一個開始字符
? ? ? ? ? ? ? close:遍歷出的所有結(jié)果拼接一個結(jié)束字符
? ? ? ? ? ? ? index:索引,遍歷list的時候是索引,item就是當(dāng)前值;
? ? ? ? ? ? ? ? ? ? ? ? ? 遍歷map時index標(biāo)識的就是map的key,item就是map的值
? ? ? ? ? ? ? #{變量名}: 就能取出變量的值就是當(dāng)前遍歷出的元素
-->
<select id="queryUserByForEach" resultType="user">
? ? select * from easybuy_user where id in
? ? <foreach collection="ids" item="item_id" separator=","
? ? ? ? ? ? ?open="(" close=")">
? ? ? ? #{item_id}
? ? </foreach>
</select>
<insert id="batchSaveUser" parameterType="user">
? ? insert into easybuy_user(loginName, userName, password) values
? ? <foreach collection="userList" item="user" separator=",">
? ? ? ? (#{user.loginName}, #{user.userName}, #{user.password})
? ? </foreach>
</insert>set標(biāo)簽的使用
<!--
? ? ? ? 使用set標(biāo)簽或者trim標(biāo)簽與if標(biāo)簽相結(jié)合
? ? ? ? 實現(xiàn)動態(tài)更新sql語句的拼接
-->
<update id="updateUserByCondition" parameterType="user">
? ? update easybuy_user
? ? <set>
? ? ? ? <if test="userName != null">
? ? ? ? ? ? username = #{userName},
? ? ? ? </if>
? ? ? ? <if test="email != null">
? ? ? ? ? ? email = #{email},
? ? ? ? </if>
? ? </set>
? ? where id = 26;
</update>choose和when標(biāo)簽的使用(作用類似與java中的switch-case)
<select id="queryUserByChoose" resultType="com.unistart.entity.User">
? ? select * from easybuy_user
? ? <where>
? ? ? ? <choose>
? ? ? ? ? ? <when test="id != null">
? ? ? ? ? ? ? ? id=#{id}
? ? ? ? ? ? </when>
? ? ? ? ? ? <when test="userName != null">
? ? ? ? ? ? ? ? userName like #{userName}
? ? ? ? ? ? </when>
? ? ? ? ? ? <when test="sex != null">
? ? ? ? ? ? ? ? sex = #{sex}
? ? ? ? ? ? </when>
? ? ? ? ? ? <otherwise>
? ? ? ? ? ? ? ? 1=1;
? ? ? ? ? ? </otherwise>
? ? ? ? </choose>
? ? </where>
</select>trim標(biāo)簽的使用
<!--
? ? ? ? trim標(biāo)簽的使用:解決后面多出的and或or
? ? ? ? prefix="":前綴,trim標(biāo)簽體中是整個字符串拼串后的結(jié)果
? ? ? ? ? ? ? ? prefix給拼串后的整個字符串加一個前綴
? ? ? ? prefixOverrides="":前綴覆蓋:去掉整個字符串前面多余的字符
? ? ? ? suffix="":后綴,給拼串后的整個字符串加一個后綴
? ? ? ? suffixOverrides="":后綴覆蓋,去掉整個字符串后面多余的字符
-->
<select id="queryUserByTrim" resultType="user">
? ? select * from easybuy_user
? ? <trim prefix="where" suffixOverrides="and">
? ? ? ? <if test="id != null">
? ? ? ? ? ? id = #{id} and
? ? ? ? </if>
? ? ? ? <if test="userName != null and userName!=""">
? ? ? ? ? ? userName like #{userName} and
? ? ? ? </if>
? ? ? ? <if test="email != null and email!=""">
? ? ? ? ? ? email = #{email} and
? ? ? ? </if>
? ? </trim>
</select>where標(biāo)簽的使用
<!--
? ? ? ? 查詢用戶:要求,攜帶哪些字段查詢條件就按這些字段進(jìn)行查詢
? ? ? ? 使用的OGNL,類似與EL表達(dá)式
? ? ? ? 從參數(shù)中取值進(jìn)行判斷,遇見特殊符號使用轉(zhuǎn)義字符
? ? ? ? 使用where標(biāo)簽時要注意,它只能去除第一個多出來的and或or
-->
<select id="queryUserByCondition" resultType="user">
? ? select * from easybuy_user
? ? <where>
? ? ? ? <if test="id != null">
? ? ? ? ? ? and id = #{id}
? ? ? ? </if>
? ? ? ? <if test="userName != null and userName!=""">
? ? ? ? ? ? and userName like #{userName}
? ? ? ? </if>
? ? ? ? <if test="email != null and email!=""">
? ? ? ? ? ? and email = #{email}
? ? ? ? </if>
? ? </where>
</select>總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用注解+RequestBodyAdvice實現(xiàn)http請求內(nèi)容加解密方式
這篇文章主要介紹了使用注解+RequestBodyAdvice實現(xiàn)http請求內(nèi)容加解密方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Java數(shù)據(jù)結(jié)構(gòu)與算法之單鏈表深入理解
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)與算法之單鏈表深入理解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
Spring監(jiān)聽器及定時任務(wù)實現(xiàn)方法詳解
這篇文章主要介紹了Spring監(jiān)聽器及定時任務(wù)實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
我總結(jié)的幾種@Transactional失效原因說明
這篇文章主要是我總結(jié)的幾種@Transactional失效原因說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Java中ArrayList在foreach里remove的問題詳析
這篇文章主要給大家介紹了關(guān)于Java中ArrayList在foreach里remove問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧2018-09-09

