SpringBoot 集成 MyBatis Plus
- 前言
- 修改 pom.xml
- 修改配置文件
- 添加 实体类
- 添加 持久层接口
- 添加 持久层 XxxMapper.xml 文件
- 添加 业务接口层
- 添加 业务接口实现类
- 添加 控制层
- 添加 MyBatis 配置
- AutoFillMetaObjectHandler
- MyBatisPlusConfig
- 验证
前言
由于 MySQL 备份/恢复测试,模拟应用数据实时写入数据库的场景。
现集成 MyBatis Plus
做于笔记。
当前项目是以《SpringBoot 集成 Druid》基础进行持续集成的。
持续集成 MyBatis Plus
完成之后的项目截图如下:
客户表数据参考《MySQL数据库-创建测试数据》,表结构如下:
-- 1 -* 客户表
create table client(
id bigint not null primary key auto_increment comment '自增ID',
client_id bigint not null comment 'ID编号',
user_no varchar(30) comment '账号',
user_password varchar(60) comment '密码',
nick_name varchar(30) comment '昵称',
real_name varchar(30) comment '真实姓名',
created_time datetime default now() comment '创建时间',
upated_time datetime default now() comment '更新时间'
);
修改 pom.xml
在《SpringBoot 集成 Druid》章节中的 pom.xml
文件已经将 MyBatis Plus
的依赖以配置完成。
<!-- mybatis-plus 依赖
https://baomidou.com/getting-started/
<mybatis.plus.boot.starter.version>3.3.1</mybatis.plus.boot.starter.version>
-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version> ${mybatis.plus.boot.starter.version} </version>
</dependency>
修改配置文件
在 application.yml
文件中,添加以下配置:
# mybatis plus 配置
mybatis-plus:
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
mapper-locations: classpath*:/mapper/*Mapper.xml
type-aliases-package: com.junjiu.springboot.druid.entity
完整配置如下:
# 端口
server:
port: 5826
spring:
application:
# 应用名称
name: junjiu-springboot-druid
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.88.54:3306/ideadb?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false
username: fid_idea
password: 123456
initial-size: 10
max-active: 100
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
max-evictable-idle-time-millis: 600000
validation-query: SELECT 1 FROM DUAL
# validation-query-timeout: 5000
test-on-borrow: false
test-on-return: false
test-while-idle: true
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#filters: #配置多个英文逗号分隔(统计,sql注入,log4j过滤)
filters: stat,wall
stat-view-servlet:
enabled: true
url-pattern: /druid/*
# 监控页面配置.
jj:
druid:
monitor:
login-username: root
login-password: 123456
reset-enable: false
# mybatis plus 配置
mybatis-plus:
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
mapper-locations: classpath*:/mapper/*Mapper.xml
type-aliases-package: com.junjiu.springboot.druid.entity
添加 实体类
package com.junjiu.springboot.druid.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* program: junjiu-springboot-druid
* ClassName: ClientEntity
* description:
*
* @author: 君九
* @create: 2024-06-12 22:55
* @version: 1.0
**/
@Data
@TableName("client")
public class ClientEntity implements Serializable {
/**
* -- 1 -* 客户表
* create table client(
* id bigint not null primary key auto_increment comment '自增ID',
* client_id bigint not null comment 'ID编号',
* user_no varchar(30) comment '账号',
* user_password varchar(60) comment '密码',
* nick_name varchar(30) comment '昵称',
* real_name varchar(30) comment '真实姓名',
* created_time datetime default now() comment '创建时间',
* upated_time datetime default now() comment '更新时间'
* );
*/
private Long id;
private Long clientId;
private String userNo;
private String userPassword;
private String nickName;
private String realName;
private Date createdTime;
private Date updatedTime;
}
添加 持久层接口
package com.junjiu.springboot.druid.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.junjiu.springboot.druid.entity.ClientEntity;
import org.apache.ibatis.annotations.Mapper;
/**
* program: junjiu-springboot-druid
* ClassName: ClientMapper.xml
* description:
*
* @author: 君九
* @create: 2024-06-12 22:57
* @version: 1.0
**/
@Mapper
public interface ClientMapper extends BaseMapper<ClientEntity> {
}
添加 持久层 XxxMapper.xml 文件
此处文件名称定义为 ClientMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.junjiu.springboot.druid.mapper.ClientMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.junjiu.springboot.druid.entity.ClientEntity">
<id column="id" property="id" />
<result column="client_id" property="clientId" />
<result column="user_no" property="userNo" />
<result column="user_password" property="userPassword" />
<result column="nick_name" property="nickName" />
<result column="real_name" property="realName" />
<result column="created_time" property="createdTime" />
<result column="updated_time" property="updatedTime" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, client_id, user_no, user_password, nick_name, real_name, created_time, updated_time
</sql>
</mapper>
添加 业务接口层
package com.junjiu.springboot.druid.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.junjiu.springboot.druid.entity.ClientEntity;
/**
* program: junjiu-springboot-druid
* ClassName: ClientService
* description:
* 客户端业务接口.
*
* @author: 君九
* @create: 2024-06-12 22:57
* @version: 1.0
**/
public interface ClientService extends IService<ClientEntity> {
}
添加 业务接口实现类
package com.junjiu.springboot.druid.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.junjiu.springboot.druid.entity.ClientEntity;
import com.junjiu.springboot.druid.mapper.ClientMapper;
import com.junjiu.springboot.druid.service.ClientService;
import org.springframework.stereotype.Service;
/**
* program: junjiu-springboot-druid
* ClassName: ClientServiceImpl
* description:
* 客户业务实现类.
*
* @author: 君九
* @create: 2024-06-12 22:58
* @version: 1.0
**/
@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, ClientEntity> implements ClientService {
}
添加 控制层
package com.junjiu.springboot.druid.controller;
import com.junjiu.springboot.druid.entity.ClientEntity;
import com.junjiu.springboot.druid.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* program: junjiu-springboot-druid
* ClassName: ClientController
* description:
*
* @author: 君九
* @create: 2024-06-12 22:59
* @version: 1.0
**/
@RestController
@RequestMapping("/client")
public class ClientController {
@Autowired
private ClientService clientService;
/**
* 测试添加数据.
* @return
*/
@RequestMapping("/testAdd")
public String testAdd() {
ClientEntity clientEntity = new ClientEntity();
clientEntity.setClientId(1001L);
clientEntity.setNickName("九皇叔叔");
clientEntity.setRealName("君九");
clientEntity.setUserNo("JunJiu");
clientEntity.setUserPassword("123456");
clientService.save(clientEntity);
return "Success.";
}
}
添加 MyBatis 配置
说明:
当前配置主要两个方面:
- 分页配置:Table表格展示数据时的分页功能。
- 自动填充配置:
添加
|修改
|逻辑删除
时需更新时间的功能。
AutoFillMetaObjectHandler
package com.junjiu.springboot.druid.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
/**
* program: junjiu-springboot-druid
* ClassName: AutoFillMetaObjectHandler
* description:
*
* @author: 君九
* @create: 2024-06-12 22:52
* @version: 1.0
**/
@Component
public class AutoFillMetaObjectHandler implements MetaObjectHandler {
/**
* 新增数据时,自动填充创建时间+修改时间 为当前时间。
* @param metaObject
*/
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createdTime", LocalDateTime.class, LocalDateTime.now());
this.strictInsertFill(metaObject, "updatedTime", LocalDateTime.class, LocalDateTime.now());
}
/**
* 更新数据时,自动填充创建时间+修改时间 为当前时间。
* @param metaObject
*/
@Override
public void updateFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "updatedTime", LocalDateTime.class, LocalDateTime.now());
}
}
MyBatisPlusConfig
package com.junjiu.springboot.druid.config;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* program: junjiu-springboot-druid
* ClassName: MyBatisPlusConfig
* description:
*
* @author: 君九
* @create: 2024-06-12 22:49
* @version: 1.0
**/
@Configuration
public class MyBatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
/**
* 自动填充功能
* @return
*/
@Bean
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setMetaObjectHandler(new AutoFillMetaObjectHandler());
return globalConfig;
}
}
验证
- 启动项目
- 进入到如下类中,鼠标右键 Run 启动即可。
- 在浏览器访问:http://localhost:5826/client/testAdd
- 查询数据库