Mybatis----动态sql

8.动态sql

8.1什么是动态sql

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

8.2 需求

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

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

8.3mapper.xml

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

其他的不要修改,它会根据传入的参数,动态的拼接sql。

8.4 sql片段

8.5.1 需求

将上边实现的动态sql判断代码块抽取出来,组成一个sql片段,其它statement中就可以引用sql片段。

8.5.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.username !=null and userCustom.username !=''">
                    and user.username like '%${userCustom.username}%'
                </if>
            </if>
     </sql>

8.5.3 引用sql片段

    <select id="findUserList" parameterType="com.xjs.mybatis.po.UserQueryVo" resultType="com.xjs.mybatis.po.UserCustom">
        select * from user 
        <!-- where可以自动去掉条件的第一个and -->
        <where>
            <!-- 引用sql片段的id,如果refid指定的id不在本mapper文件中,需要前边加namespace -->
            <include refid="query_user_where"></include>
            <!-- 在这里还要引用其他的sql片段 -->
        </where>
        
    </select>

 8.6 foreach

向sql中传递一个数组或List,mybatis使用foreach。

8.6.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)

8.6.2 在输入参数类型中添加List<Integer> ids传入多个id

8.6.3 修改mapper.xml

 WHERE id=1 or id=10 OR id=16

在查询条件中,查询条件定义成一个sql片段,需要修改sql片段。

            <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>

测试:

//传入多个id
        List<Integer> ids=new ArrayList<Integer>();
        ids.add(1);
        ids.add(10);
        ids.add(16);
        
        userQueryVo.setIds(ids);

另一种sql拼写:

                <!-- and id IN(1,10,16) -->
                <foreach collection="ids" item="user_id" open="and id in(" close=")" separator=",">
                    #{user_id}
                </foreach>
原文地址:https://www.cnblogs.com/xjs1874704478/p/11243484.html