代码自动生成器
- 由于在普通业务开发中大多数增删改查操作都是重复的大量的,修改的内容也是相当的少,就如一个模版一样。
- 所以在此构造一个基于Mybatis-Plus的代码生成器,旨在于快速生成项目结构和基础代码。
1、搭建环境
- 新建一个Springboot空项目,引入相关依赖:
<?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>
<groupId>com.mannor</groupId>
<artifactId>MPGenerator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MPGenerator</name>
<description>MPGenerator</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!-- velocity模版引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.27</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.mannor.mpgenerator.MpGeneratorApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
除此之外,什么资源配置信息都不用添加
2、准备要用到的数据库并且建表
示例数据库表:
3、编写生成器
- 在三级目录下放入下列代码,根据自己不同的配置,适当修改配置参数
- 点击运行即可生成代码,及目录。
package com.mannor.mpgenerator;
import com.baomidou.mybatisplus.annotation.IdType;
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;
public class Generator {
public static void main(String[] args) {
// 1.创建代码生成器的对象
AutoGenerator autoGenerator = new AutoGenerator();
//数据库数据源连接配置
DataSourceConfig datasource = new DataSourceConfig();
datasource.setDriverName("com.mysql.cj.jdbc.Driver");
datasource.setUrl("jdbc:mysql://localhost:3306/mybatisplus_db");
datasource.setUsername("root");
datasource.setPassword("toor");
autoGenerator.setDataSource(datasource);
//设置全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");//设置代码生成的路径
globalConfig.setOpen(false);//设置生成完毕后是否打开生成代码所在的目录
globalConfig.setAuthor("雷迪亚兹");//设置作者
globalConfig.setFileOverride(true);//设置是否覆盖原始生成的文件
globalConfig.setMapperName("%sDao");//设置数据层接口名,%s为占位符,指代模块名称
globalConfig.setIdType(IdType.ASSIGN_ID);//设置Td生成策略
autoGenerator.setGlobalConfig(globalConfig);
//包名和相关配置
PackageConfig packageInfo = new PackageConfig();
packageInfo.setParent("com.redaz");//设置生成的包名,与代码所在位置不冲突,二者叠加组成完整
packageInfo.setEntity("domain");//设置实体类包名
packageInfo.setMapper("dao");//设置数据层包名
autoGenerator.setPackageInfo(packageInfo);
//策略设置
StrategyConfig strategyConfig = new StrategyConfig();
//strategyConfig.setInclude( "tbl_user");//设置当前参与生成的表名,参数为可变参数
//strategyConfig.setTablePrefix("tbl_");//设置数据库表的前缀名称,模块名=数据库表名I-前缀名例如: User = tbl_use
strategyConfig.setRestControllerStyle(true);//设置是否启用Rest风格
strategyConfig.setVersionFieldName( "version" );//设置乐观锁字段名
strategyConfig.setLogicDeleteFieldName("deleted");//设置逻辑删除字段名
strategyConfig.setEntityLombokModel(true); //设置是否启用Lombok
autoGenerator.setStrategy(strategyConfig);
// 2.执行代码生成器
autoGenerator.execute();
}
}