您的位置:首页 > 其它

Mybatis中的三个标签<where> <set> <trim>

2017-02-15 16:29 561 查看


一。<where>标签

1.复制上文工程,重命名为Mybatis08工程,工程结构图如下:

2.修改UserInfoMapper.xml,具体内容如下:

[html] view
plain copy

 





<select id="findUserInfoByUnoQuantity" parameterType="Map"  

        resultMap="UserInfoResult">  

        select * from userinfo  

        <where>  

            <if test="department!=null">  

            <span style="white-space:pre">    </span>department like #{department}  

            </if>  

            <if test="gender!=null">  

                AND gender=#{gender}  

            </if>  

            <if test="position!=null">  

                AND position like #{position}  

            </if>  

        </where>  

    </select>  

【解释】

a.select之后没有直接写Sql语句的where,而是使用<where>标签

b.按照标准写法,第一个<if>标签内的AND应该不写,但是,就算开发中书写也不会报错。这就是where标签帮助我们自动的移除了第一个AND链接。但是,第二个之后的<if>标签内,必须有AND链接。

c.如果没有一个条件符合,则返回所有条目。

d.<if>标签的其他用法请参考前文,这里不再赘述

3.修改单元测试方法,如下:

[java] view
plain copy

 





@Test  

    public void testSeletOne() {  

        try {  

  

            Map<String, Object> map = new HashMap<String, Object>();  

            map.put("department", "1");  

            map.put("gender", "1");  

            map.put("position", "工程师");  

            Departments d = new Departments("2", "%售%");  

            map.put("d", d);  

            UserInfoDao userInfo = sqlSession.getMapper(UserInfoDao.class);  

            List<UserInfo> UIList = userInfo.findUserInfoByUnoQuantity(map);  

            for (UserInfo ui : UIList) {  

                System.out.println(ui.toString());  

            }  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

    }  

4.运行单元测试方法,观察输出即可。

5.结论:where 元素知道只有在一个以上的<if>条件有值的情况下才去插入“WHERE”子句。而且,若内容是“AND”或“OR”开头的,where 元素也知道如何将他们去除。

--------------------------------------------------------------------------------------------------------------------------------------------------------

二.<trim>标签

1.该标签的功能与<where>类似,并且额外的提供了前缀后缀功能。具体用法如下:

2.修改Mapper文件,具体内容如下:

[html] view
plain copy

 





<select id="findUserInfoByTrim" parameterType="Map"  

        resultMap="UserInfoResult">  

        select * from userinfo  

        <trim prefix="where" prefixOverrides="and|or">  

            <if test="department!=null">  

                AND department like #{department}  

            </if>  

            <if test="gender!=null">  

                AND gender=#{gender}  

            </if>  

            <if test="position!=null">  

                AND position like #{position}  

            </if>  

        </trim>  

    </select>  

【解释】

a.我们使用<trim>替代<where>标签。

b.属性“prefix”表示:加入前缀where

c.属性“prefixOverrides”表示:自动覆盖第一个“and”或者“or”

d.后缀的用法类似;
3.增加对应接口,修改单元测试方法调用接口,如下:

[java] view
plain copy

 





@Test  

    public void testSeletOne() {  

        try {  

  

            Map<String, Object> map = new HashMap<String, Object>();  

            map.put("department", "1");  

            map.put("gender", "1");  

            map.put("position", "工程师");  

            Departments d = new Departments("2", "%售%");  

            map.put("d", d);  

            UserInfoDao userInfo = sqlSession.getMapper(UserInfoDao.class);  

            List<UserInfo> UIList = userInfo.findUserInfoByTrim(map);  

            for (UserInfo ui : UIList) {  

                System.out.println(ui.toString());  

            }  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

    }  

4.运行单元测试方法,观察控制台输出即可。

-------------------------------------------------------------------------------------------------------------------------------------

三。<set>标签

1.注意:此标签用于update语句。请各位看官注意观察书写方法。

2.修改Mapper文件,具体内容如下:

[html] view
plain copy

 





<update id="updateUserInfoBySet" parameterType="userInfo">  

        update userInfo  

        <set>  

            <if test="mobile!=null">  

                mobile=#{mobile},  

            </if>  

            <if test="gender!=null">  

                gender=#{gender},  

            </if>  

            <if test="position!=null">  

                position = #{position},  

            </if>  

        </set>  

        where userid=#{userid}  

    </update>  

【解释】

a.SQL语句的set被<set>标签替代。

b.每个<if>中语句最后都带有逗号,如果有写过SQL语句的同学就一定知道,最后的逗号是不能有的,因此,这里的<set>标签能够帮助我们自动的移除最后一个<if>中的逗号。

c.<trim>是一个非常强大的标签,因此,我们也可以通过<trim>来实现<set>的功能,如下:【这种写法的运行效果与<set>等价】

[html] view
plain copy

 





<update id="updateUserInfoBySet" parameterType="userInfo">  

        update userInfo  

        <trim prefix="SET" suffixOverrides=",">  

            <if test="mobile!=null">  

                mobile=#{mobile},  

            </if>  

            <if test="gender!=null">  

                gender=#{gender},  

            </if>  

            <if test="position!=null">  

                position = #{position},  

            </if>  

        </trim>  

        where userid=#{userid}  

    </update>  

3.增加对应接口,修改单元测试方法调用接口,如下:

[java] view
plain copy

 





@Test  

    public void testUpdate() {  

        try {  

  

            UserInfo ui = new UserInfo("admin", "3", "经理", "77778888","0", "admin@email.com", null);  

            UserInfoDao userInfo = sqlSession.getMapper(UserInfoDao.class);  

            int re = userInfo.updateUserInfoBySet(ui);  

            if(re==1){  

                System.out.println("更新成功");  

            }  

            sqlSession.commit();  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

    }  

4.运行单元测试方法,观察控制台输出即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: