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

來源:互聯網
上載者:User

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

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

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

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

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

1. if&where1.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);    }

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.