Spring+SpringMVC+MyBatis深入學習及搭建(五)——動態sql


轉載請注明出處:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 

前面有講到Spring+SpringMVC+MyBatis深入學習及搭建(四)——MyBatis輸入映射與輸出映射

mybatis核心:對sql語句進行靈活操作,通過表達式進行判斷,對sql進行靈活拼接、組裝。

mybatis提供各種標簽方法實現動態拼接sql。

1. if&where

1.2 需求

用戶信息綜合查詢列表和用戶信息查詢列表總數這兩個statement的定義使用動態sql。

對查詢條件進行判斷,如果輸入參數不為空才進行查詢條件拼接。

1.3 mapper.xml

    <select id="findUserList" parameterType="joanna.yan.mybatis.entity.UserQueryVo" resultType="joanna.yan.mybatis.entity.UserCustom">
        SELECT * FROM USER
        <!--where可以自動去掉條件中的第一個and  -->
        <where>
            <if test="userCustom!=null">
                <if test="userCustom.sex!=null and userCustom.sex!=''">
                    and user.sex=#{userCustom.sex}
                </if>
                <if test="userCustom.username!=null and userCustom.username!=''">
                    and user.username LIKE '%${userCustom.username}%'
                </if>
            </if>
        </where>
    </select>

<select id="findUserCount" parameterType="joanna.yan.mybatis.entity.UserQueryVo" resultType="int"> SELECT count(*) FROM USER <!--where可以自動去掉條件中的第一個and --> <where> <if test="userCustom!=null"> <if test="userCustom.sex!=null and userCustom.sex!=''"> and user.sex=#{userCustom.sex} </if> <if test="userCustom.username!=null and userCustom.username!=''"> and user.username LIKE '%${userCustom.username}%' </if> </if> </where> </select>

1.4測試代碼

    @Test
    public void findUserListTest() throws Exception{
        SqlSession sqlSession=sqlSessionFactory.openSession();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        //創建包裝對象,設置查詢條件
        UserQueryVo userQueryVo=new UserQueryVo();
        UserCustom userCustom=new UserCustom();
        //由於這里使用動態sql,如果不設置某個值,條件不會拼接在sql中
//        userCustom.setSex("1");
        userCustom.setUsername("張三豐");
        userQueryVo.setUserCustom(userCustom);
        List<UserCustom> list=userMapper.findUserList(userQueryVo);
        System.out.println(list);
    }

打印的sql:如果不設置sex的值,條件不會拼接在sql中

2.sql片段

2.1 需求

將上邊實現的動態sql判斷代碼塊抽取出來,組成一個sql片段。其它的statement中就可以引用sql片段。方便程序員進行開發。

2.2 定義sql片段

    <!--定義sql片段
        id:sql片段的唯一標識
        
        經驗:1.是基於單表來定義sql片段的,這樣的話這個sql片段可重用性才高
            2.在sql片段中不要包括where
      -->
    <sql id="query_user_where">
        <if test="userCustom!=null">
            <if test="userCustom.sex!=null and userCustom.sex!=''">
                and user.sex=#{userCustom.sex}
            </if>
            <if test="userCustom.username!=null and userCustom.username!=''">
                and user.username LIKE '%${userCustom.username}%'
            </if>
        </if>
    </sql>

2.3 引用sql片段

在mapper.xml中定義statement中引用sql片段:

    <select id="findUserList" parameterType="joanna.yan.mybatis.entity.UserQueryVo" resultType="joanna.yan.mybatis.entity.UserCustom">
        SELECT * FROM USER
        <!--where可以自動去掉條件中的第一個and  -->
        <where>
            <!--引用sql片段的id,如果refid指定的id不在本mapper文件中,需要在前邊加namespace  -->
            <include refid="query_user_where"></include>
            <!--在這里還可以引用其它的sql片段  -->
        </where>
    </select>

<select id="findUserCount" parameterType="joanna.yan.mybatis.entity.UserQueryVo" resultType="int"> SELECT count(*) FROM USER <!--where可以自動去掉條件中的第一個and --> <where> <!--引用sql片段的id,如果refid指定的id不在本mapper文件中,需要在前邊加namespace --> <include refid="query_user_where"></include> <!--在這里還可以引用其它的sql片段 --> </where> </select>

3. foreach

向sql傳遞數組或List,mybatis使用foreach解析。

3.1 需求

在用戶查詢列表和查詢總數的statement中增加多個id輸入查詢。

sql語句如下,兩種方法:

SELECT * FROM USER WHERE id=1 OR id=10 OR id=16

SELECT * FROM USER WHERE id IN(1,10,16)

3.2 在輸入參數類型中添加List<Integer> ids傳入多個id

3.3 修改mapper.xml

WHERE id=1 OR id=10 OR id=16

在前面的查詢條件中,查詢條件定義成了一個sql片段,現在我們需要修改sql片段。

    <!--定義sql片段
        id:sql片段的唯一標識
        
        經驗:1.是基於單表來定義sql片段的,這樣的話這個sql片段可重用性才高
            2.在sql片段中不要包括where
      -->
    <sql id="query_user_where">
        <if test="userCustom!=null">
            <if test="userCustom.sex!=null and userCustom.sex!=''">
                and user.sex=#{userCustom.sex}
            </if>
            <if test="userCustom.username!=null and userCustom.username!=''">
                and user.username LIKE '%${userCustom.username}%'
            </if>
            <if test="ids!=null">
                <!--使用foreach遍歷傳入的ids
                    collection:指定輸入對象中集合屬性
                    item:每個遍歷生成的對象名
                    open:開始遍歷時拼接的串
                    close:結束遍歷時拼接的串
                    separator:遍歷的兩個對象中需要拼接的串
                 -->
                 <!--是要實現下邊的sql拼接:
                     AND (id=1 OR id=10 OR id=16)
                   -->
                <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
                    <!--每個遍歷需要拼接的串  -->
                    id=#{user_id}
                </foreach>
            </if>
        </if>
    </sql>

3.4 測試代碼

    @Test
    public void findUserListTest() throws Exception{
        SqlSession sqlSession=sqlSessionFactory.openSession();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        //創建包裝對象,設置查詢條件
        UserQueryVo userQueryVo=new UserQueryVo();
        UserCustom userCustom=new UserCustom();
        //由於這里使用動態sql,如果不設置某個值,條件不會拼接在sql中
//        userCustom.setSex("1");
        userCustom.setUsername("小明");
        //傳入多個id
        List<Integer> ids=new ArrayList<>();
        ids.add(1);
        ids.add(10);
        ids.add(16);
        userQueryVo.setIds(ids);
        userQueryVo.setUserCustom(userCustom);
        List<UserCustom> list=userMapper.findUserList(userQueryVo);
        System.out.println(list);
    }

 如果此文對您有幫助,微信打賞我一下吧~ 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM