MyBatis常用動態(tài)sql大總結
簡介
相信大家沒用Mybatis之前,都碰到過各種條件判斷拼接SQL、需要去掉多余的逗號等痛苦,Mybatis中的動態(tài)SQL能很好的解決上面說的情況,可以很靈活的組裝SQL語句,從而提高開發(fā)效率。
1、SQL的動態(tài)拼接有哪些
- if標簽
- where標簽
- choose when otherwise標簽
- set標簽
- trim標簽
- bind標簽
- sql和include標簽 foreach標簽
2、if標簽:
test中寫判斷條件 參數(shù)直接paramN或者別名 特點: 只要成立就拼接在Sql語句中,都成立就全部都拼接 注意: where子句中加上1=1來規(guī)避and的風險
<select id="uid" resultType="UserEntity"> select * from UserEntity where 1=1 <if test="param1!=null and param1!=''"> and outno=#{param1} </if> <if test="param2!=null and param2!=''"> and inno=#{param2} </if> </select>
條件模糊查以及查詢時間段內(nèi)數(shù)據(jù)
<select id="uid" resultType="UserEntity"> select * from UserEntity where 1=1 <if test="param1!=null and param1!=''"> and outno=#{param1} </if> <if test="param2!=null and param2!=''">and inno LIKE CONCAT('%',#{param2 },'%' ) </if> <if test="effectiveTime !=null and effectiveTime !=''"> and begin_time <= #{effectiveTime} //effectiveTime 是封的查詢條件類的查詢字段 and end_time >= #{effectiveTime} //begin_time,end_time 對應數(shù)據(jù)庫字段。 </if> </select>
3、where標簽:
特點:
會自動地給Sql語句添加where關鍵字,并將第一個and去除。
<select id="uid" resultType="UserEntity"> select * from UserEntity <where> <if test="param1!=null and param1!=''"> and outno=#{param1} </if> <if test="param2!=null and param2!=''"> and inno=#{param2} </if> </where> </select>
4、choose when otherwise標簽
特點:
條件只要有一個成立,其他的就不會再判斷了。
如果沒有成立的條件則默認執(zhí)行otherwise中的內(nèi)容
<select id="uid" resultType="UserEntity"> select * from UserEntity <where> <choose> <when test="param1!=null and param1!=''"> and outno=#{param1} </when> <when test="param2!=null and param2!=''"> and inno=#{param2} </when> <otherwise> and 1=1 </otherwise> </choose> </where> </select>
5、set標簽:
產(chǎn)生一個set關鍵字,自動去除最后一個逗號。
注意:
在判斷條件中最后保持有一個永遠成立的條件。避免sql錯誤。
<update id="uid"> update accountTable <set> <if test="aname!=null and aname!=''"> aname=#{aname}, </if> <if test="money !=null and money !=''"> money=#{money}, </if> <if test="ano !=null and ano !=''"> ano=#{ano}, </if> </set> where ano=#{ano} </update>
6、trim標簽:
prefix:在trim的內(nèi)容前添加指定的內(nèi)容
prefixOverrides在trim的內(nèi)容前去除指定的內(nèi)容
suffix:在trim的內(nèi)容后添加指定的內(nèi)容
suffixOverrides:在trim的內(nèi)容后去除指定的內(nèi)容
注意:
先去除后添加
添加內(nèi)容會默認添加一個空格。
<update id="upT" parameterType="account"> update account <trim prefix="$" prefixOverrides="" suffix="" suffixOverrides=""> <if test="ano !=null and ano !=''"> ano=#{ano}, </if> <if test="aname!=null and aname!=''"> aname=#{aname}, </if> <if test="money !=null and money !=''"> money=#{money}, </if> </trim> where ano=#{ano} </update>
7、bind標簽:
name:參數(shù)名
value:表達式,注意字符串拼接按照變量方式進行拼接
例如:
<bind name="money" value="'$'+money"/>
給參數(shù)重新賦值
<update id="upB" parameterType="account"> <bind name="money" value="money+100"/> update account <trim prefix="set" suffixOverrides=","> <if test="ano !=null and ano !=''"> ano=#{ano}, </if> <if test="aname!=null and aname!=''"> aname=#{aname}, </if> <if test="money !=null and money !=''"> money=#{money}, </if> </trim> where ano=#{ano} </update>
8、sql和include標簽:
sql標簽:在外部聲明公用SQL語句
id
include標簽:引入聲明的公共SQL語句
refid:
優(yōu)點:便于SQL的整體修改
缺點:難于閱讀
<select id="selA" resultType="account"> select <include refid="mysql"></include> from account </select> <sql id="mysql"> ano,aname,apwd,money </sql>
9、foreach標簽:
構造IN條件語句時需要對集合進行遍歷,這時候可以使用foreach元素。foreach會去掉多余","。若集合為空,則不會執(zhí)行foreach元素中的操作,但此時會多出"in"關鍵字,報錯。
item:集合中的元素
index:元素所在集合的下標,迭代map時,index是鍵
collection:集合的類型,可選值list,array,除此之外還可以是@Param("name")、Map中的key、類的成員變量 open、
close:在首、尾拼接的字符
separator:每個元素間的分隔符
注: foreach標簽支持List、Set、Map、Array等的遍歷
迭代數(shù)組或者List、Set集合時,index是迭代次數(shù),item是本次迭代獲取的元素;
迭代Map(或Map.Entry對象的集合)時,index是鍵,item是值
collection屬性介紹
傳入單參數(shù)且是List時,collection="list"
傳入單參數(shù)且是Array時,collection="array"
傳入單參數(shù)且是Set時,需使用@Param注解,同下
傳入單參數(shù)且使用@Param("name")時,collection="name",即和@Param注解的value屬性值相同,此時list、array無效
傳入多參數(shù)且封裝成Map時,如map.put("ids", Arrays.asList(1, 2)),此時collection="ids"
傳入多參數(shù)且封裝成類時,如User類中有成員變量List roleIds,此時collection="roleIds";若User類中有成員變量Role role,Role類中有成員變量prilIds,此時collection="role.prilIds"
<select id="selF" parameterType="list" resultType="account"> select * from account where ano in <foreach collection="list" item="item" open="(" separator="," close=")"> #{item} </foreach> </select> <insert id="inF"> insert into log values <foreach collection="list" item="log" separator=","> (#{log.outno},#{log.inno},#{log.money}) </foreach> </insert>
10、示例
批量修改的動態(tài)sql
controller層
/** * 批量修改 */ @PutMapping("/aaa") @ApiOperation(value = "下架") public R aaa(@RequestBody Asa asa) { this.goodTestService.updateState(asa.getIds(),asa.getMsg()); return R.data("cg"); }
service
package com.troy.testa.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.troy.testa.dto.GoodTestDTO; import com.troy.testa.entity.GoodTest; import org.springframework.data.domain.Pageable; import java.util.List; /** * (GoodTest)表服務接口 * @author zhangh * @date 2021-03-21 15:31:34 */ public interface GoodTestService extends IService<GoodTest> { void updateState(String[] ids,String msg); }
serviceImp
@Override public void updateState(String[] ids,String msg) { baseMapper.updateState(ids,msg); }
dao
package com.troy.testa.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.troy.testa.dto.GoodTestDTO; import com.troy.testa.entity.GoodTest; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import com.baomidou.mybatisplus.core.metadata.IPage; import java.util.List; /** * (GoodTest)表數(shù)據(jù)庫訪問層 * @author zhangh * @date 2021-03-21 15:31:34 */ @Mapper public interface GoodTestDao extends BaseMapper<GoodTest> { void updateState(@Param("ids") String[] ids,@Param("msg") String msg); }
xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.troy.testa.dao.GoodTestDao"> <update id="updateState"> <foreach collection="ids" item="item" index="index" separator=";"> UPDATE good_test SET good_state = 1,good_name=#{msg} WHERE id = #{item} </foreach> </update>
postman測試
表已批量修改
成功!
雖然可以用程序循環(huán)實現(xiàn),但是用了動態(tài)sql少去了不少工作量。
總結:
發(fā)中,經(jīng)常需要根據(jù)不同的條件動態(tài)拼接SQL,并且還確保空格、列名最后的逗號、多余的AND、OR條件等。在MyBatis中處理這種情況是比較方便容易的。
到此這篇關于MyBatis常用動態(tài)sql的文章就介紹到這了,更多相關MyBatis動態(tài)sql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java根據(jù)實體生成SQL數(shù)據(jù)庫表的示例代碼
這篇文章主要來和大家分享一個Java實現(xiàn)根據(jù)實體生成SQL數(shù)據(jù)庫表的代碼,文中的實現(xiàn)代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-07-07IDEA2023創(chuàng)建MavenWeb項目并搭建Servlet工程的全過程
Maven提供了大量不同類型的Archetype模板,通過它們可以幫助用戶快速的創(chuàng)建Java項目,這篇文章主要給大家介紹了關于IDEA2023創(chuàng)建MavenWeb項目并搭建Servlet工程的相關資料,需要的朋友可以參考下2023-10-10logback?EvaluatorFilter日志過濾器源碼解讀
這篇文章主要為大家介紹了logback?EvaluatorFilter日志過濾器源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11關于Idea卡在Resolving Maven dependencies的解決方案
本文詳細介紹了關于Idea卡在Resolving Maven dependencies的解決方案,文中通過圖文結合的形式給大家介紹的非常詳細,對大家解決問題有一定的幫助,需要的朋友可以參考下2024-02-02