美文网首页JAVA数据之美
MyBatis框架(9):动态sql

MyBatis框架(9):动态sql

作者: 奇点一氪 | 来源:发表于2019-11-07 19:07 被阅读0次
  • 1.什么是动态sql
    mybatis核心,对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

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

  • 3.mapper.xml
    原查询语句配置:

<mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">
    
    <!-- 用户信息综合查询 
    #{UserCustom.sex}取出包装对象中性别值
    ${UserCustom.username}取得pojo包装对象中用户名称
    -->
    <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" 
                                resultType="cn.edu.hpu.mybatis.PO.UserCustom">
        select * from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
    </select>
    
    <!-- 用户信息综合查询总数 -->
    <select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">
        select count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
    </select>
    ......
</mapper>

修改后的查询语句配置:

<!-- 用户信息综合查询 
    #{UserCustom.sex}取出包装对象中性别值
    ${UserCustom.username}取得pojo包装对象中用户名称
    -->
    <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" 
                                resultType="cn.edu.hpu.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.edu.hpu.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>
  • 4.测试代码
//用户信息综合查询
    @Test
    public void testFindUserList() throws Exception{
        
        SqlSession sqlSession=sqlSessionFactory.openSession();
        
        //创建UserMapper代理对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        
        //创建包装对象,设置查询条件
        UserQueryVo userQueryVo=new UserQueryVo();
        UserCustom userCustom=new UserCustom();
        //由于这里使用动态sql,如果这里不设置某个值,条件不会拼接在sql中
        //userCustom.setSex("男");
        userCustom.setUsername("张三");
        userQueryVo.setUserCustom(userCustom);
        
        //调用userMapper的方法
        List<UserCustom> users=userMapper.findUserList(userQueryVo);
        
        for (int i = 0; i < users.size(); i++) {
            UserCustom user=(UserCustom)users.get(i);
            System.out.println(user.getId()+":"+user.getUsername());
        }
    }

测试结果:
1:张三
4:张三丰

输出日志:

DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 31761534.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@1e4a47e]
DEBUG [main] - ==>  Preparing: select * from user WHERE user.username like '%张三%' 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - <==      Total: 2

发现sql语句为select * from user WHERE user.username like '%张三%' ,并没有将sex拼接进去,说明我们的动态sql设置成功

相应的,把userCustom.setUsername("张三");也注释掉,发现输出日志:

DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 24027753.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@16ea269]
DEBUG [main] - ==>  Preparing: select * from user 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - <==      Total: 5

发现sql语句为select * from user,并没有将sex和username拼接进去,说明我们的动态sql设置成功

5.sql片段

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

5.2定义sql片段

<mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">
 
    <!-- 定义sql片段 
    id:sql片段的唯一标识 
    在sql片段中不要加入where
    经验:一般我们定义sql片段是为了可重用性,是基于单表来定义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>
    ......
</mapper>

5.3引用sql片段
在mapper.xml中定义的statement中引用sql片段


<!-- 用户信息综合查询 
    #{UserCustom.sex}取出包装对象中性别值
    ${UserCustom.username}取得pojo包装对象中用户名称
    -->
    <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" 
                                resultType="cn.edu.hpu.mybatis.PO.UserCustom">
        select * from user 
        
        <!-- where标签可以自动去掉第一个and -->  
        <where>
            <!-- 应用sql片段的id,如果refid指定的id不再本mapper文件中,需要前边加namespace -->
            <include refid="query_user_where"></include>
            <!-- 在这里还可能要引用其他的sql片段 -->
        </where>
    </select>
    
    <!-- 用户信息综合查询总数 -->
    <select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">
        select count(*) from user 
 
 
        <!-- where标签可以自动去掉第一个and -->  
        <where>
            <!-- 应用sql片段的id,如果refid指定的id不再本mapper文件中,需要前边加namespace -->
            <include refid="query_user_where"></include>
            <!-- 在这里还可能要引用其他的sql片段 -->
        </where>
    </select>

测试:

//用户信息综合查询
@Test
public void testFindUserList() throws Exception{
    
    SqlSession sqlSession=sqlSessionFactory.openSession();
    
    //创建UserMapper代理对象
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    
    //创建包装对象,设置查询条件
    UserQueryVo userQueryVo=new UserQueryVo();
    UserCustom userCustom=new UserCustom();
    //由于这里使用动态sql,如果这里不设置某个值,条件不会拼接在sql中
    userCustom.setSex("男");
    userCustom.setUsername("张三");
    userQueryVo.setUserCustom(userCustom);
    
    //调用userMapper的方法
    List<UserCustom> users=userMapper.findUserList(userQueryVo);
    
    for (int i = 0; i < users.size(); i++) {
        UserCustom user=(UserCustom)users.get(i);
        System.out.println(user.getId()+":"+user.getUsername());
    }
}

测试结果:
1:张三
4:张三丰
输出日志:

DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 17689439.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@10deb5f]
DEBUG [main] - ==>  Preparing: select * from user 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - <==      Total: 5

说明sql片段引用成功
小结:
sql片段方便程序员进行开发

相关文章

  • MyBatis框架(9):动态sql

    1.什么是动态sqlmybatis核心,对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。...

  • MyBatis动态SQL

    一.动态SQL简介 MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其他类似框架的经...

  • MyBatis动态SQL

    MyBatis 动态SQL 内容 Mybatis动态SQL在XML中支持的几种标签: if chose trim、...

  • Mybatis框架下SQL注入漏洞书目录

    Mybatis框架下SQL注入漏洞 SQL注入漏洞基本原理 Mybatis框架介绍 Mybatis框架下易产生SQ...

  • Mybatis--动态SQL(if,where,trim)

    Mybatis框架的动态SQL技术是一种根据特定条件动态拼接SQL语句的功能,作用是为了解决拼接SQL语句字符串的...

  • MyBatis核心知识点

    (1)Mybatis动态sql是做什么的?都有哪些动态sql?能简述一下动态sql的执行原理不? Mybatis动...

  • MyBatis 动态SQL(*.xml)

    原文参考MyBatis 动态SQL MyBatis的动态SQL大大减少了拼接SQL语句时候的各种格式问题,这里摘录...

  • MyBatis中$和#的区别

    动态 SQL 是 mybatis 的强大特性之一,也是它优于其他 ORM 框架的一个重要原因。mybatis 在对...

  • Mybatis动态SQL

    MyBatis Mybatis笔记连载上篇连接MyBatis缓存Mybatis笔记连载下篇连接 动态SQL 动态S...

  • MyBatis的动态SQL与日志Log4J、SQL语句构造器

    一、MyBatis动态SQL 动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似...

网友评论

    本文标题:MyBatis框架(9):动态sql

    本文链接:https://www.haomeiwen.com/subject/vtmvbctx.html