MyBatis动态sql

这篇文章介绍了MyBatis-动态sql


动态 SQL 是 MyBatis 的强大特性之一。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。

一.标签

if标签:条件判断
choose、when、otherwise标签:选择
trim、where、set标签
foreach标签: 遍历集合
sql、include标签:定义、使用代码片段

二.动态sql语句

准备

Student类,数据库中与之对应

package com.hem.pojo;

public class Student {
    private Integer id;
    private String name;
    private String email;
    private Integer age;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", age=" + age +
                '}';
    }

    public Student(String name, String email, Integer age) {
        this.name = name;
        this.email = email;
        this.age = age;
    }

    public Student(Integer id, String name, String email, Integer age) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.age = age;
    }

    public Student() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

1.查询

1.1多条件动态查询

使用< where > + < if >
if进行条件判断,where 可以去掉多余的and

示例
StudentMapper接口:

package com.hem.mapper;

import com.hem.pojo.Student;

import java.util.List;

public interface StudentMapper {

    List<Student> selectDynamic(Student student);
}

StudentMapper.xml文件:

<select id="selectDynamic" resultType="student">
        select id,name,email,age
        from student
        <where>
            <if test=" name != null and name != '' ">
                name like concat('%',#{name},'%')
            </if>
            <if test=" email != null and email != '' ">
                and email like concat('%',#{email},'%')
            </if>
            <if test=" age != null ">
                and age = #{age}
            </if>
        </where>;
    </select>

测试类:

    @Test
    public void testinsertDynamic() throws IOException {
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = factory.openSession();

//        获取StudentMapper接口的代理对象
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student("小","1",18);
        List<Student> list = studentMapper.selectDynamic(student);
        list.forEach(student1 -> System.out.println(student1));
        sqlSession.close();
    }

测试结果:
在这里插入图片描述

1.2单条件-多条件动态查询(从多条件中选一个进行查询)

使用< where >,< choose >,< when >
choose:相当于switch
when:相当于case

示例
StudentMapper接口:

public interface StudentMapper {
    List<Student> selectByCondition(Student student);
}

StudentMapper.xml:

<select id="selectByCondition" resultType="student">
        select id,name,email,age
        from student
        <where>
            <choose>  <!-- 相当于switch -->
                <when test="age != null"> <!-- 相当于case -->
                    age = #{age}
                </when>
                <when test="name != null and name!='' ">
                    and name like concat('%',#{name},'%')
                </when>
                <when test="email != null and email!='' ">
                    and email like concat('%',#{email},'%')
                </when>
            </choose>;
        </where>
    </select>

2.修改

使用< set> + < if >
if进行条件判断,set可以去掉多余的逗号’ , ’

示例
StudentMapper接口:

package com.hem.mapper;

import com.hem.pojo.Student;

import java.util.List;

public interface StudentMapper {
    int updateDynamic(Student student);
}

StudentMapper.xml文件:

    <update id="updateDynamic" parameterType="student">
        update student
        <set>
            <if test=" name != null and name != '' ">
                name = #{name},
            </if>
            <if test=" email != null and email != '' ">
                email = #{email},
            </if>
            <if test=" age != null ">
                age = #{age},
            </if>
        </set>
        where id = #{id};
    </update>

3.定义使用代码片段

< sql >:定义代码片段
< include >:使用代码片段

<!--    定义代码片段-->
    <sql id="allColumns">
        id,name,email,age
    </sql>
    
<!--    使用代码片段-->
   <include refid="allColumns"/>

例子

    <sql id="allColumns">
        id,name,email,age
    </sql>
    
<select id="selectAll" resultType="student">
        select <include refid="allColumns"/>
        from student;
    </select>

202210162215日

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baibai___

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值