小伙伴们,有没有这样一个体验,每次开始写一个项目时,搭建项目环境,建entity,mapper,service,controller层文件的感到繁琐,这属实体力活呀!然而,自从有了Mybatis-Plus,自动生成代码,公司就再也不需要我这个fw了,哭唧唧~~
下面是在JDK21环境、maven3.6.3、SpringBoot3.x更新的代码生成器。
- 1. pomxml
<?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>3.2.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.hu</groupId><artifactId>mybatisplus-generator</artifactId><version>0.0.1-SNAPSHOT</version><name>mybatisplus-generator</name><description>Demo project for Spring Boot</description><properties><java.version>21</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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><!-- mysql驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.20</version><scope>runtime</scope></dependency><!-- druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.18</version></dependency><!-- mybatis-plus-boot-starter --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.5.3</version></dependency><!-- freemarker --><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.32</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>
- 2.application.yml
server:port: 8081
logging:level:com.hu.mybatisplus: debug
spring:# 数据源配置datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/simpledemo?charactEncoding=UTF-8&serverTimezone=UTCusername: rootpassword: rootservlet:multipart:enabled: truemax-file-size: 2000MBmax-request-size: 2000MB
mybatis-plus:global-config:db-config:logic-delete-value: 1logic-not-delete-value: 0
- 3. 创建单元测试类com.hu.mybatisplus.MybatisplusGeneratorApplicationTests
@Testvoid testGenerator() {FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/simpledemo?charactEncoding=UTF-8&serverTimezone=UTC", "root", "root")//1.全局配置.globalConfig(builder -> {builder.author("shunpeng.hu")
// .enableSwagger().fileOverride() // 是否覆盖.outputDir("E:/study_code/mybatisplus-generator/src/main/java") // 设置生成路径.dateType(DateType.ONLY_DATE);})//2.包的配置.packageConfig(builder -> {builder.moduleName("generator").parent("com.hu.mybatisplus").entity("domain").mapper("mapper").service("service").serviceImpl("impl").controller("controller").pathInfo(Collections.singletonMap(OutputFile.xml, "E:/study_code/mybatisplus-generator/src/main/resources/mapper"));})//3.策略配置.strategyConfig(builder -> {builder.addInclude("tb_employee", "tb_department") // 设置要映射的表.addTablePrefix("tb_") // 设置过滤表前缀.entityBuilder().enableLombok() // 自动lombok.naming(NamingStrategy.underline_to_camel).columnNaming(NamingStrategy.underline_to_camel).logicDeletePropertyName("deleted"); // 逻辑删除})//4.Freemarker引擎模板.templateEngine(new FreemarkerTemplateEngine()).execute();}
执行一下单元测试,就能有当前工程代码生成的模块~ 来吧,看看Freemarker模板引擎生成代码吧!
这些属实体力活的事情不需要额外花时间去做的~