欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis在注解上如何實(shí)現(xiàn)動(dòng)態(tài)SQL

 更新時(shí)間:2022年06月13日 11:57:20   作者:k55  
這篇文章主要介紹了Mybatis在注解上如何實(shí)現(xiàn)動(dòng)態(tài)SQL,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

在注解上實(shí)現(xiàn)動(dòng)態(tài)SQL

使用Mybatis注解實(shí)現(xiàn)sql語(yǔ)句,但是有些時(shí)候有些字段是空的,這時(shí)候這個(gè)空的字段就要從條件查詢(xún)語(yǔ)句中刪除,這個(gè)時(shí)候就需要用到動(dòng)態(tài)Sql。

注解的動(dòng)態(tài)語(yǔ)句支持以下

  • trim
  • where
  • set
  • foreach
  • if
  • choose
  • when
  • otherwise
  • bind
@Select({"<script> " +
        "select * from t_user " +
        "where  1=1 " +
        "<if test='userId!=null'> and id = #{userId}</if> " +
        "</script>"})

要加上標(biāo)簽就可以實(shí)現(xiàn)條件判斷

但是在無(wú)法使用大于號(hào) 、小于號(hào),那如何解決這問(wèn)題呢,其實(shí)只要把大于號(hào)、小于號(hào)轉(zhuǎn)義即可

注解方式動(dòng)態(tài)sql寫(xiě)法和注意事項(xiàng)

@Select({"<script>" +
            "select * from tb_examine" +
            "<where> 1 = 1" +
            "<if test = \" employeeId != null and  employeeId != '' \"> AND employee_id = #{employeeId} </if>" +
            "<if test = \" gradeId != null and gradeId != '' \"> AND grade_id = #{gradeId} </if>" +
            "<if test = \" year != null and year != '' \"> AND year like #{year} </if>" +
            "<if test = \" (statrMonth != null and statrMonth != '') and (endMonth == null or endMonth == '') \"> AND month &gt;= #{statrMonth} </if>" +
            "<if test = \" (statrMonth == null or  statrMonth == '') and (endMonth != null and endMonth != '')   \"> AND month &lt;= #{endMonth} </if>" +
            "<if test = \" (statrMonth != null and statrMonth != '') and (endMonth != null and endMonth != '')   \">AND month &gt;= #{statrMonth} AND month &lt;= #{endMonth}  </if>" +
            "</where>" +
            "</script>"})
public List<Examine> getName(Examine examine);

判斷字符串為空串 用單引號(hào)

大于等于用

小于等于用

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論