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

mybatis中的動(dòng)態(tài)sql問題

 更新時(shí)間:2023年02月27日 14:13:28   作者:一入Java深似海丶  
這篇文章主要介紹了mybatis中的動(dòng)態(tài)sql問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Mybatis框架的動(dòng)態(tài)SQL技術(shù)是一種根據(jù)特定條件動(dòng)態(tài)拼裝SQL語句的功能,它存在的意義是通過標(biāo)簽解決拼接SQL語句字符串時(shí)的問題

1、if(常用)

if:根據(jù)標(biāo)簽中test屬性所對應(yīng)的表達(dá)式?jīng)Q定標(biāo)簽中的內(nèi)容是否需要拼接到SQL中

/**
     * 多條件查詢
     */
    List<Emp> getEmpByCondition(Emp emp);

當(dāng)empName為null和“ ”時(shí),會(huì)拼接and age,此時(shí)會(huì)報(bào)錯(cuò),可以1=1恒成立解決。

  • 用and表示&&(并且)
  • emp_name是字段名
  • if只有test標(biāo)簽且必須使用
<!--List<Emp> getEmpListByMoreTJ(Emp emp);-->
    <select id="getEmpListByMoreTJ" resultType="Emp">
select * from t_emp where 1=1
<if test="empName!= '' and empName!= null">
    and emp_name = #{empName}
</if>
<if test="age != '' and age != null">
    and age = #{age}
</if>
<if test="sex != '' and sex != null">
    and sex = #{sex}
</if>
</select>
@Test
    public void testGetEmpByCondition(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        List<Emp> list = mapper.getEmpByCondition(new Emp(null, "陳智", 33, "女", null));
        System.out.println(list);
    }
select eid,emp_name,age,sex,email from t_emp where emp_name = ? and age = ? or sex = ?

2、where

當(dāng)where標(biāo)簽中有內(nèi)容時(shí),會(huì)自動(dòng)生成where關(guān)鍵字,并且將內(nèi)容前多余的and或or去掉(在程序執(zhí)行后生成的sql會(huì)將內(nèi)容前多余的and或or去掉)

and寫在第二句是為了和上面拼接,寫在第一句是和下面拼接(即固定有elect * from t_emp emp_name= ?)

<select id="getEmpListByMoreTJ2" resultType="Emp">
    select * from t_emp
<where>
<if test="empName != '' and empName != null">
    emp_name = #{empName }
</if>
<if test="age != '' and age != null">
    and age = #{age}
</if>
<if test="sex != '' and sex != null">
    and sex = #{sex}
</if>
</where>
</select>

當(dāng)where標(biāo)簽中沒有內(nèi)容時(shí)(或者使用getEmpByCondition傳入的值全為null或“ ”),此時(shí)where標(biāo)簽沒有任何效果。則直接SQL語句為select * from t_emp

--------------------分割--------------------

where標(biāo)簽不能將其中內(nèi)容后面多余的and或or去掉:(會(huì)報(bào)錯(cuò))

語句為select * from t_emp emp_name= ? and

<select id="getEmpListByMoreTJ2" resultType="Emp">
    select * from t_emp
<where>
<if test="empName != '' and empName != null">
    emp_name = #{empName } and
</if>
<if test="age != '' and age != null">
    age = #{age} and
</if>
<if test="sex != '' and sex != null">
    sex = #{sex}
</if>
</where>
</select>

3、trim

where的增強(qiáng)

若標(biāo)簽中有內(nèi)容時(shí):

  • prefix|suffix:將trim標(biāo)簽中內(nèi)容前面或后面添加指定內(nèi)容
  • suffixOverrides|prefixOverrides:將trim標(biāo)簽中內(nèi)容前面或后面去掉指定內(nèi)容

若標(biāo)簽中沒有內(nèi)容時(shí),trim標(biāo)簽也沒有任何效果(跟上面的where一樣)

<!--List<Emp> getEmpByCondition(Emp emp);-->
    <select id="getEmpByCondition" resultType="Emp">
        select <include refid="empColumns"></include> from t_emp
        <trim prefix="where" suffixOverrides="and|or">
            <if test="empName != null and empName != ''">
                emp_name = #{empName} and
            </if>
            <if test="age != null and age != ''">
                age = #{age} or
            </if>
            <if test="sex != null and sex != ''">
                sex = #{sex} and
            </if>
            <if test="email != null and email != ''">
                email = #{email}
            </if>
        </trim>
    </select>

4.choose、when、otherwise

相當(dāng)于if...else if...else

/**
     * 測試choose、when、otherwise
     */
    List<Emp> getEmpByChoose(Emp emp);

when至少要有一個(gè),otherwise最多只能有一個(gè)

與第一點(diǎn)if的區(qū)別:choose只會(huì)滿足一個(gè)條件便退出,一個(gè)不滿足則執(zhí)行otherwise

<!--List<Emp> getEmpByChoose(Emp emp);-->
    <select id="getEmpByChoose" resultType="Emp">
        select * from t_emp
        <where>
            <choose>
                <when test="empName != null and empName != ''">
                    emp_name = #{empName}
                </when>
                <when test="age != null and age != ''">
                    age = #{age}
                </when>
                <when test="sex != null and sex != ''">
                    sex = #{sex}
                </when>
                <when test="email != null and email != ''">
                    email = #{email}
                </when>
                <otherwise>
                    did = 1
                </otherwise>
            </choose>
        </where>
    </select>
@Test
    public void testGetEmpByChoose(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        List<Emp> list = mapper.getEmpByChoose(new Emp(null, "", null, "", ""));
        System.out.println(list);
    }

5、foreach

  • collection:設(shè)置需要循環(huán)的數(shù)組或集合
  • item:表示數(shù)組或集合中的每一個(gè)數(shù)據(jù)
  • separator:循環(huán)體之間的分割符
  • open:foreach標(biāo)簽所循環(huán)的所有內(nèi)容的開始符
  • close:foreach標(biāo)簽所循環(huán)的所有內(nèi)容的結(jié)束符

5.1批量刪除

/**
     * 通過數(shù)組實(shí)現(xiàn)批量刪除
     */
    int deleteMoreByArray(@Param("eids") Integer[] eids);
<!--int deleteMoreByArray(@Param("eids") Integer[] eids);-->
    <delete id="deleteMoreByArray">
        delete from t_emp where
        <foreach collection="eids" item="eid" separator="or">
            eid = #{eid}
        </foreach>
        <!--
            delete from t_emp where eid in
            <foreach collection="eids" item="eid" separator="," open="(" close=")">
                #{eid}
            </foreach>
        -->
    </delete>
@Test
    public void testDeleteMoreByArray(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        int result = mapper.deleteMoreByArray(new Integer[]{12,13,14,15});
        System.out.println(result);
    }

5.2批量添加

/**
     * 通過list集合實(shí)現(xiàn)批量添加
     */
    int insertMoreByList(@Param("emps") List<Emp> emps);
<!--int insertMoreByList(@Param("emps") List<Emp> emps);-->
    <insert id="insertMoreByList">
        insert into t_emp values
        <foreach collection="emps" item="emp" separator=",">
            (null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
        </foreach>
    </insert>

Arrays.asList該方法是將數(shù)組轉(zhuǎn)化成List集合的方法 

如果你的List只是用來遍歷,就用Arrays.asList()。

如果你的List還要添加或刪除元素,還是乖乖地new一個(gè)java.util.ArrayList,然后一個(gè)一個(gè)的添加元素。

@Test
    public void testInsertMoreByList(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        Emp emp1 = new Emp(null,"a1",23,"男","123@qq.com");
        Emp emp2 = new Emp(null,"a2",23,"男","123@qq.com");
        Emp emp3 = new Emp(null,"a3",23,"男","123@qq.com");
        List<Emp> emps = Arrays.asList(emp1, emp2, emp3);
        System.out.println(mapper.insertMoreByList(emps));
    }

6、sql標(biāo)簽

設(shè)置SQL片段:<sql id="empColumns">eid,emp_name,age,sex,email</sql>

引用SQL片段:<include refid="empColumns"></include>

總結(jié)

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

相關(guān)文章

  • Java?超基礎(chǔ)講解String的使用

    Java?超基礎(chǔ)講解String的使用

    字符串廣泛應(yīng)用?在?Java?編程中,在?Java?中字符串屬于對象,Java?提供了?String?類來創(chuàng)建和操作字符串,讓我們一起來了解它
    2022-04-04
  • Java實(shí)現(xiàn)克隆的三種方式實(shí)例總結(jié)

    Java實(shí)現(xiàn)克隆的三種方式實(shí)例總結(jié)

    這篇文章主要介紹了Java實(shí)現(xiàn)克隆的三種方式,結(jié)合實(shí)例形式總結(jié)分析了java淺復(fù)制、深復(fù)制以及使用serializable實(shí)現(xiàn)深復(fù)制的相關(guān)操作技巧,需要的朋友可以參考下
    2018-08-08
  • 常用的Spring Boot調(diào)用外部接口方式實(shí)現(xiàn)數(shù)據(jù)交互

    常用的Spring Boot調(diào)用外部接口方式實(shí)現(xiàn)數(shù)據(jù)交互

    Spring Boot提供了多種調(diào)用外部接口的方式,可以方便地實(shí)現(xiàn)與其他系統(tǒng)的數(shù)據(jù)交互,提高系統(tǒng)的可擴(kuò)展性和數(shù)據(jù)共享能力,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-04-04
  • JAVA使用SimpleDateFormat類表示時(shí)間代碼實(shí)例

    JAVA使用SimpleDateFormat類表示時(shí)間代碼實(shí)例

    這篇文章主要介紹了JAVA使用SimpleDateFormat類表示時(shí)間代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 解決SpringBoot使用yaml作為配置文件遇到的坑

    解決SpringBoot使用yaml作為配置文件遇到的坑

    這篇文章主要介紹了解決SpringBoot使用yaml作為配置文件遇到的坑,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 基于java web獲取網(wǎng)頁訪問次數(shù)代碼實(shí)例

    基于java web獲取網(wǎng)頁訪問次數(shù)代碼實(shí)例

    這篇文章主要介紹了基于java web獲取網(wǎng)頁訪問次數(shù)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • ProtoStuff不支持BigDecimal序列化及反序列化詳解

    ProtoStuff不支持BigDecimal序列化及反序列化詳解

    這篇文章主要為大家介紹了ProtoStuff不支持BigDecimal序列化/反序列化,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Java 并發(fā)編程之ThreadLocal詳解及實(shí)例

    Java 并發(fā)編程之ThreadLocal詳解及實(shí)例

    這篇文章主要介紹了Java 并發(fā)編程之ThreadLocal詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • 詳解WebSocket+spring示例demo(已使用sockJs庫)

    詳解WebSocket+spring示例demo(已使用sockJs庫)

    本篇文章主要介紹了WebSocket spring示例demo(已使用sockJs庫),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • 關(guān)于Javaweb的轉(zhuǎn)發(fā)和重定向詳解

    關(guān)于Javaweb的轉(zhuǎn)發(fā)和重定向詳解

    這篇文章主要介紹了關(guān)于Javaweb的轉(zhuǎn)發(fā)和重定向詳解,請求的轉(zhuǎn)發(fā),是指服務(wù)器收到請求后,從一個(gè)服務(wù)器端資源跳轉(zhuǎn)到同一個(gè)服務(wù)器端另外一個(gè)資源的操作,需要的朋友可以參考下
    2023-05-05

最新評論

安陆市| 许昌市| 天津市| 蓬安县| 宝应县| 禄丰县| 噶尔县| 囊谦县| 易门县| 白银市| 政和县| 凌源市| 莎车县| 万山特区| 青浦区| 绍兴县| 高雄县| 惠东县| 湖北省| 象州县| 呼和浩特市| 分宜县| 六盘水市| 保康县| 白银市| 高密市| 九龙县| 铜山县| 江津市| 迁安市| 七台河市| 隆化县| 大理市| 永靖县| 赞皇县| 呼伦贝尔市| 清河县| 七台河市| 霍邱县| 和龙市| 景谷|