• day13:MyBatis映射文件深入


    MyBatis映射文件深入

    动态sql语句

    动态sql语句概述

    Mybatis 的映射文件中,前面我们的 SQL 都是比较简单的,有些时候业务逻辑复杂时,我们的 SQL是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了。参考的官方文档,描述如下:
    在这里插入图片描述

    动态 SQL 之

    我们之前都是使用的静态的sql语句,如:

    select * from user WHERE id=#{id} and username=#{username} and password=#{password} 
    
    • 1

    这种语句无法根据传入不同数量的参数(如只传入id,或传入id和username不传入password)来更改查询的条件。
    在动态sql语句中我们根据实体类的不同取值,使用不同的 SQL语句来进行查询。比如在 id如果不为空时可以根据id查询,如果username 不同空时还要加入用户名作为条件。这种情况在我们的多条件组合查询中经常会碰到。

    <mapper namespace="com.mapper.UserMapper">
        <select id="findByCondition" parameterType="user" resultType="user">
            select * from user
            <where>
                <if test="id!=0">
                    and id=#{id}
                if>
                <if test="username!=null">
                    and username=#{username}
                if>
                <if test="password!=null">
                    and password=#{password}
                if>
            where>
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    当查询条件id和username都存在时,控制台打印的sql语句如下:

    … … …
    //获得MyBatis框架生成的UserMapper接口的实现类
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    User condition = new User();
    condition.setId(1);
    condition.setUsername("zhangsan");
    condition.setPassword("123");
    User user = userMapper.findByCondition(condition);
    … … …
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    当查询条件只有id存在时,控制台打印的sql语句如下:

    … … …
    //获得MyBatis框架生成的UserMapper接口的实现类
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    User condition = new User();
    condition.setId(1);
    User user = userMapper.findByCondition(condition);
    … … …
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    当传入的只是空的user对象参数,没有任何查询条件时,会将所有的数据进行查询。

    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    User condition = new User();
    User user = userMapper.findByCondition(condition);
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    动态 SQL 之

    循环执行sql的拼接操作,例如:SELECT * FROM USER WHERE id IN (1,2,5)

    <select id="findByIds" parameterType="list" resultType="user">
    	select * from User
    	<where>
    		<foreach collection="array" open="id in(" close=")" item="id" separator=",">
    		 
             
    			#{id}
    		foreach>
    	where>
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    测试代码片段如下:

    … … …
    //获得MyBatis框架生成的UserMapper接口的实现类
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    int[] ids = new int[]{2,5};
    List<User> userList = userMapper.findByIds(ids);
    System.out.println(userList);
    … … …
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    foreach标签的属性含义如下:
    foreach标签用于遍历集合,它的属性:
    • collection:代表要遍历的集合元素,注意编写时不要写#{}
    • open:代表语句的开始部分
    • close:代表结束部分
    • item:代表遍历集合的每个元素,生成的变量名
    • sperator:代表分隔符

    SQL片段抽取

    Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的
    在这里插入图片描述
    ###知识小结
    MyBatis映射文件配置: