目录
一.原本项目的目录:
二.新生成的项目的目录:
三.具体实现代码 :
package com.demo.study;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
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;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
public class TestCodeGenerator {
public static void main(String[] args) {
// 模块名
String moduleName = "study";
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
// 获取当前项目根路径
String projectPath = System.getProperty("user.dir");
System.out.println("输出当前项目根路径:"+projectPath);
gc.setOutputDir(projectPath+"/src/main/java");
gc.setAuthor("Echo");
gc.setOpen(false); //不打开生产的文件
gc.setFileOverride(false); //不覆盖之前生成的文件
gc.setServiceName("%Service");
gc.setIdType(IdType.AUTO);// 主键策略 自增 注意要和数据库中表实际情况对应
gc.setDateType(DateType.ONLY_DATE);
gc.setSwagger2(true);//自动开启swagger2的支持
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUrl("jdbc:mysql://localhost:3306/mydata?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(moduleName);
pc.setParent("com.demo");
pc.setController("controller");
pc.setService("service");
pc.setEntity("entity");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// strategy.setInclude("t_user");
//可以用同配符号:表示生成t_开头的对应库下所有表
strategy.setInclude("t"+"_\\w*");
strategy.setNaming(NamingStrategy.underline_to_camel);// 下划线转驼峰
strategy.setTablePrefix("t_");//去掉t_这个前缀后生成类名
strategy.setEntityLombokModel(true);// 自动生成lombok注解 记住要有lombok依赖和对应的插件哈
strategy.setLogicDeleteFieldName("is_deleted");//设置逻辑删除字段 要和数据库中表对应哈
// 设置创建时间和更新时间自动填充策略
TableFill created_date = new TableFill("created_date", FieldFill.INSERT);
TableFill updated_date = new TableFill("updated_date", FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList();
tableFills.add(created_date);
tableFills.add(updated_date);
strategy.setTableFillList(tableFills);
// 乐观锁策略
strategy.setVersionFieldName("version");
strategy.setRestControllerStyle(true);//采用restful 风格的api
strategy.setControllerMappingHyphenStyle(true); // controller 请求地址采用下划线代替驼峰
mpg.setStrategy(strategy);
// 执行
mpg.execute();
}
}
application.yml:
spring:
application:
name: springboot-nixiangProject
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydata?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: 123456
pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot-nixiangProject</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--导入 对应的依赖 这里用3.0.5 官网上最新的是一个临时版temp的-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<!-- 模板引擎 记得加上不然后面自动生成代码会报错-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<!--swagger 依赖 记得要加上,不然后面自动生成实体代码注解会爆红-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.2</version>
</dependency>
</dependencies>
</project>
四.直接运行代码,就是上图生成的结果。当然,数据库链接得有。