MyBatis 动态sql语句 if foreach 详解

动态的if语句

Mybatis 的映射文件中,当业务逻辑复杂时,我们的 SQL是动态变化的,就需要用到动态的sql语句

动态 SQL 之 if 传过来具体对象

我们根据实体类的不同取值,使用不同的 SQL语句来进行查询。比如在 id如果不为空时可以根据id查询,如果username 不同空时还要加入用户名作为条件。这种情况在我们的多条件组合查询中经常会碰到。
我们在UserMapper里只需要定义如下的方法

public List<User> findByCondition(User user) throws Exception;

在UserMapper.xml 里则需要配置

<!--根据条件查询  if的动态sql-->
    <select id="findByCondition" parameterType="user" resultType="user">
<!--         select * from mybatis.user  -->
        <include refid="selectUser"></include>
        <where>
            <if test="id!=0 and id!=null">
                and id=#{id}
            </if>
            <if test="username!=null and username != '' ">
                and username=#{username}
            </if>
            <if test="password!=null and password != '' ">
                and password=#{password}
            </if>
        </where>
    </select>

当查询条件id和username都存在时,控制台打印的sql语句如下:
在这里插入图片描述

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

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

在这里插入图片描述
MyBatis框架会根据我们传过来的user对象里的值 自动动态的进行拼接sql语句

动态 SQL 之 if 传过来单个参数

UserMapper接口

public List<User> findByCondition3(Integer id) throws Exception;

测试代码

/**
     * 动态sql语句  if   传递单个参数
     */
    @Test
    public void test22() throws Exception {
        SqlSession sqlSession = MyBatisUtil.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> byCondition3 = mapper.findByCondition3(10);
        for (User user : byCondition3) {
            System.out.println(user);
        }
        sqlSession.close();
    }

UserMapper.xml
配置查询id大于 传过来的id值的数据库里的数据(上面给的是10)

<select id="findByCondition3" parameterType="int" resultType="user">
        <!--         select * from mybatis.user  -->
        <include refid="selectUser"></include>
        <where>
            <!--还可以把value换成 _parameter  -->
            <if test="#{value}">
                and id > #{value}
            </if>
        </where>
    </select>

这里的坑是 取值得用 #{value} 或者 #{_parameter}
否则会报没有getter方法的错 如下
在这里插入图片描述

动态 SQL 之 foreach 传过来集合

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

UserMapper

<!--根据id查询  foreach动态sql 传过来list集合-->
    <select id="findByIds" resultType="user" parameterType="java.util.List">

        <!--         select * from mybatis.user   -->
        <include refid="selectUser"/>
        <where>
            --                    集合是  list集合  以什么开头       以什么结束  每次遍历得到的对象   以什么分割
            <foreach collection="list" open="id in ("  close=")" item="id"      separator=",">
                #{id}
            </foreach>
        </where>

    </select>

UserMapper.xml

<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>
动态 SQL 之 foreach 传过来数组

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

UserMapper

/**
     * 动态语句  sql foreach  数组
     * @throws Exception
     */
    @Test
    public void test30() throws Exception {
        SqlSession sqlSession = MyBatisUtil.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //模拟数据

        int[] array =new int[4];
        array[0] = 1;
        array[1] = 2;
        array[2] = 5;

        List<User> byIds2 = mapper.findByIds2(array);
        for (User user : byIds2) {
            System.out.println(user);
        }

        sqlSession.close();
    }

UserMapper.xml

 <!--根据id查询  foreach动态sql 传过来array数组-->
    <select id="findByIds2" resultType="user" parameterType="list">
        <include refid="selectUser"></include>
        <where>
            <foreach collection="array" item="id" open="id in (" close=")" separator=",">
                #{id}
            </foreach>
        </where>
    </select>

在这里插入图片描述

发布了31 篇原创文章 · 获赞 8 · 访问量 1538

猜你喜欢

转载自blog.csdn.net/qq_37126480/article/details/103561083
今日推荐