mybatis 动态Sql

一、什么是动态sql

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

二、where

   <select id="findUserlist"
        parameterType="com.xxx.mybatis.po.UserQueryVo"
        resultType="com.xxx.mybatis.po.UserExtend">
        select * from t_user
        <!-- where可以自动去掉条件中的第一个and -->
        <where>
            <if test="userExtend!=null">
                <if test="userExtend.sex!=null and userExtend.sex!=''">
                    and sex = #{userExtend.sex}
                </if>
                <if test="userExtend.username!=null and userExtend.username!=''">
                    and userName LIKE CONCAT('%',#{userExtend.username},'%' )
                </if>
            </if>
        </where>
    </select>

三、sql片段

其它的statement中就可以引用sql片段,方便程序员进行开发

定义sql片段

    <sql id="query_user_where">
        <if test="userExtend!=null">
            <if test="userExtend.sex!=null and userExtend.sex!=''">
                and sex = #{userExtend.sex}
            </if>
            <if test="userExtend.username!=null and userExtend.username!=''">
                and userName LIKE CONCAT('%',#{userExtend.username},'%' )
            </if>
        </if>
    </sql>

引用sql片段,如果refid指定id不在本mapper.xml文件中,需要前面加命名空间点(格式:namespace+"."+id)

        <where>
            <include refid="query_user_where" />
        </where>

sql片段不要写where

四、foreach

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

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

public class UserQueryVo {
    private List<Integer> ids;

2、mapper.xml

<select id="findUserlist"
        parameterType="com.xxx.mybatis.po.UserQueryVo"
        resultType="com.xxx.mybatis.po.UserExtend">
        select * from t_user
        <where>
            <include refid="query_user_where"/>
            <if test="ids!=null">
                <!-- 
                collection:指定输入对象中集合属性
                item:遍历集合中项目名
                open:sql开始遍历前和别的sql语句的连接串(and / or)
                close:结束遍历时拼接的串
                separator:遍历的项目之间拼接的字符串
                 -->
                 <!-- 实现
                     and (id=1 OR id=10 OR id=16)
                  -->
                <foreach collection="ids" item="id" open="And (" close=")"
                    separator="Or">
                    id=#{id}
                </foreach>
                
                 <!-- 实现
                     and IN(1,10,16)
                  --><!--
                <foreach collection="ids" item="userid" open="And IN(" close=")"
                    separator=",">
                    #{userid}
                </foreach> -->
            </if>
        </where>
    </select>

3、测试代码

    @Test
    void testFindUserList() {
    
    UserMapper userMapper = sqlSessionFactory.openSession().getMapper(UserMapper.class);
    UserQueryVo queryVo=new UserQueryVo();
    UserExtend userExtend=new UserExtend();
    userExtend.setSex("0");
    userExtend.setUsername("小");
    
    queryVo.setUserExtend(userExtend);
    List<Integer> ids=new ArrayList<Integer>();
    ids.add(1);
    ids.add(10);
    ids.add(9);
    queryVo.setIds(ids );
    List<UserExtend> list = userMapper.findUserlist(queryVo);
    System.out.println(list);
    }
原文地址:https://www.cnblogs.com/WarBlog/p/14931670.html