一、mybatisplus是什么?
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
二、整合mybatisplus的步骤
1.引入相关jar包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
此外还需要引入其他相关jar包
2.完整pom文件内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatisplus</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.application配置文件内容
spring:
datasource:
# 数据源基本配置
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://101.34.49.127:3306/springBootAll?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
# mysql5 不需要声明serverTimezone=GMT%2B8& 时区
# mysql8 需要声明serverTimezone=GMT%2B8& 时区
server:
port: 8082 # 配置项目启动端口
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #控制台打印日志
4.项目主方法内容
@SpringBootApplication
@MapperScan("com.example.demo.dao") //dao接口位置
public class SpringbootMybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisplusApplication.class, args);
}
}
5.dao接口层内容
//此处继承BaseMapper实现mybatisplus的基础操作
public interface UserDao extends BaseMapper<User> {
}
6.service服务处方法
@Service
public class UserService {
@Resource
UserDao userDao;
public User findUserById(Integer userId){
return userDao.selectById(userId);
}
public List<User> selectUserList(){
return userDao.selectList(null);
}
public void insertUser(User user){
userDao.insert(user);
}
public void updateUser(User user){
userDao.updateById(user);
}
public List<User> findUserListByQuery(User user){
//查询条件
QueryWrapper<User> wrapper = new QueryWrapper<>();
//模糊查询
wrapper.like("user_name",user.getUserName());
wrapper.like("idCardNumber",user.getIdCardNumber());
return userDao.selectList(wrapper);
}
}
7.User实体类
@TableName("user")
@Data
public class User {
@TableId(type = IdType.ASSIGN_ID)
private Integer id;
@TableField("user_name")
private String userName;
@TableField("idCardNumber")
private String idCardNumber;
@TableField("phone_number")
private String phoneNumber;
@TableField("dept_id")
private Integer deptId;
/**
* 新增时填入时间
*/
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**
* 新增或修改时填入数据
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
注意:
@KeySequence(value = "SEQ_TEMPLATE_PARAM") 注解中是当主键id的生成策略为序列时使用
@TableFiled(exist = false) 说明此字段不为表中字段
8.插件配置类
/**
* @description:插件配置类
* @author: xiaYZ
* @createDate: 2022/2/3
* @version: 1.0
*/
@Component
public class MyHandle implements MetaObjectHandler {
/**
* 新增时填充策略
*/
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime",new Date(),metaObject);
this.setFieldValByName("updateTime",new Date(),metaObject);
}
/**
* 修改时填充策略
*/
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime",new Date(),metaObject);
}
}
9. Controller控制层内容
/**
* @description:
* @author: xiaYZ
* @createDate: 2022/2/2
* @version: 1.0
*/
@Controller
@RequestMapping("userController")
public class UserController {
@Resource
UserService userService;
/***
* description: findUserById
* version: 1.0 ->
* date: 2022/2/2 10:37
* author: xiaYZ
* iteration: 迭代说明
* @param userId
* @return java.lang.String
*/
@ResponseBody
@RequestMapping("findUserById")
@Description("查询用户信息通过id")
public String findUserById(Integer userId){
User user = new User();
try{
user = userService.findUserById(userId);
}catch (Exception e){
e.printStackTrace();
}
return user.toString();
}
/***
* description: insertUser
* version: 1.0 ->
* date: 2022/2/3 11:32
* author: xiaYZ
* iteration: 迭代说明
* @param user
* @return java.lang.String
*/
@ResponseBody
@PostMapping("insertUser")
@Description("新增用户")
public String insertUser(User user){
try{
userService.insertUser(user);
}catch (Exception e){
e.printStackTrace();
return "新增用户失败";
}
return "新增用户成功";
}
/****
* description: updateUser
* version: 1.0 ->
* date: 2022/2/3 11:54
* author: xiaYZ
* iteration: 迭代说明
* @param user
* @return java.lang.String
*/
@ResponseBody
@PutMapping("updateUser")
@Description("修改用户")
public String updateUser(User user){
try{
userService.updateUser(user);
}catch (Exception e){
e.printStackTrace();
return "修改用户失败";
}
return "修改用户成功";
}
/*****
* description: findUserListByQuery
* version: 1.0 ->
* date: 2022/2/7 14:01
* author: xiaYZ
* iteration: 迭代说明
* @param user
* @return java.util.List<com.example.demo.entity.User>
*/
@ResponseBody
@RequestMapping("findUserListByQuery")
@Description("条件查询")
public List<User> findUserListByQuery(User user){
List<User> list = new ArrayList<>();
try{
list = userService.findUserListByQuery(user);
}catch (Exception e){
e.printStackTrace();
}
return list;
}
}
三、项目运行截图
总结
- 此次项目只是简单的springBoo整合mybatisplus,只涉及简单的CRUD操作
- 更多详细功能操作请查询官网mybatisPlus