SpringBoot整合MybatisPlus实现增删改查功能
目录
- 1.概述
- 2.引入依赖
- 3.配置连接信息
- 4.新建两个表
- 5.在项目中创建相应的实体类
- 5.1 创建基础实体类
- 5.2 SysDictType实体类
- 5.3 SysDictData实体类
- 6.创建DAO继承MybatisPlus增强接口
- 6.1 SysDictTypeMapper
- 6.2 SysDictDataMapper
- 7.进一步封装到Service层
- 7.1 定义Service接口中的抽象方法
- 7.2 实现Service接口中的方法
- 8.在控制层上进行增删改查操作
- 8.1 SysDictTypeController
- 9.自动填充功能
- 9.1 自定义实现类 DomainInterceptor
- 10.分页查询
- 10.1 添加分页插件
- 10.2 定义分页接口
- 10.3 实现分页接口
- 10.4 控制层调用
- 10.5 体验效果
1.概述
MybatisPlus是国产的第三方插件, 它封装了许多常用的CURDapi,免去了我们写mapper.xml的重复劳动,这里介绍了基本的整合SpringBoot和基础用法。
2.引入依赖
在项目中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>
3.配置连接信息
在项目中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
4.新建两个表
本案例通过一个完整的业务流程来介绍如何使用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
5.在项目中创建相应的实体类
通过观察SQL语句发现两个表中有很多相同的字段,所有我们把相同的字段抽离出来,放到一个基础的实体类中,其他实体类通过集成方式获取公共的字段。
5.1 创建基础实体类
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中的一个注解,后面会讲解到的。
基础实体类创建好了,接着我们把剩下的两个实体类也一同创建吧。
5.2 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; }
5.3 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; }
6.创建DAO继承MybatisPlus增强接口
为两个实体类中分别添加DAO继承MybatisPlus增强接口,这样就可以集成了增删改查的功能了。
6.1 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> { }
6.2 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> { }
7.进一步封装到Service层
7.1 定义Service接口中的抽象方法
1.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> { }
2.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> { }
7.2 实现Service接口中的方法
1.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 { }
2.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 { }
8.在控制层上进行增删改查操作
8.1 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)? "删除成功":"删除失败"; } }
体验效果
新增数据
修改数据
删除数据
9.自动填充功能
原理:
- 实现元对象处理器接口:
com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
- 注解填充字段
@TableField(.. fill = FieldFill.INSERT)
生成器策略部分也可以配置!
9.1 自定义实现类 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()); } }
10.分页查询
10.1 添加分页插件
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; } }
10.2 定义分页接口
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); }
10.3 实现分页接口
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); } }
10.4 控制层调用
@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; }
10.5 体验效果
以上就是SpringBoot整合MybatisPlus实现增删改查功能的详细内容,更多关于SpringBoot MybatisPlus增删改查的资料请关注我们其它相关文章!
赞 (0)