最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis的mapper特殊字符轉(zhuǎn)移及動(dòng)態(tài)SQL條件查詢小結(jié)

 更新時(shí)間:2021年09月24日 16:09:39   作者:kenx  
mybatis mapper文件中條件查詢符,如>=,<,之類是不能直接寫的會(huì)報(bào)錯(cuò)的需要轉(zhuǎn)移一下,本文給大家介紹了常見(jiàn)的條件查詢操作,對(duì)mybatis的mapper特殊字符及動(dòng)態(tài)SQL條件查詢相關(guān)知識(shí)感興趣的朋友一起看看吧

前言

我們知道在項(xiàng)目開發(fā)中之前使用數(shù)據(jù)庫(kù)查詢,都是基于jdbc,進(jìn)行連接查詢,然后是高級(jí)一點(diǎn)jdbcTemplate進(jìn)行查詢,但是我們發(fā)現(xiàn)還是不是很方便,有大量重復(fù)sql語(yǔ)句,與代碼偶合,效率低下,于是就衍生出來(lái)ORM框架,如Mybatis,Hibernate,還有SpringBoot的,Spring Data JPA

條件查詢

我們知道在mybatis mapper文件中條件查詢符,如>=,<,之類是不能直接寫的會(huì)報(bào)錯(cuò)的需要轉(zhuǎn)移一下 如下圖表

詳細(xì)內(nèi)容參考

常見(jiàn)的條件查詢操作有

我們通過(guò)mybatis 提供的特有標(biāo)簽進(jìn)行條件判斷,達(dá)到動(dòng)態(tài)拼接sql語(yǔ)句

if標(biāo)簽 where標(biāo)簽 choose when otherwise標(biāo)簽 foreach標(biāo)簽

快速入門

if標(biāo)簽

語(yǔ)法:

<if test="xxx != null and xxx != ''">

test中寫判斷條件 參數(shù)直接paramN或者別名 多個(gè)條件使用and或者or連接

只要條件成立就拼接在Sql語(yǔ)句中,都成立就全部都拼接

注意where子句中加上1=1來(lái)規(guī)避and的風(fēng)險(xiǎn)

如下例子:

<select id="selg" resultType="log">
    select * from log where 1=1
    <if test="param1!=null and param1!=''">
    and outno=#{param1}
    </if>
    <if test="param2!=null and param2!=''">
    and inno=#{param2}
    </if>
</select>

where標(biāo)簽

對(duì)上面if標(biāo)簽條件判斷where連接做了處理會(huì)自動(dòng)的給Sql語(yǔ)句添加where關(guān)鍵字,并將第一個(gè)and去除

上面sql可以改造成如下:

<select id="selg" resultType="log">
      select * from log 
  <where>
       <if test="param1!=null and param1!=''">
      and outno=#{param1}
      </if>
      <if test="param2!=null and param2!=''">
      and inno=#{param2}
      </if>
  </where>
     
</select>

choose when otherwise標(biāo)簽

類似于Java語(yǔ)法中的,case,switch語(yǔ)句判斷

條件只要有一個(gè)成立,其他的就不會(huì)再判斷了。如果沒(méi)有成立的條件則默認(rèn)執(zhí)行otherwise中的內(nèi)容

上面sql可以改造成如下:

<select id="selg" resultType="log">
      select * from log 
  <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>

foreach標(biāo)簽

語(yǔ)法:

 <foreach collection="idList" item="id" open="(" separator="," close=")">
</foreach>

  • collection:要遍歷的集合對(duì)象
  • item:記錄每次遍歷的結(jié)果
  • open:在結(jié)果的左邊添加內(nèi)容
  • separator:結(jié)果和結(jié)果之間的內(nèi)容
  • close:在最后添加的內(nèi)容

常用于in查詢,和批量插入操作 如下案例:

<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="insertBatch">
        INSERT INTO t_user
        (id, name, password)
        VALUES
        <foreach collection ="userList" item="user" separator =",">
            (#{user.id}, #{user.name}, #{user.password})
        </foreach >
    </insert>

其他標(biāo)簽使用參考點(diǎn)擊進(jìn)入·

場(chǎng)景案例

1.當(dāng)我們需要對(duì)多張表的關(guān)聯(lián)數(shù)據(jù)進(jìn)行復(fù)雜動(dòng)態(tài)條件查詢的時(shí)候,就需要用到 if標(biāo)簽進(jìn)行判斷 如下

根據(jù)用戶手機(jī)號(hào)姓名年齡性別,等進(jìn)行動(dòng)態(tài)條件檢索,這個(gè)時(shí)候我們需要?jiǎng)討B(tài)通過(guò)調(diào)節(jié)去拼接sql 當(dāng)條件滿足sql語(yǔ)句加上對(duì)應(yīng)條件差許

<select id="findUsersByUser" resultType="cn.soboys.kmall.sys.entity.User">
        select tu.USER_ID,tu.USERNAME,tu.SSEX,td.DEPT_NAME,tu.MOBILE,tu.EMAIL,tu.STATUS,tu.CREATE_TIME,
        td.DEPT_ID
        from t_user tu left join t_dept td on tu.DEPT_ID = td.DEPT_ID
        where tu.ADMIN_TYPE_ID  &gt;= 0 AND tu.ADMIN_TYPE_ID  &lt;= #{userParams.adminType}
        <if test="userParams.roleId != null and userParams.roleId != ''">
           and (select group_concat(ur.ROLE_ID)
            from t_user u
            right join t_user_role ur on ur.USER_ID = u.USER_ID,
            t_role r
            where r.ROLE_ID = ur.ROLE_ID
            and u.USER_ID = tu.USER_ID and r.ROLE_ID=#{userParams.roleId})
        </if>


        <if test="userParams.mobile != null and userParams.mobile != ''">
            AND tu.MOBILE =#{userParams.mobile}
        </if>
        <if test="userParams.username != null and userParams.username != ''">
            AND tu.USERNAME   like CONCAT('%',#{userParams.username},'%')
        </if>
        <if test="userParams.ssex != null and userParams.ssex != ''">
            AND tu.SSEX  =#{userParams.ssex}
        </if>
        <if test="userParams.status != null and userParams.status != ''">
            AND tu.STATUS =#{userParams.status}
        </if>
        <if test="userParams.deptId != null and userParams.deptId != ''">
            AND td.DEPT_ID =#{userParams.deptId}
        </if>
        <if test="userParams.createTime != null and userParams.createTime != ''">
            AND DATE_FORMAT(tu.CREATE_TIME,'%Y%m%d') BETWEEN substring_index(#{userParams.createTime},'#',1) and substring_index(#{userParams.createTime},'#',-1)
        </if>
    </select>

對(duì)應(yīng)mapper對(duì)應(yīng)的方法

<T> IPage<User> findUsersByUser(Page<T> page, @Param("userParams") SearchUserParams userParams);

對(duì)應(yīng)參數(shù)實(shí)體對(duì)象

@Data
public class SearchUserParams {
    private String username;
    private String mobile;
    private String status;
    private String ssex;
    private Long deptId;
    private String createTime;
    private long adminType;
    private String roleId;
}

通過(guò)if標(biāo)簽去判斷條件是否滿足,滿足就拼接對(duì)應(yīng)sql

注意在上面我們提到的條件拼接第一個(gè)是where連接,而不是and應(yīng)規(guī)避and風(fēng)險(xiǎn)保證sql語(yǔ)法正確 如下

<select id="findSearchCouponsPage" parameterType="cn.soboys.kmall.bean.web.params.SearchCouponParams" resultType="coupon">
        select *
        from coupon c
        left join user_coupon uc on c.coupon_id = uc.coupon_id
        WHERE 1 = 1
        <if test="couponParams.userId != null and couponParams.userId != ''">
           and uc.user_id =#{couponParams.userId}
        </if>
        <if test="couponParams.status != null and couponParams.status != ''">
            and c.status =#{couponParams.status}
        </if>
        <if test="couponParams.couponId != null and couponParams.couponId != ''">
            and c.coupon_id =#{couponParams.couponId}
        </if>
        <if test="couponParams.couponType != null and couponParams.couponType != ''">
            and c.type =#{couponParams.couponType}
        </if>
    </select>

我們可以通過(guò)假定給他一個(gè)默認(rèn)條件 WHERE 1 = 1來(lái)解決,也可以通過(guò)嵌套where標(biāo)簽來(lái)解決

到此這篇關(guān)于mybatis的mapper特殊字符轉(zhuǎn)移及動(dòng)態(tài)SQL條件查詢的文章就介紹到這了,更多相關(guān)mybatis的mapper特殊字符轉(zhuǎn)移內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

格尔木市| 平舆县| 老河口市| 峡江县| 郁南县| 高要市| 泉州市| 来宾市| 伊金霍洛旗| 新田县| 靖远县| 鄂托克前旗| 丰顺县| 凉山| 辽阳县| 镇赉县| 普宁市| 潼南县| 镇坪县| 西畴县| 广安市| 孝感市| 洞头县| 泸州市| 武宣县| 桦甸市| 宿州市| 桦川县| 吉木乃县| 长子县| 新疆| 乐平市| 河津市| 伊宁县| 佛冈县| 和顺县| 汤原县| 伊春市| 海安县| 九龙坡区| 鄂伦春自治旗|