SpringBoot整合MybatisPlus基本的增删改查,保姆级教程( 三 )

SysDictDataServiceImplpackage 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: */@Servicepublic class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {}8|0在控制层上进行增删改查操作8|1SysDictTypeControllerpackage 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 {@AutowiredSysDictTypeService sysDictTypeService;@ApiOperation("字典添加")@PostMapping("/add")public String add(@Validated(value = https://www.isolves.com/it/cxkf/jiagou/2022-05-05/ValidGroup.Crud.Create.class) @RequestBody SysDictType sysDictType) {returnsysDictTypeService.save(sysDictType)? "添加成功":"添加失败";}@ApiOperation("字典修改")@PutMapping("/edit")public String edit(@Validated(value = ValidGroup.Crud.Update.class) @RequestBody SysDictType sysDictType) {returnsysDictTypeService.updateById(sysDictType)? "修改成功":"修改失败";}@ApiOperation("字典删除")@DeleteMapping("/del/{id}")publicString del( @PathVariable String id) {returnsysDictTypeService.removeById(id)? "删除成功":"删除失败";}}体验效果新增数据

SpringBoot整合MybatisPlus基本的增删改查,保姆级教程

文章插图
 

SpringBoot整合MybatisPlus基本的增删改查,保姆级教程

文章插图
 
修改数据
SpringBoot整合MybatisPlus基本的增删改查,保姆级教程

文章插图
 

SpringBoot整合MybatisPlus基本的增删改查,保姆级教程

文章插图
 
删除数据
SpringBoot整合MybatisPlus基本的增删改查,保姆级教程

文章插图
 
9|0自动填充功能原理:
  • 实现元对象处理器接口:com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
  • 注解填充字段 @TableField(.. fill = FieldFill.INSERT)生成器策略部分也可以配置!
9|1自定义实现类 DomainInterceptorpackage 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: 字 段 填 充 拦 截 器 */@Componentpublic class DomainInterceptor implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {createField(metaObject);}@Overridepublic 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|0分页查询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分页插件 */@Configurationpublic class MyBatisPlusConfig {/*** mybatis-plus分页插件*/@Beanpublic MybatisPlusInterceptormybatisPlusInterceptor() {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);}


推荐阅读