mybatis学习之动态SQL

概念

 mybatis核心就是对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活的拼接和组装


需求

用户信息的综合查询列表和用户信息查询的列表总数这两个statement使用动态sql

对查询条件进行判断,如果输入参数不为空,才进行查询条件拼接

where 与 if


mapper.xml:
    <!--
    用户信息的综合查询
    #{userCustom.sex} : 取出pojo包装对象中性别的值
    ${userCustom.username} : 取出pojo包装对象中用户名的值
     -->
    <select id="findUserList" parameterType="UserQueryVo"  resultType="UserCustom" >
        select * from user
        <!--
         where 可以自动去掉条件中的第一个and
         -->
        <where>
            <if test="UserCustom != null">
                <if test="UserCustom.sex != null and UserCustom.sex != '' ">
                   and user.sex = #{userCustom.sex} and
                </if>
                <if test="UserCustom.username != null and UserCustom.username != '' ">
                   and user.username LIKE '%${userCustom.username}%'
                </if>
            </if>
        </where>
    </select>

    <!--
    用户信息的综合查询总数
    parameterType:和findUserList一样
    resultType:输出的结果类型
     -->
    <select id="findUserCount" parameterType="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} and
                </if>
                <if test="UserCustom.username != null and UserCustom.username != '' ">
                    and user.username LIKE '%${userCustom.username}%'
                </if>
            </if>
        </where>
    </select>
可以使用where 和 if 条件语句进行sql语句的拼接
注意:
在此处的条件语句中,我们不能将包装pojo里面的pojo对象名写错,需要跟包装pojo类内部命名一致,不然就会报这个错
### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'UserCustom' in 'class pojo.UserQueryVo'
### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'UserCustom' in 'class pojo.UserQueryVo'

sql片段

需求
将上面实现的动态sql判断代码块,抽取出来,组成一个sql片段,其他的statement中可以引用该sql片段,方便程序员开发

定义
     <!--
     定义一个sql片段
     id:唯一标识
     经验参考:定义sql片段是基于单标,这样的sql片段可重用性才高
     在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>


引用:
    <select id="findUserList" parameterType="UserQueryVo"  resultType="UserCustom" >
        select * from user
        <!--
        where 可以自动去掉条件中的第一个and

        -->
        <where>
            <!--
            引用sql片段
            若sql片段不在本文件中,需要在前面加上mapper的namespace
            -->
            <include refid="query_user_where"/>
        </where>
    </select>



foreach

向sql中传递一个List,mybatis使用foreach解析

需求

在用户查询列表和查询总数的statement中,增加多个id的输入查询
sql语句如下:
SELECT * FROM USER WHERE id IN (1,2,16,28,29)
OR
SELECT * FROM USER WHERE id = 1 OR id = 2 OR id = 16 OR id = 28 OR id = 29

在输入的参数类型中添加List<Integer>ids 传入多个ID
// 传入多个ID
    private List<Integer> ids;
同时生成get set方法

修改mapper.xml
在sql片段中加入:
		<if test="ids != null ">
                    <!--
                    使用foreach 遍历传入的ids集合
                     collection:指定输入对象中的集合属性名
                     items:每次遍历生成的对象名
                     open; 开始遍历时的拼接串 and( 。。。 开始位置
                     close:结束遍历时的拼接串 ).. 结束位置
                     separator:遍历两个对象中间需要拼接的串
                     -->
                    <foreach collection="ids" item="foreach_id" open="and(" close=")" separator="or">
                        <!-- 每次遍历需要拼接的串 -->
                        id = #{foreach_id};
                    </foreach>
                </if>
另外一种:
<foreach collection="ids" item="user_id" open="and id in(" close=")" separator=",">
                        #{user_id}
</foreach>


测试:
    @Test
    public void testFindUserList() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建一个UserMapper的对象,mybatis自动生成mapper代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //创建pojo包装对象
        UserQueryVo userQueryVo = new UserQueryVo();
        //创建pojo扩展对象
        UserCustom userCustom = new UserCustom();
//        userCustom.setSex("1");
        userCustom.setUsername("测试");
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        ids.add(16);
        ids.add(28);
        ids.add(29);
        //将pojo扩展对象set进pojo包装对象
        userQueryVo.setUserCustom(userCustom);
        userQueryVo.setIds(ids);
        //调用UserMapper的方法
        List<UserCustom> list = userMapper.findUserList(userQueryVo);
        System.out.println(list);
    }











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值