Mybatis的动态SQL语句(六)

本文基于Mybatis的SqlMapConfig.xml配置文件(五)进行功能完善
项目结构图:
在这里插入图片描述
红框标记的是不需要改动的。

1.动态 SQL 之<if>标签

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

1.1mapper接口

/**
     * 根据传入参数调节
     * @param user 查询的条件:有可能是地址,有可能姓名,有可能都有。。。
     * @return
     */
    List<User> findUserByCondition(User user);

1.2UserMapper.xml映射配置

<!--根据条件查询-->
    <select id="findUserByCondition" resultType="user" parameterType="user">
        select * from user where 1 = 1
            <if test="username != null">
                and username = #{username}
            </if>
            <if test="sex != null">
               and sex = #{sex}
           </if>
    </select>
注意:标签的 test 属性中写的是对象的属性名。 另外要注意 where 1=1 的作用!

1.3测试

MybatisTest.java

 /**
     * 根据情况查询
     */
    @Test
    public void testfindByCondition(){
        User u = new User();
        u.setUsername("aotocommit");
        u.setSex("m");
        List<User>  users = userMapper.findUserByCondition(u);
        for(User user : users){
            System.out.println(user);
        }
    }

2.动态 SQL 之<where>标签

为了简化上面where 1=1的条件拼装,我们可以采用标签来简化开发。

2.1UserMapper.xml映射配置

    <!--根据条件查询-->
    <select id="findUserByCondition" resultType="user" parameterType="user">
        select * from user
        <where>
            <if test="username != null">
                and username = #{username}
            </if>
           <if test="sex != null">
                and sex = #{sex}
            </if>
        </where>
    </select>

3.动态标签之<foreach>标签

3.1在 QueryVo 中加入一个 List 集合用于封装参数

package com.wd.entity;

import java.util.List;

public class QueryVo {

    private User user;
    private List<Integer> ids;


    public List<Integer> getIds() {
        return ids;
    }

    public void setIds(List<Integer> ids) {
        this.ids = ids;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

3.2mapper接口

 /**
     * 根据queryvo中提供的id集合,查询用户信息
     * @param vo
     * @return
     */
    List<User> findUserInIds(QueryVo vo);

3.3UserMapper映射配置

  <!--根据queryvo中提供的id集合,查询用户信息-->
    <select id="findUserInIds" resultType="user" parameterType="user">
        select * from user
        <where>
            <if test="ids != null and ids.size() > 0">
                <foreach collection="ids" open="and id in (" close=")" item="id" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>

    </select>

3.4测试

MybatisTest.java

 /**
     * 测试foreach标签的使用
     */
    @Test
    public void testFindInIds(){
        QueryVo vo = new QueryVo();
        List<Integer> list = new ArrayList<Integer>();
        list.add(41);
        list.add(42);
        list.add(46);
        vo.setIds(list);

        //5.执行查询所有方法
        List<User> users = userMapper.findUserInIds(vo);
        for(User user : users){
            System.out.println(user);
        }

    }

4.Mybatis 中简化编写的 SQL 片段

Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的。

4.1定义代码片段

 <!-- 了解的内容:抽取重复的sql语句-->
    <sql id="defaultUser">
        select * from user
    </sql>

4.2引用代码片段

在这里插入图片描述

5.项目源代码

源代码地址
https://github.com/Qreply/mybatisDynamicSql.git

发布了49 篇原创文章 · 获赞 3 · 访问量 2325

猜你喜欢

转载自blog.csdn.net/xiao_count/article/details/102663878