• Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql(转发同上)


    原地址:http://www.cnblogs.com/shanheyongmu/p/7121807.html

    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);
        }
    复制代码

    转载自 http://www.cnblogs.com/Joanna-Yan/p/6908763.html

    早年同窗始相知,三载瞬逝情却萌。年少不知愁滋味,犹读红豆生南国。别离方知相思苦,心田红豆根以生。
  • 相关阅读:
    oracle查询表最后的操作时间
    设置tomcat开机自启
    jmeter 连接mysql
    ubuntu卸载软件
    转 ubuntu 安装chrome 和chromedriver
    转 ps -ef ps -aux 区别
    ubuntu 20 jenkins 开机启动
    Ubuntu20.04安装JDK
    ubuntu 安装指定版本gitlab
    Gitlab备份和恢复操作记录 转
  • 原文地址:https://www.cnblogs.com/ycmxm/p/7125580.html
Copyright © 2020-2023  润新知