参考文档
springboot整合Mybatis-plus
1.添加依赖
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--hutool工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.4</version>
</dependency>
<!--springboot2.X整合mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
<!--代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.7</version>
</dependency>
<!--必须添加该jar包,官方好像没有添加,不然会报错-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<!--此jar包用于保证重新mybatis-plus核心代码不会报错-->
<dependency>
<groupId>org.realityforge.org.jetbrains.annotations</groupId>
<artifactId>org.jetbrains.annotations</artifactId>
<version>1.7.0</version>
</dependency>
2.配置application.yml
# 应用服务 WEB 访问端口
server:
port: 8080
# 配置数据源
spring:
datasource:
url: jdbc:mysql://192.168.211.131:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
# 配置MybatisPlus
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
auto-mapping-behavior: full
mapper-locations: classpath*:mapper/*.xml
3.添加全局配置文件,启用分页插件(选配)
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInnerInterceptor paginationInterceptor() {
return new PaginationInnerInterceptor();
}
}
4.启动类添加包扫描
@SpringBootApplication
@MapperScan("com.mybatisplus.mapper")
public class SpringbootMybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisplusApplication.class, args);
}
}
5.添加代码生成器工具类
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
/**
* mybatisplus代码生成器
*/
public class GeneratorCodeConfig {
/**
* 数据库连接信息
*/
public static final String AUTHOR = "weiyiji";
public static final String URL = "jdbc:mysql://192.168.211.131:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
public static final String USERNAME = "root";
public static final String PASSWORD = "123456";
/**
* 指定表名
*/
public static final String[] TABLENAME = {"f_file"};
/**
* 表前缀
*/
public static final String[] TABLE_PREFIX = {"f_"};
public static void main(String[] args) {
FastAutoGenerator.create(URL, USERNAME, PASSWORD)
.globalConfig(builder -> builder
.author(AUTHOR)
.outputDir("D://src")
)
.packageConfig(builder -> builder
.parent("com.mybatisplus")
.entity("entity")
.mapper("mapper")
.service("service")
.controller("controller")
.serviceImpl("service.impl")
.xml("mapper")
)
.strategyConfig(builder -> {
builder.addInclude(TABLENAME)
.addTablePrefix(TABLE_PREFIX)
.entityBuilder()
.enableLombok()
.controllerBuilder()
.enableRestStyle();
}
)
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
}
提示:到此处就可以正常使用了,由于我本人有强迫症,所以我将生产的接口前的I去掉了,如果和我一样需要去掉的可以再往下进行配置
6.修改代码生成器核心类,调整服务层接口命名规则
6.1创建包名(必须是这个包名,这样类加载器才会加载我们的这个类)
com.baomidou.mybatisplus.generator.config.builder
6.2创建核心配置类
package com.baomidou.mybatisplus.generator.config.builder;
/**
* 重写mybatis-plus代码生成器代码,去掉生成的service接口的前缀I
*
* @author wyj
* @date 2024/6/24 14:00
*/
import com.baomidou.mybatisplus.generator.ITemplate;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.function.ConverterFileName;
import com.baomidou.mybatisplus.generator.util.ClassUtils;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* Service属性配置
*
* @author nieqiurong 2020/10/11.
* @since 3.5.0
*/
public class Service implements ITemplate {
private final static Logger LOGGER = LoggerFactory.getLogger(Service.class);
private Service() {
}
/**
* 是否生成serviceImpl
*
* @since 3.5.6
*/
@Getter
private boolean generateServiceImpl = true;
/**
* 是否生成service
*
* @since 3.5.6
*/
@Getter
private boolean generateService = true;
/**
* @since 3.5.6
*/
@Getter
private String serviceTemplate = ConstVal.TEMPLATE_SERVICE;
/**
* @since 3.5.6
*/
@Getter
private String serviceImplTemplate = ConstVal.TEMPLATE_SERVICE_IMPL;
/**
* 自定义继承的Service类全称,带包名
*/
private String superServiceClass = ConstVal.SUPER_SERVICE_CLASS;
/**
* 自定义继承的ServiceImpl类全称,带包名
*/
private String superServiceImplClass = ConstVal.SUPER_SERVICE_IMPL_CLASS;
@NotNull
public String getSuperServiceClass() {
return superServiceClass;
}
@NotNull
public String getSuperServiceImplClass() {
return superServiceImplClass;
}
/**
* 转换输出Service文件名称
*
* @since 3.5.0
*/
private ConverterFileName converterServiceFileName = (entityName -> entityName + ConstVal.SERVICE);
/**
* 转换输出ServiceImpl文件名称
*
* @since 3.5.0
*/
private ConverterFileName converterServiceImplFileName = (entityName -> entityName + ConstVal.SERVICE_IMPL);
/**
* 是否覆盖已有文件(默认 false)
*
* @since 3.5.2
*/
@Getter
private boolean fileOverride;
@NotNull
public ConverterFileName getConverterServiceFileName() {
return converterServiceFileName;
}
@NotNull
public ConverterFileName getConverterServiceImplFileName() {
return converterServiceImplFileName;
}
@Override
@NotNull
public Map<String, Object> renderData(@NotNull TableInfo tableInfo) {
Map<String, Object> data = new HashMap<>();
data.put("superServiceClassPackage", this.superServiceClass);
data.put("superServiceClass", ClassUtils.getSimpleName(this.superServiceClass));
data.put("superServiceImplClassPackage", this.superServiceImplClass);
data.put("superServiceImplClass", ClassUtils.getSimpleName(this.superServiceImplClass));
data.put("generateServiceImpl", this.generateServiceImpl);
data.put("generateService", this.generateService);
return data;
}
public static class Builder extends BaseBuilder {
private final Service service = new Service();
public Builder(@NotNull StrategyConfig strategyConfig) {
super(strategyConfig);
}
/**
* Service接口父类
*
* @param clazz 类
* @return this
*/
public Builder superServiceClass(@NotNull Class<?> clazz) {
return superServiceClass(clazz.getName());
}
/**
* Service接口父类
*
* @param superServiceClass 类名
* @return this
*/
public Builder superServiceClass(@NotNull String superServiceClass) {
this.service.superServiceClass = superServiceClass;
return this;
}
/**
* Service实现类父类
*
* @param clazz 类
* @return this
*/
public Builder superServiceImplClass(@NotNull Class<?> clazz) {
return superServiceImplClass(clazz.getName());
}
/**
* Service实现类父类
*
* @param superServiceImplClass 类名
* @return this
*/
public Builder superServiceImplClass(@NotNull String superServiceImplClass) {
this.service.superServiceImplClass = superServiceImplClass;
return this;
}
/**
* 转换输出service接口文件名称
*
* @param converter 转换处理
* @return this
* @since 3.5.0
*/
public Builder convertServiceFileName(@NotNull ConverterFileName converter) {
this.service.converterServiceFileName = converter;
return this;
}
/**
* 转换输出service实现类文件名称
*
* @param converter 转换处理
* @return this
* @since 3.5.0
*/
public Builder convertServiceImplFileName(@NotNull ConverterFileName converter) {
this.service.converterServiceImplFileName = converter;
return this;
}
/**
* 格式化service接口文件名称
*
* @param format 格式
* @return this
* @since 3.5.0
*/
public Builder formatServiceFileName(@NotNull String format) {
return convertServiceFileName((entityName) -> String.format(format, entityName));
}
/**
* 格式化service实现类文件名称
*
* @param format 格式
* @return this
* @since 3.5.0
*/
public Builder formatServiceImplFileName(@NotNull String format) {
return convertServiceImplFileName((entityName) -> String.format(format, entityName));
}
/**
* 覆盖已有文件(该方法后续会删除,替代方法为enableFileOverride方法)
*
* @see #enableFileOverride()
*/
@Deprecated
public Builder fileOverride() {
LOGGER.warn("fileOverride方法后续会删除,替代方法为enableFileOverride方法");
this.service.fileOverride = true;
return this;
}
/**
* 覆盖已有文件
*/
public Builder enableFileOverride() {
this.service.fileOverride = true;
return this;
}
/**
* 禁用生成Service
*
* @return this
* @since 3.5.6
*/
public Builder disable() {
this.service.generateService = false;
this.service.generateServiceImpl = false;
return this;
}
/**
* 禁用生成
*
* @return this
* @since 3.5.6
*/
public Builder disableService() {
this.service.generateService = false;
return this;
}
/**
* 禁用生成ServiceImpl
*
* @return this
* @since 3.5.6
*/
public Builder disableServiceImpl() {
this.service.generateServiceImpl = false;
return this;
}
/**
* Service模板路径
*
* @return this
* @since 3.5.6
*/
public Builder serviceTemplate(@NotNull String template) {
this.service.serviceTemplate = template;
return this;
}
/**
* ServiceImpl模板路径
*
* @return this
* @since 3.5.6
*/
public Builder serviceImplTemplate(@NotNull String template) {
this.service.serviceImplTemplate = template;
return this;
}
@NotNull
public Service get() {
return this.service;
}
}
}