本篇文章知识点:
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=true
url: ${spring.datasource.jdbc-url}
username: xxxx
password: xxxxx
idle-timeout: 60000
max-lifetime: 72000
connection-timeout: 3200
validation-timeout: 1000
maximum-pool-size: 20
minimum-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();
}
@Autowired
public 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> {
}
Service 实现层 @Service(“iUserInfoService”) 名字必须定义因为 controller 通过构造器注入
@Service("iUserInfoService")
public class IUserInfoServiceImpl extends BaseServiceImpl<UserInfoMapper, UserInfoPO> implements IUserInfoService {
}
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;
}