Mybatis深入了解(五)----动态SQL

简介: 什么是动态SQL 实例Mapperxml测试代码sql片段定义sql片段引用sql片段foreach应用场景在输入参数类型中添加List ids传入多个id修改Mapperxml测试代码什么是动态SQL?     动态sql是mybatis的核心,主要是对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活的拼接、组装。

什么是动态SQL?


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

实例


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

    

Mapper.xml


<!-- 用户信息综合查询
    #{userCustom.sex}:取出pojo包装对象中性别值
    ${userCustom.username}:取出pojo包装对象中用户名称
     -->
    <select id="findUserList" parameterType="cn.itcast.mybatis.po.UserQueryVo" 
            resultType="cn.itcast.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>


    </select>
<select id="findUserCount" parameterType="cn.itcast.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.username!=null and userCustom.username!=''">
                and user.username LIKE '%${userCustom.username}%'
            </if>
        </if>
    </where>


    </select>

    

测试代码


//用户信息的综合 查询
    @Test
    public void testFindUserList() 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("1");
        userCustom.setUsername("小明");
        userQueryVo.setUserCustom(userCustom);
        //调用userMapper的方法

        List<UserCustom> list = userMapper.findUserList(userQueryVo);

        System.out.println(list);
    }

    

sql片段


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

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

引用sql片段

<!-- 用户信息综合查询
    #{userCustom.sex}:取出pojo包装对象中性别值
    ${userCustom.username}:取出pojo包装对象中用户名称
     -->
    <select id="findUserList" parameterType="cn.itcast.mybatis.po.UserQueryVo" 
            resultType="cn.itcast.mybatis.po.UserCustom">
    SELECT * FROM USER
    <!-- 
    where可以自动去掉条件中的第一个and
     -->
    <where>
        <!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前边加namespace -->
        <include refid="query_user_where"></include>
        <!-- 在这里还要引用其它的sql片段  -->
    </where>


    </select>

    其中的关键是这一句引用:

<!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前边加namespace -->
<include refid="query_user_where"></include>

    

foreach


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

应用场景

    在用户查询列表和查询总数的statement中增加多个id输入查询。
    类似下面的sql语句:

SELECT * FROM USER WHERE id=1 OR id=10 OR id=16

SELECT * FROM USER WHERE id IN(1,10,16)

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

public class UserQueryVo {

    //传入多个id
    private List<Integer> ids;

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

测试代码

    修改上面的测试方法,传入多个id进行测试。

    //创建包装对象,设置查询条件
    UserQueryVo userQueryVo = new UserQueryVo();
    UserCustom userCustom = new UserCustom();
    //由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中
    //userCustom.setSex("1");
    userCustom.setUsername("小明");
    //传入多个id
    List<Integer> ids = new ArrayList<Integer>();
    ids.add(1);
    ids.add(10);
    ids.add(16);
    //将ids通过userQueryVo传入statement中
    userQueryVo.setIds(ids);
    userQueryVo.setUserCustom(userCustom);
    //调用userMapper的方法

    List<UserCustom> list = userMapper.findUserList(userQueryVo);

    System.out.println(list);
相关文章
|
2天前
|
SQL XML Java
mybatis 调用修改SQL时 出现了一个问题 没有修改成功也没有报错
mybatis 调用修改SQL时 出现了一个问题 没有修改成功也没有报错
13 0
|
1月前
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
21 1
|
1月前
|
Java 关系型数据库 数据库连接
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
26 1
|
1月前
Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)
Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)
15 1
|
1月前
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
22 1
|
1月前
Mybatis+mysql动态分页查询数据案例——房屋信息的实现类(HouseDaoMybatisImpl)
Mybatis+mysql动态分页查询数据案例——房屋信息的实现类(HouseDaoMybatisImpl)
22 2
|
1月前
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
15 1
|
1月前
|
Java 数据库连接 mybatis
Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)
Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)
20 1
|
1月前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
15 1
|
1月前
Mybatis+mysql动态分页查询数据案例——房屋信息的接口(IHouseDao)
Mybatis+mysql动态分页查询数据案例——房屋信息的接口(IHouseDao)
12 1