mybatis 动态sql

1 动态sql

什么是动态sql

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

需求

用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。
对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。

mapper.xml

<!--用户信息综合查询
    #{userCustom.sex}:取出pojo包装对象中性别值
    ${userCustom.loginname}:取出pojo包装对象中用户名称
      -->
    <select id="findUserList2" parameterType="mybatis.po.UserQueryVo" resultType="mybatis.po.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.loginname!=null and userCustom.loginname!=''">
                    and user.loginname like '%${userCustom.loginname}%'
                </if>
            </if>
        </where>
    </select>

    <select id="findUserCount2" parameterType="mybatis.po.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.loginname!=null and userCustom.loginname!=''">
                    and user.loginname like '%${userCustom.loginname}%'
                </if>
            </if>
        </where>
    </select>

测试代码

@Test
    public void testFindUserList2() throws Exception{
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //创建UserMapper对象,mybatis自动生成mapper代理对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        //创建包装对象,设置查询条件
        UserQueryVo userQueryVo=new UserQueryVo();
        UserCustom userCustom=new UserCustom();
        //由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中
        //userCustom.setSex("男");
        userCustom.setLoginname("小明");
        userQueryVo.setUserCustom(userCustom);
        //调用userMapper方法
        List<UserCustom> list=userMapper.findUserList2(userQueryVo);
        System.out.println(list);
    }

    @Test
    public void testFindUserCount() throws Exception{
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //创建UserMapper对象,mybatis自动生成mapper代理对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        //创建包装对象,设置查询条件
        UserQueryVo userQueryVo=new UserQueryVo();
        UserCustom userCustom=new UserCustom();
        //userCustom.setSex("男");
        userCustom.setLoginname("小明");
        userQueryVo.setUserCustom(userCustom);
        //调用userMapper方法
        int count=userMapper.findUserCount2(userQueryVo);
        System.out.println(count);
    }

2 sql片段

1 需求

将上边实现的动态sql判断代码块抽取出来,组成一个sql片段。其它的statement中就可以引用sql片段。
方便程序员进行开发。

2 定义sql片段

<!--定义sql片段
    id:sql片段的唯一标识  
    经验:是基于表单来定义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.loginname!=null and userCustom.loginname!=''">
                and user.loginname like '%${userCustom.loginname}%'
            </if>
        </if>
    </sql>
    <!--在mapper.xml中定义的statement中引用sql片段:  -->
    <select id="findUserList3" parameterType="mybatis.po.UserQueryVo" resultType="mybatis.po.UserCustom">
        select * from user
        <where>
            <!--引用sql片段的id,如果refid指定的id不在本mapper文件中,需要前边加载namespace  -->
            <include refid="query_user_where"></include>
        </where>
    </select>

foreach

向sql传递数组或List,mybatis使用foreach解析

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)

.2 在输入参数类型中添加List ids传入多个id

<sql id="user_id">
        <if test="ids!=null">
        <!--使用foreach遍历传入ids
        collection:指定输入对象中集合属性
        item:每个遍历生成对象中
        open:结束遍历时拼接的串
        separator:遍历的两个对象中需要拼接的串
          -->
          <!--使用实现下边的sql拼接:and (id=1 or id=10 or id=16)  -->
            <foreach collection="ids" item="uid"  open="and (" close=")" separator="or">
                uid=#{uid}
            </foreach>
        </if>
    </sql>
    <sql id="user_id2">
        <if test="ids!=null">
        <!--使用foreach遍历传入ids
        collection:指定输入对象中集合属性
        item:每个遍历生成对象中
        open:结束遍历时拼接的串
        separator:遍历的两个对象中需要拼接的串
          -->
          <!--使用实现下边的sql拼接:and (id=1 or id=10 or id=16)  -->
            <foreach collection="ids" item="uid"  open="and uid in (" close=")" separator=",">
                #{uid}
            </foreach>
        </if>
    </sql>

    <!--在mapper.xml中定义的statement中引用sql片段:  -->
    <select id="findUserByIds" parameterType="mybatis.po.UserQueryVo" resultType="mybatis.po.UserCustom">
        select * from user
        <where>
            <!--引用sql片段的id,如果refid指定的id不在本mapper文件中,需要前边加载namespace  -->
            <include refid="user_id2"></include>
        </where>
    </select>

测试代码

@Test
    public void testFindUserByIds() throws Exception{
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //创建UserMapper对象,mybatis自动生成mapper代理对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        //创建包装对象,设置查询条件
        UserQueryVo userQueryVo=new UserQueryVo();
        List<Integer> ids=new ArrayList<Integer>();
        ids.add(1234276);
        ids.add(1234277);
        ids.add(1234279);
        userQueryVo.setIds(ids);
        //调用userMapper方法
        List<UserCustom> list=userMapper.findUserByIds(userQueryVo);
        System.out.println(list);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值