概述
MybatisPlus是国产的第三方插件, 它封装了许多常用的CURDapi,免去了我们写mapper.xml的重复劳动,这里介绍了基本的整合SpringBoot和基础用法。
引入依赖
在项目中pom
文件引入mybatisplus
和mysql
驱动依赖,如下图
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency>
配置连接信息
在项目中application.yaml文件中配置数据库的连接信息
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/didiadmin?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true username: root password: 123456
新建两个表
本案例通过一个完整的业务流程来介绍如何使用mybatispuls
,首先在数据库中新建两个表,一个是字典类型表sys_dict_type
和一个字典数据表sys_dict_data
CREATE TABLE `sys_dict_type` ( `id` CHAR (19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '标识', `type_name` VARCHAR (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典类型名称', `type_code` VARCHAR (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典类型标识', `description` VARCHAR (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典类型描述', `enable` CHAR (1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '是否启用', `create_by` CHAR (19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人ID', `create_name` CHAR (19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人名称', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_by` CHAR (19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人ID', `update_name` CHAR (19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人名称', `update_time` datetime DEFAULT NULL COMMENT '修改时间', `remark` VARCHAR (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`) USING BTREE ) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC CREATE TABLE `sys_dict_data` ( `data_id` CHAR (19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '标识', `data_label` CHAR (19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典标签', `data_value` CHAR (20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典值', `type_code` CHAR (20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '所属类型', `is_default` CHAR (1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '是否默认', `update_by` CHAR (19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人ID', `update_name` CHAR (19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人名称', `update_time` datetime DEFAULT NULL COMMENT '修改时间', `create_by` VARCHAR (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人ID', `create_name` CHAR (19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人名称', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `remark` VARCHAR (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注', `enable` CHAR (1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '是否启用', PRIMARY KEY (`data_id`) USING BTREE ) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC
在项目中创建相应的实体类
通过观察SQL语句发现两个表中有很多相同的字段,所有我们把相同的字段抽离出来,放到一个基础的实体类中,其他实体类通过集成方式获取公共的字段。
创建基础实体类
package com.didiplus.common.base; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.TableField; import lombok.Data; import java.io.Serializable; import java.time.LocalDateTime; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/29 * Desc: 基 础 实 体 类 */ @Data public class BaseDomain implements Serializable { /** * 创建时间 */ @TableField(value = "create_time", fill = FieldFill.INSERT) private LocalDateTime createTime; /** * 创建人 */ @TableField(value = "create_by", fill = FieldFill.INSERT) private String createBy; /** * 创建人名称 */ @TableField(value = "create_name", fill = FieldFill.INSERT) private String createName; /** * 修改时间 */ @TableField(value = "update_time", fill = FieldFill.UPDATE) private LocalDateTime updateTime; /** * 修改人 */ @TableField(value = "update_by", fill = FieldFill.UPDATE) private String updateBy; /** * 修改人名称 */ @TableField(value = "update_name", fill = FieldFill.UPDATE) private String updateName; /** * 备注 */ private String remark; }
@TableField 是mybatisplus中的一个注解,后面会讲解到的。
基础实体类创建好了,接着我们把剩下的两个实体类也一同创建吧。
SysDictType实体类
package com.didiplus.modules.sys.domain; import com.didiplus.common.base.BaseDomain; import com.didiplus.common.base.ValidGroup; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import javax.validation.constraints.*; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/25 * Desc: 字典类型领域模型 */ @Data @ApiModel(value = "字典类型") public class SysDictType extends BaseDomain { /** * 标识 */ @Null(groups = ValidGroup.Crud.Create.class) @NotNull(groups = ValidGroup.Crud.Update.class,message = "字典ID不能为空") @ApiModelProperty("ID") private String id; /** * 字典名称 */ @NotBlank(message = "字典名称必填项") @ApiModelProperty(value = "字典名称",example = "用户ID") private String typeName; /** * 字典类型 */ @NotBlank(message = "字典编码不能为空") @ApiModelProperty(value = "字典编码") private String typeCode; /** * 字典描述 */ @ApiModelProperty(value = "字典描述") private String description; /** * 字典可用状态 */ @NotBlank(message = "字典状态不能为空") @ApiModelProperty(value = "字典状态") private Boolean enable; }
SysDictData实体类
package com.didiplus.modules.sys.domain; import com.baomidou.mybatisplus.annotation.TableField; import com.didiplus.common.base.BaseDomain; import com.didiplus.common.base.ValidGroup; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import javax.validation.constraints.Null; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/29 * Desc: 字典值领域模型 */ @Data public class SysDictData extends BaseDomain { /** * id 编号 */ @Null(groups = ValidGroup.Crud.Create.class) @NotNull(groups = ValidGroup.Crud.Update.class,message = "字典数据ID不能为空") @ApiModelProperty("ID") @TableField(value = "data_id") private String dataId; /** * 字典显示 */ @NotBlank(message = "字典数据名称必填项") @ApiModelProperty(value = "字典数据名称") private String dataLabel; /** * 字典值 */ @NotBlank(message = "字典数据值不能为空") @ApiModelProperty(value = "字典数据值") private String dataValue; /** * 字典类型 */ @ApiModelProperty(value = "字典编码") @NotBlank(message = "字典数据值不能为空") private String typeCode; /** * 是否为默认 */ @ApiModelProperty(value = "字典编码") @NotBlank(message = "字典数据值不能为空") private String isDefault; /** * 是否启用 */ @NotBlank(message = "字典状态不能为空") @ApiModelProperty(value = "字典数据状态") private Boolean enable; }
创建DAO继承MybatisPlus增强接口
为两个实体类中分别添加DAO继承MybatisPlus增强接口,这样就可以集成了增删改查的功能了。
SysDictTypeMapper
package com.didiplus.modules.sys.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.didiplus.modules.sys.domain.SysDictType; import org.apache.ibatis.annotations.Mapper; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/29 * Desc: 字典类型接口 */ @Mapper public interface SysDictTypeMapper extends BaseMapper<SysDictType> { }
SysDictDataMapper
package com.didiplus.modules.sys.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.didiplus.modules.sys.domain.SysDictData; import org.apache.ibatis.annotations.Mapper; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/29 * Desc: 字典数据接口 */ @Mapper public interface SysDictDataMapper extends BaseMapper<SysDictData> { }
进一步封装到Service层
定义Service接口中的抽象方法
SysDictDataService
package com.didiplus.modules.sys.service; import com.baomidou.mybatisplus.extension.service.IService; import com.didiplus.modules.sys.domain.SysDictData; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/29 * Desc: */ public interface SysDictDataService extends IService<SysDictData> { }
SysDictTypeService
package com.didiplus.modules.sys.service; import com.baomidou.mybatisplus.extension.service.IService; import com.didiplus.modules.sys.domain.SysDictType; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/29 * Desc: */ public interface SysDictTypeService extends IService<SysDictType> { }
实现Service接口中的方法
SysDictTypeServiceImpl
package com.didiplus.modules.sys.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.didiplus.modules.sys.domain.SysDictType; import com.didiplus.modules.sys.mapper.SysDictTypeMapper; import com.didiplus.modules.sys.service.SysDictTypeService; import org.springframework.stereotype.Service; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/29 * Desc: */ @Service public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService { }
SysDictDataServiceImpl
package com.didiplus.modules.sys.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.didiplus.modules.sys.domain.SysDictType; import com.didiplus.modules.sys.mapper.SysDictTypeMapper; import com.didiplus.modules.sys.service.SysDictTypeService; import org.springframework.stereotype.Service; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/29 * Desc: */ @Service public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService { }
在控制层上进行增删改查操作
SysDictTypeController
package com.didiplus.modules.sys.controller; import com.didiplus.common.base.ValidGroup; import com.didiplus.modules.sys.domain.SysDictType; import com.didiplus.modules.sys.service.SysDictTypeService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.v3.oas.annotations.parameters.RequestBody; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/25 * Desc: 数据字典控制器 */ @RestController @Api(tags = "数据字典") @RequestMapping("/api/sys/dictType") public class SysDictTypeController { @Autowired SysDictTypeService sysDictTypeService; @ApiOperation("字典添加") @PostMapping("/add") public String add(@Validated(value = ValidGroup.Crud.Create.class) @RequestBody SysDictType sysDictType) { return sysDictTypeService.save(sysDictType)? "添加成功":"添加失败"; } @ApiOperation("字典修改") @PutMapping("/edit") public String edit(@Validated(value = ValidGroup.Crud.Update.class) @RequestBody SysDictType sysDictType) { return sysDictTypeService.updateById(sysDictType)? "修改成功":"修改失败"; } @ApiOperation("字典删除") @DeleteMapping("/del/{id}") public String del( @PathVariable String id) { return sysDictTypeService.removeById(id)? "删除成功":"删除失败"; } }
体验效果
新增数据
修改数据
删除数据
自动填充功能
原理:
- 实现元对象处理器接口:
com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
- 注解填充字段
@TableField(.. fill = FieldFill.INSERT)
生成器策略部分也可以配置!
自定义实现类 DomainInterceptor
package com.didiplus.common.web.interceptor; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.time.LocalDateTime; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/5/4 * Desc: 字 段 填 充 拦 截 器 */ @Component public class DomainInterceptor implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { createField(metaObject); } @Override public void updateFill(MetaObject metaObject) { updateField(metaObject); } /** * @Field 创建时间 * */ public void createField(MetaObject metaObject){ this.strictInsertFill(metaObject,"createTime", LocalDateTime.class,LocalDateTime.now()); } /** * @Field 修改时间 * */ public void updateField(MetaObject metaObject) { this.strictUpdateFill(metaObject,"updateTime",LocalDateTime.class,LocalDateTime.now()); } }
分页查询
添加分页插件
package com.didiplus.common.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/5/4 * Desc: mybatis-plus分页插件 */ @Configuration public class MyBatisPlusConfig { /** * mybatis-plus分页插件 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
定义分页接口
package com.didiplus.modules.sys.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.didiplus.common.web.domain.PageDomain; import com.didiplus.modules.sys.domain.SysDictType; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/29 * Desc: 数据字典类型服务类 */ public interface SysDictTypeService extends IService<SysDictType> { IPage<SysDictType> page(PageDomain pageDomain); }
实现分页接口
package com.didiplus.modules.sys.service.impl; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.didiplus.common.web.domain.PageDomain; import com.didiplus.modules.sys.domain.SysDictType; import com.didiplus.modules.sys.mapper.SysDictTypeMapper; import com.didiplus.modules.sys.service.SysDictTypeService; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/29 * Desc: */ @Service public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService { @Resource SysDictTypeMapper sysDictTypeMapper; @Override public IPage<SysDictType> page(PageDomain pageDomain) { IPage<SysDictType> page = new Page<>(pageDomain.getPage(),pageDomain.getLimit()); return sysDictTypeMapper.selectPage(page,null); } }
控制层调用
@RestController @Api(tags = "数据字典") @RequestMapping("/api/sys/dictType") public class SysDictTypeController { @Autowired SysDictTypeService sysDictTypeService; @ApiOperation("字典分页查询") @GetMapping public IPage list(@RequestBody PageDomain pageDomain){ return sysDictTypeService.page(pageDomain); } }
PageDomain定义了分页接收的两个参数
PageDomain类
package com.didiplus.common.web.domain; import lombok.Data; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/5/4 * Desc: 分 页 参 数 封 装 */ @Data public class PageDomain { /** * 当前页 */ private Integer page; /** * 每页数量 */ private Integer limit; }
体验效果
下一篇,我们继续来学习mybatisPlus的代码生成器