目录
一、动态 SQL
二、xml 自动生成工具
1. 引入依赖
2. 添加 generator.xml 并修改
三、MyBatis-plus
1. 引入依赖
2. mybatis-plus 配置
3. 编码
前言
本文介绍了MyBatis的动态SQL功能及其常用标签:1. <if>标签实现条件判断;2. <trim>标签处理多余字符;3. <where>标签智能生成查询条件;4. <set>标签用于更新操作;5. <foreach>标签遍历集合;6. <include>标签复用SQL片段。还讲解了MyBatis Generator自动生成代码工具的使用方法,包括配置文件和插件配置。最后介绍了MyBatis-Plus的快速集成方式,展示了如何通过注解映射数据库表,并利用其提供的BaseMapper简化开发。这些技术能有效提升MyBatis开发效率和灵活性。
一、动态 SQL
动态 SQL 是 MyBatis 的强大特性之一,能够完成不同条件下不同 SQL 的拼接;
1. <if> 标签
比如在填表的时候,某些字段是必填字段,某些字段是非必填字段,用户填写完成后提交,就需要根据用户填写的字段,拼接相应的 SQL 的语句,不同用户填写的字段不同,拼接的 SQL 语句也就不同;
接口定义:
import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserInfoXmlMapper {Integer insertUserByCondition(UserInfo userInfo);
}xml 实现:
    insert into userinfo (username, password,age, gender )values (#{username}, #{password},#{age}, #{gender} ) 2. <trim> 标签
上面的 xml 实现拼接的 sql 语句,如果 gender 属性没有填,则 age 属性会多拼接一个逗号,导致 sql 语法错误;
为了解决上述问题,引入了 <trim> 标签;
- prefix:表⽰整个语句块,以prefix的值作为前缀;
- suffix:表⽰整个语句块,以suffix的值作为后缀;
- prefixOverrides:表示整个语句块要去除掉的前缀;
- suffixOverrides:表⽰整个语句块要去除掉的后缀;
接口定义不变,xml 实现如下:
insert into userinfousername, password, age, gender #{username}, #{password}, #{age}, #{gender}  <trim> 标签通过设置前缀后缀的方式,可以将多余的部分,比如逗号去掉;
3. <where> 标签
<where> 标签可以实现和 <trim> 标签类似的功能,当有多个条件可输入的时候,可以去除掉开头多余的 and 或者 or,当没有条件的时候,可以去除掉 where 关键字;
接口定义:
import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserInfoXmlMapper {List queryByCondition(UserInfo userInfo);
} xml 实现:
    id, username, password, age, gender, phone, delete_flag, create_time, update_time 4. <set> 标签
<set> 标签会动态插入 set 关键字,也会去除掉额外的逗号;
接口定义:
import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserInfoXmlMapper {Integer updateUserByCondition(UserInfo userInfo);
}xml 实现:
    update userinfousername = #{username}, age = #{age}, delete_flag = #{deleteFlag}  5. <foreach> 标签
对集合遍历可以使用 <foreach> 标签;
标签具有以下属性:
- collection:绑定⽅法参数中的集合,如List,Set,Map或数组对象;
- item:遍历时的每⼀个对象;
- open:语句块开头的字符串;
- close:语句块结束的字符串;
- separator:每次遍历之间间隔的字符串;
接口方法:
import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserInfoXmlMapper {Integer deleteByIds(List ids);
} xml 实现:
    delete from userinfowhere id in#{id}  6. <include> 标签
对于重复的冗余 xml 片段,可以使用 <sql> 标签封装成一个 sql 片段,然后通过 <include> 标签进行引用;
    id, username, password, age, gender, phone, delete_flag, create_time, update_time 二、xml 自动生成工具
1. 引入依赖
org.mybatis.generator mybatis-generator-maven-plugin 1.3.5 src/main/resources/generator/generatorConfig.xml true true mysql mysql-connector-java 8.0.33 注意配置 generator 配置文件所在位置;
引入插件后,多了一个插件,如下图:


2. 添加 generator.xml 并修改
双击 generate,生成实体类,mapper xml 文件和 mapper 接口;

三、MyBatis-plus
1. 引入依赖
        com.baomidou mybatis-plus-spring-boot3-starter 3.5.5 2. mybatis-plus 配置
spring:application:name: mybatis-plus-demo# 数据库配置datasource:url: jdbc:mysql://127.0.0.1:3308/mybatis_test?characterEncoding=utf8&useSSL=falseusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:#  配置 mybatis xml 的文件路径,在 resources/mapper 创建所有表的 xml 文件mapper-locations: classpath:mybatis/**Mapper.xmlconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 配置打印 MyBatis日志map-underscore-to-camel-case: true #配置驼峰自动转换3. 编码
@TableName 注解用于指定数据库表名;
@TableId 注解用于指定主键;
@TableFiled 注解用于指定列名
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
@Data
@TableName("userinfo")
public class UserInfo {@TableIdprivate Integer id;@TableFieldprivate String username;private String password;private Integer age;private Integer gender;private String phone;private Integer deleteFlag;private Date createTime;private Date updateTime;
}import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserInfoMapper extends BaseMapper {
} BaseMapper 中有常用的接口实现,直接调用即可;