<?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.5.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hrf</groupId>
<artifactId>blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>blog</name>
<description>blog</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!-- mybatis-plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<!-- MP代码生成器依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.0</version>
</dependency>
<!--freemarker模板引擎-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Shanghai
username: root
password: "0429"
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启SQL语句打印
# global-config:
# db-config:
# id-type: auto # 配置了全局采用自增主键策略
package com.hrf.blog.generator;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;
public class GeneratorTest {
@Test
public void generate() {
AutoGenerator generator = new AutoGenerator();
//1. 全局配置
GlobalConfig config = new GlobalConfig();
// 获取用户程序当前路径
String projectPath = System.getProperty("user.dir");
// 设置输出到的目录
config.setOutputDir(projectPath + "/src/main/java");
config.setAuthor("hrf");
// 生成结束后是否打开文件夹
config.setOpen(true);
// 是否覆盖已有文件
config.setFileOverride(true);
// 全局配置添加到 generator 上
generator.setGlobalConfig(config);
//2. 数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
// 数据库类型
// dataSourceConfig.setDbType(DbType.MYSQL);
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Shanghai");
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("0429");
// 数据源配置添加到 generator代码生成器主类
generator.setDataSource(dataSourceConfig);
//3. 包配置, 生成的代码放在哪个包下
PackageConfig packageConfig = new PackageConfig();
// packageConfig.setModuleName("module");// 父包模块名
packageConfig.setParent("com.hrf.blog");// 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
packageConfig.setService("service");// Service包名
packageConfig.setEntity("entity");// Entity包名
packageConfig.setServiceImpl("service.impl");// ServiceImpl包名
packageConfig.setMapper("mapper");// Mapper包名
packageConfig.setController("controller");// Contoller包名
packageConfig.setXml("mapper.xml");// Mapper.xml包名
// 包配置添加到 generator
generator.setPackageInfo(packageConfig);
//4. 策略配置 数据库表配置,通过该配置,可指定需要生成哪些表或者排除哪些表等信息,
StrategyConfig strategyConfig = new StrategyConfig();
// 下划线驼峰命名转换
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
// 数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
// 自定义继承类全称,带包名 (以下为示例)
// strategyConfig.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
// strategyConfig.setSuperControllerClass("com.baomidou.ant.common.BaseController");
strategyConfig.setEntityLombokModel(true);// 【实体】是否为lombok模型(默认 false)
strategyConfig.setRestControllerStyle(true);// 生成 @RestController 控制器
strategyConfig.setSuperEntityColumns("id");// 写于父类中的公共字段
// strategyConfig.setInclude("fnd_user");// 需要包含的表名,允许正则表达式(与exclude二选一配置)
// strategyConfig.setExclude("m_fnd_user1");// 需要排除的表名,允许正则表达式
strategyConfig.setControllerMappingHyphenStyle(true);// 驼峰转连字符
strategyConfig.setEntityTableFieldAnnotationEnable(true);// 是否生成实体时,生成字段注解 默认false;
// 把策略配置添加到代码生成器主类
generator.setStrategy(strategyConfig);
// 5. 向代码生成器主类上配置模板引擎,这是是freemarker,mpg.execute()方法就是执行生成代码类。
generator.setTemplateEngine(new FreemarkerTemplateEngine());
// 开始生成
generator.execute();
}
}
package com.hrf.blog;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.hrf.blog.mapper")//扫描包
public class BlogApplication {
public static void main(String[] args) {
SpringApplication.run(BlogApplication.class, args);
}
}
参考地址MyBatis-Plus
<!-- mybatis-plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<!-- MP代码生成器依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3</version>
</dependency>
产生原因:新版本中对象已经私有化。
解决方法:使用新的代码生成器
@Test
public void generate(){
// 获取用户程序当前路径
String projectPath = System.getProperty("user.dir");
// 设置输出到的目录
String out = projectPath + "/src/main/java";
// 设置mapper.xml生成路径
String xmlOut = projectPath + "/src/main/java/com/hrf/blog/user/mapper/xml";
FastAutoGenerator.create("jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Shanghai", "root", "0429")
.globalConfig(builder -> {
builder.author("hrf") // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir(out); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com.hrf.blog.user") // 设置父包名
// .moduleName("blogUser") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.xml, xmlOut)); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder
// .addInclude("t_simple") // 设置需要生成的表名
.addTablePrefix("blog_", "c_"); // 设置过滤表前缀
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
package com.hrf.blog.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.hrf.blog.mapper")
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
// @Bean
// public ConfigurationCustomizer configurationCustomizer() {
// return configuration -> configuration.setUseDeprecatedExecutor(false);
// }
}