当前位置: 首页 > news >正文

告别手写CRUD通过Springboot+mybatis-plug 实现任意表Crud从Controller开始不需要手写一行代码

本篇文章知识点:
Mybatis-plugs
Springboot
java基础 封装 继承
泛型类的使用和扩展

各位童靴是不是在开发工作中很苦恼每次做需求新加了一张表后要从新写crud的基础逻辑下面 就教大家如何从Controller到mapper 不写一行代码 实现CRUD

首先 引入依赖

  <!-- MyBatis-Plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.0</version> <!-- 请使用最新版本 --></dependency><!-- MySQL 驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>

配置文件

spring:datasource:# MySql 8.0以上版本driver-class-name: com.mysql.cj.jdbc.Driver# 兼容以前的配置jdbc-url: jdbc:mysql://xxxxxxx:3306/xxxx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&useTimezone=true&serverTimezone=GMT%2B8&allowMultiQueries=trueurl: ${spring.datasource.jdbc-url}username: xxxxpassword: xxxxxidle-timeout: 60000max-lifetime: 72000connection-timeout: 3200validation-timeout: 1000maximum-pool-size: 20minimum-idle: 5

然后 分别 写 BaseController BaseService MyBaseMapper三个类

先写 MyBaseMapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;public interface MyBaseMapper<T> extends BaseMapper<T> {}

继续写 BaseService

import com.baomidou.mybatisplus.extension.service.IService;public interface BaseService<T> extends IService<T> {// 可以添加一些自定义的服务方法
}

然后写BaseServiceImpl


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.indeep.user.mapper.IndeepBaseMapper;
import com.indeep.user.mapper.po.BaseEntity;
import org.springframework.stereotype.Service;public class BaseServiceImpl<M extends MyBaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {}

最后定义一个BaseController

import com.indeep.user.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/base")
@Api(value = "base", description = "base相关的API")
public class BaseController<T, S extends BaseService<T>> {protected S service;@GetMapping("/list")@ApiOperation(value = "获取列表", notes = "获取列表")public List<T> list() {return service.list();}@Autowiredpublic BaseController(@Qualifier("iPeopleService") S service) {this.service = service;}@PostMapping("/save")@ApiOperation(value = "新增数据", notes = "新增数据")public boolean save(@RequestBody T entity) {if (entity == null) {//  throw new HttpMessageNotReadableException("请求体不能为空", null);}return service.save(entity);}@PutMapping("/update")@ApiOperation(value = "根据id修改数据", notes = "根据id修改数据")public boolean update(@RequestBody T entity) {return service.updateById(entity);}@DeleteMapping("/delete/{id}")@ApiOperation(value = "根据id删除数据", notes = "根据id删除数据")public boolean delete(@PathVariable Long id) {return service.removeById(id);}@GetMapping("/get/{id}")@ApiOperation(value = "根据id获取数据", notes = "根据id删除数据")public T getById(@PathVariable Long id) {return service.getById(id);}
}

接下来 我们看看从controller 到 mapper 的代码

Controller 层

import com.indeep.user.controller.base.BaseController;
import com.indeep.user.mapper.po.UserInfoPO;
import com.indeep.user.service.IUserInfoService;
import com.indeep.user.vo.UserInfoVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/user")
@Api(value = "用户管理", description = "用户管理相关的API")
public class UserInfoController  extends BaseController<UserInfoPO, IUserInfoService> {public UserInfoController(@Qualifier("iUserInfoService") IUserInfoService service) {super(service);}
}

Service 层

import com.indeep.user.mapper.po.UserInfoPO;
import com.indeep.user.service.base.BaseService;public interface IUserInfoService extends BaseService<UserInfoPO> {
}

Mapper 层

import com.indeep.user.mapper.po.UserInfoPO;public interface UserInfoMapper extends MyBaseMapper<UserInfoPO> {
}

提醒下
所有的数据库层实体类 都需要继承BaseEntity BaseEntity 中可以是表的共有属性 比如 创建时间 修改时间 创建人 修改人等
另外就是Service必须是在@Service中写上命名 因为 在Controller 层需要再构造器里面 传参数
@Qualifier(“iUserInfoService”)

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;import java.io.Serializable;
import java.util.Date;@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@TableName("indeep_people")
public class PeoplePO extends BaseEntity implements Serializable {private static final long serialVersionUID = -1L;/*** 用户id*/@TableId(type = IdType.AUTO)private Long id;private String userName;private String password;}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • sql注入实战——thinkPHP
  • Unity入门3——脚本入门
  • 基于SpringBoot+Vue社团管理系统的设计与实现
  • cisp-pte考试复盘
  • C# 根据MySQL数据库中数据,批量删除OSS上的垃圾文件
  • Linux(离线)内网部署 thingsboard-gateway 网关实战modbus通讯
  • 【docker快捷部署系列二】用docker-compose快速配置多个容器,docker部署Springboot+Vue项目和mysql数据库
  • Python | Leetcode Python题解之第329题矩阵中的最长递增路径
  • 手写Redis缓存系统,第三章:持久化-增加可靠性
  • SuccBI+低代码文档中心 — 可视化分析(仪表板)(下)
  • Linux-Shell入门-05
  • 白骑士的Matlab教学实战项目篇 4.3 控制系统设计
  • 保研考研机试攻略:第一章——从零开始
  • 怎样才算精通 Excel?
  • linux之网络子系统-GRO机制分析
  • 【mysql】环境安装、服务启动、密码设置
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • extract-text-webpack-plugin用法
  • js数组之filter
  • magento2项目上线注意事项
  • October CMS - 快速入门 9 Images And Galleries
  • React的组件模式
  • Spring声明式事务管理之一:五大属性分析
  • Spring思维导图,让Spring不再难懂(mvc篇)
  • Vue 动态创建 component
  • Vultr 教程目录
  • 二维平面内的碰撞检测【一】
  • 给Prometheus造假数据的方法
  • 聊聊flink的BlobWriter
  • 前端面试之闭包
  • 微信公众号开发小记——5.python微信红包
  • 我感觉这是史上最牛的防sql注入方法类
  • 学习HTTP相关知识笔记
  • 一天一个设计模式之JS实现——适配器模式
  • #预处理和函数的对比以及条件编译
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (C#)一个最简单的链表类
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (二)windows配置JDK环境
  • (二)原生js案例之数码时钟计时
  • (十一)c52学习之旅-动态数码管
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • .NET : 在VS2008中计算代码度量值
  • .net 7 上传文件踩坑
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .NET 某和OA办公系统全局绕过漏洞分析
  • .NET 药厂业务系统 CPU爆高分析
  • .NET 中创建支持集合初始化器的类型
  • .NET/C# 的字符串暂存池
  • .NET和.COM和.CN域名区别
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • .sh
  • .xml 下拉列表_RecyclerView嵌套recyclerview实现二级下拉列表,包含自定义IOS对话框...
  • @Autowired和@Resource装配