mybatis基础知识(五)&动态sql

1.1什么是动态sql

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

1.2需求
用户信息综合查询列表和用户信息查询列表总数这俩个statement的定义使用动态sql语句
对查询条件的判断: 如果输入的参数不为空才能进行查询条件拼接
1.2.1mapper.xml

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

2sql片段

2.1需求
将上边实现的动态的sql判断代码块抽取出来。组成一个sql片段。其他的statement中就可以引用sql片段
2.2定义sql片段  
<!-- 定义sql片段
    id:sq片段的唯一标识

    经验:是基于单标来定义sql片段,这样话这个sql片段可以重父率才比较高
    -->
    <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.3sql片段代码在其中的使用
<select id="findUserList" parameterType="mybaties.po.UserQueryVo"
    resultType="int">
    select * from user
    <!--
    where"可以自动去掉条件中的第一个and
    -->
    <where>
        <!--引用sql片段的refid如果id指定的id不在本mapper文件中,需要在前边家namespace>
        <include refid="query_user_where"</include>
        <!--这里引用其他的片段>  
    </where>    
    </select>       
2.4foreach标签
向sql传递数组组成listmybatis使用forEach解析
    2.4.1需求
        在用户查询列表和查询总数的statement中增加多个id输入查询   
        sql语句如下
            俩种方法
                select * from user where id=1 or id=2 or id=15;
                select * from user where id in(1,2,5)
    2.4.2   在输入参数类型中添加List<Integer>ids传入多个id
        public class UserQueryVo{
            //传入多个id
            private List<Integer> ids;

        }
    2.4.3修改mapper.xml
        <if  test="ids!=null">
        <!-- 使用forEach遍历ids
            collection 指定输入对象集合属性
            item:每个遍历生成对象中
            open:开始遍历是拼接的串
            close:结束遍历是拼接的串
            separator:遍历的俩个对象中需要拼接的串
            -->
            <!-- 遍历实现下边的sq的拼接
            AND (id=1 or id=2 )
            -->
            <foreach collection="ids"  item="user_id" open="AND (" close =")"  separator='or">
                <!-- 每个遍历需要拼接的串-->
                id=#{user_id}
            </foreacn>
            <!-- 遍历实现下边的sq的拼接
            id in(1,2,5)
            and id in(1,2,5)
            -->
            <foreach collection="ids"  item="user_id" open="and id in(" close =")"  separator=',">
                <!-- 每个遍历需要拼接的串-->
                id=#{user_id}
            </foreacn>                  
    </if>
    2.4.4 测试代码
    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("1");
        userCustom.setUsername("小明");
        //传入多个id
        List<Integer> ids=new ArrayList<Integer>();
        ids.add(1);
        ids.add(10);
        ids.add(15);
        //将IDS通过userQueryVo传入statement中
        userQueryVo.setIds(ids);
        userQueryVo.setUserCustom(userCustom);
        //调用userMapper的方法

        System.out.print(lcount;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值