MyBatis Plus
国产的开源框架,基于 MyBatis
核心功能就是简化 MyBatis 的开发,提高效率。
MyBatis Plus 快速上手
Spring Boot(2.3.0) + MyBatis Plus(国产的开源框架,并没有接入到 Spring 官方孵化器中)
1、创建 Maven 工程
2、pom.xml 引入 MyBatis Plus 的依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
3、创建实体类
package com.southwind.mybatisplus.entity;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}
4、创建 Mapper 接口
package com.southwind.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.southwind.mybatisplus.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
5、application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8
username: root
password: root
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
6、启动类需要添加 @MapperScan(“mapper所在的包”),否则无法加载 Mppaer bean。
package com.southwind.mybatisplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.southwind.mybatisplus.mapper")
public class MybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusApplication.class, args);
}
}
7、测试
package com.southwind.mybatisplus.mapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class UserMapperTest {
@Autowired
private UserMapper mapper;
@Test
void test(){
mapper.selectList(null).forEach(System.out::println);
}
}
常用注解
@TableName
映射数据库的表名
package com.southwind.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName(value = "user")
public class Account {
private Integer id;
private String name;
private Integer age;
}
@TableId(type = IdType.AUTO)
设置主键映射,value 映射主键字段名
type 设置主键类型,主键的生成策略
AUTO(0),
NONE(1),
INPUT(2),
ASSIGN_ID(3),
ASSIGN_UUID(4),
/** @deprecated */
@Deprecated
ID_WORKER(3),
/** @deprecated */
@Deprecated
ID_WORKER_STR(3),
/** @deprecated */
@Deprecated
UUID(4);
| 值 | 描述 |
|---|---|
| AUTO | 数据库自增 |
| NONE | Mabatis Plus set 主键,雪花算法实现 (用Long存) |
| INPUT | 需要开发者手动赋值 |
| ASSIGN_ID | Mabatis Plus 分配 ID,Long、Integer、String |
| ASSIGN_UUID | 分配 UUID,Strinig |
INPUT 如果开发者没有手动赋值,则数据库通过自增的方式给主键赋值,如果开发者手动赋值,则存入该值
AUTO 默认就是数据库自增,开发者无需赋值(赋值无效, 仍然自增)
ASSIGN_ID MP 自动赋值,雪花算法
ASSIGN_UUID 主键的数据类型必须是 String,自动生成 UUID 进行赋值
@TableField
映射非主键字段,value 映射字段名
exist 表示是否为数据库字段 false,如果实体类中的成员变量在数据库中没有对应的字段,则可以使用 exist. VO、DTO用的多
select 表示是否查询该字段
fill 表示是否自动填充,将对象存入数据库的时候,由 MyBatis Plus 自动给某些字段赋值,create_time、update_time
1、给表添加 create_time、update_time 字段
2、实体类中添加成员变量
package com.southwind.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
@Data
@TableName(value = "user")
public class User {
@TableId
private String id;
@TableField(value = "name",select = false)
private String title;
private Integer age;
@TableField(exist = false)
private String gender;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
3、创建自动填充处理器
package com.southwind.mybatisplus.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime",new Date(),metaObject);
this.setFieldValByName("updateTime",new Date(),metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime",new Date(),metaObject);
}
}
@Version
标记乐观锁,通过 version 字段来保证数据的安全性,当修改数据的时候,会以 version 作为条件,当条件成立的时候才会修改成功。
线程 1:update … set version = 2 where version = 1
线程2 :update … set version = 2 where version = 1
1、数据库表添加 version 字段,默认值为 1
2、实体类添加 version 成员变量,并且添加 @Version
package com.southwind.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.util.Date;
@Data
@TableName(value = "user")
public class User {
@TableId
private String id;
@TableField(value = "name",select = false)
private String title;
private Integer age;
@TableField(exist = false)
private String gender;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
@Version
private Integer version;
}
3、注册配置类
package com.southwind.mybatisplus.config;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
// 返回乐观锁拦截器
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor(){
return new OptimisticLockerInterceptor();
}
}
@EnumValue
1、通用枚举类注解,将数据库字段映射成实体类的枚举类型成员变量
package com.southwind.mybatisplus.enums;
import com.baomidou.mybatisplus.annotation.EnumValue;
public enum StatusEnum {
WORK(1,"上班"),
REST(0,"休息");
StatusEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
@EnumValue
private Integer code;
private String msg;
}
package com.southwind.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.*;
import com.southwind.mybatisplus.enums.StatusEnum;
import lombok.Data;
import java.util.Date;
@Data
@TableName(value = "user")
public class User {
@TableId
private String id;
@TableField(value = "name",select = false)
private String title;
private Integer age;
@TableField(exist = false)
private String gender;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
@Version
private Integer version;
// status 要和表中字段名一样
private StatusEnum status;
}
application.yml
type-enums-package:
com.southwind.mybatisplus.enums
2、实现接口
package com.southwind.mybatisplus.enums;
import com.baomidou.mybatisplus.core.enums.IEnum;
public enum AgeEnum implements IEnum<Integer> {
ONE(1,"一岁"),
TWO(2,"两岁"),
THREE(3,"三岁");
private Integer code;
private String msg;
AgeEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
// 和数据库字段对应
@Override
public Integer getValue() {
return this.code;
}
}
@TableLogic
映射逻辑删除
1、数据表添加 deleted 字段
2、实体类添加注解
package com.southwind.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.*;
import com.southwind.mybatisplus.enums.AgeEnum;
import com.southwind.mybatisplus.enums.StatusEnum;
import lombok.Data;
import java.util.Date;
@Data
@TableName(value = "user")
public class User {
@TableId
private String id;
@TableField(value = "name",select = false)
private String title;
private AgeEnum age;
@TableField(exist = false)
private String gender;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
@Version
private Integer version;
@TableField(value = "status")
private StatusEnum statusEnum;
@TableLogic
private Integer deleted;
}
3、application.yml 添加配置
global-config:
db-config:
logic-not-delete-value: 0
logic-delete-value: 1
mapper 查询
mapper.selectList()
// 不加条件查询
//mapper.selectList(null);
// 设置条件用, 这里用 Wrapper 的实现类
QueryWrapper wrapper = new QueryWrapper();
// Map<String,Object> map = new HashMap<>();
// map.put("name","小红");
// map.put("age",3);
// wrapper.allEq(map);
// wrapper.gt("age",2);
// wrapper.ne("name","小红");
// wrapper.ge("age",2);
//like '%小' 左模糊 右模糊
// wrapper.likeLeft("name","小");
//like '小%'
// wrapper.likeRight("name","小");
//inSQL
// wrapper.inSql("id","select id from user where id < 10");
// wrapper.inSql("age","select age from user where age > 3");
// wrapper.orderByDesc("age");
// wrapper.orderByAsc("age");
// wrapper.having("id > 8");
mapper.selectList(wrapper).forEach(System.out::println);
// System.out.println(mapper.selectById(7));
//查询多个 id mapper.selectBatchIds(Arrays.asList(7,8,9)).forEach(System.out::println);
//Map 只能做等值判断,逻辑判断需要使用 Wrapper 来处理
// Map<String,Object> map = new HashMap<>();
// map.put("id",7);
// mapper.selectByMap(map).forEach(System.out::println);
QueryWrapper wrapper = new QueryWrapper();
wrapper.eq("id",7);
//// System.out.println(mapper.selectCount(wrapper));
//
// //将查询的结果集封装到Map中
// mapper.selectMaps(wrapper).forEach(System.out::println);
// System.out.println("-------------------");
// mapper.selectList(wrapper).forEach(System.out::println);
System.out.println(mapper.selectOne(wrapper));
分页查询
首先在 MybatisConfig 中添加 MybatisPlusInterceptor
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
//这是分页拦截器
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setOverflow(true);
paginationInnerInterceptor.setMaxLimit(500L);
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
分页查询代码
//构造分页参数
Page<User> page = new Page<>(pn, 2);
//调用page进行分页
Page<User> userPage = userService.page(page, null);
// userPage.getRecords()
// userPage.getCurrent()
// userPage.getPages()
model.addAttribute("users",userPage);
自定义 SQL(多表关联查询)
在 mapper 中自定义了 sql 需要在 service 和 serviceImpl 实现
package com.southwind.mybatisplus.entity;
import lombok.Data;
@Data
public class ProductVO {
private Integer category;
private Integer count;
private String description;
private Integer userId;
private String userName;
}
package com.southwind.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.southwind.mybatisplus.entity.ProductVO;
import com.southwind.mybatisplus.entity.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper extends BaseMapper<User> {
@Select("select p.*,u.name userName from product p,user u where p.user_id = u.id and u.id = #{id}")
List<ProductVO> productList(Integer id);
}
添加
User user = new User();
user.setTitle("小明");
user.setAge(22);
mapper.insert(user);
System.out.println(user);
删除
//mapper.deleteById(1);
// mapper.deleteBatchIds(Arrays.asList(7,8));
// QueryWrapper wrapper = new QueryWrapper();
// wrapper.eq("age",14);
// mapper.delete(wrapper);
Map<String,Object> map = new HashMap<>();
map.put("id",10);
mapper.deleteByMap(map);
修改
// //update ... version = 3 where version = 2
// User user = mapper.selectById(7);
// user.setTitle("一号");
//
// //update ... version = 3 where version = 2
// User user1 = mapper.selectById(7);
// user1.setTitle("二号");
//
// mapper.updateById(user1);
// mapper.updateById(user);
UpdateWrapper<Employee> updateWrapper=new UpdateWrapper<>();
//UpdateWrapper<Employee> updateWrapper2 = Wrappers.<Employee>update();
Employee employee=new Employee();
employee.setEmail("1234@qq.com");
employee.setPhoneNumber("1234567");
updateWrapper.eq("id",6);
int affectRows=employeeMapper.update(employee,updateWrapper);
if(affectRows>0){
System.out.println("更新成功");
}else{
System.out.println("更新失败");
}
MyBatisPlus 自动生成
根据数据表自动生成实体类、Mapper、Service、ServiceImpl、Controller
1、pom.xml 导入 MyBatis Plus Generator
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
Velocity(默认)、Freemarker、Beetl
2、启动类
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class Main {
public static void main(String[] args) {
//创建generator对象
AutoGenerator autoGenerator = new AutoGenerator();
//数据源
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL);
dataSourceConfig.setUrl("jdbc:mysql://ip:3306/db?useUnicode=true&characterEncoding=UTF-8");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("root");
// 注意 mysql 版本导致这里会不同
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
autoGenerator.setDataSource(dataSourceConfig);
//全局配置
GlobalConfig globalConfig = new GlobalConfig();
// 拿到工程绝对路径 System.getProperty("user.dir")
globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");
globalConfig.setOpen(false);
globalConfig.setAuthor("yangyu");
globalConfig.setServiceName("%sService");
autoGenerator.setGlobalConfig(globalConfig);
//包信息
PackageConfig packageConfig = new PackageConfig();
// 父包
packageConfig.setParent("com.catplatform.mybatisplus");
// 在当前包下创建新的包
packageConfig.setModuleName("generator");
packageConfig.setController("controller");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setMapper("mapper");
packageConfig.setEntity("entity");
autoGenerator.setPackageInfo(packageConfig);
//配置策略
StrategyConfig strategyConfig = new StrategyConfig();
//只生成部分表的mapper service controller
// strategyConfig.setInclude("user", "seller");
// 自动增加 lombok 注解
strategyConfig.setEntityLombokModel(true);
// 下划线改成驼峰
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
// 下划线改成驼峰
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
autoGenerator.setStrategy(strategyConfig);
autoGenerator.execute();
}
}
修改 MapperScan
service
DepartmentService 继承IService
DepartmentServiceImpl 继承ServiceImpl类
@RunWith(SpringRunner.class)
@SpringBootTest
public class DepartmentServiceTest {
@Autowired
private DepartmentService departmentService;
/**
* 增
*/
@Test
public void save(){
Department department=new Department();
department.setName("洋芋");
department.setRemark("123");
boolean save = departmentService.save(department);
System.out.println(save);
}
/**
* 改
*/
@Test
public void updateById(){
Department department=new Department();
department.setId(9);
department.setName("洋芋2");
department.setRemark("1232");
boolean save = departmentService.updateById(department);
System.out.println(save);
}
/**
* 删
*/
@Test
public void removeById(){
boolean b = departmentService.removeById(9);
System.out.println(b);
}
/**
* 查
*/
@Test
public void list(){
List<Department> list = departmentService.list();
System.out.println(list);
}
/**
* 批量增加
*/
@Test
public void saveBatch(){
Department department1=new Department();
department1.setName("洋芋1");
department1.setRemark("1231");
Department department2=new Department();
department2.setName("洋芋2");
department2.setRemark("1232");
Department department3=new Department();
department3.setName("洋芋3");
department3.setRemark("1233");
boolean b = departmentService.saveBatch(Arrays.asList(department1, department2, department3));
System.out.println(b);
}
/**
* 批量增加或者更新
*/
@Test
public void saveOrUpdateBatch(){
Department department1=new Department();
department1.setName("洋芋4");
department1.setRemark("1234");
Department department2=new Department();
department2.setId(11);
department2.setName("洋芋2x");
department2.setRemark("1232x");
boolean b = departmentService.saveOrUpdateBatch(Arrays.asList(department1, department2));
System.out.println(b);
}
Spring Boot + MyBatis Plus 打包应用,直接发布 阿里云 上云
package + 在 target 中 用命令行 java -jar xxxx.jar
本文介绍了如何在SpringBoot 2.3.0中集成MyBatisPlus,包括创建Maven工程、添加依赖、配置实体类、Mapper接口、数据源设置、自动填充、乐观锁、枚举映射和逻辑删除等关键步骤。
2372

被折叠的 条评论
为什么被折叠?



