mybatisplus 代码生成器分为两个版本,mybatis-plus-generator 3.5.1 以下版本和mybatis-plus-generator 3.5.1 以上版本
mybatis-plus-generator 3.5.1 以下版本使用
添加依赖:
<!-- mybatisPlus 代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
编写自动代码生成类
public class MysqlGenerator {
public static void main(String[] args) {
GlobalConfig config = new GlobalConfig();
config.setActiveRecord(true);
config.setAuthor("lfh");
config.setOutputDir(System.getProperty("user.dir") + "\\src\\main\\java");
config.setFileOverride(true);
config.setIdType(IdType.AUTO);
config.setServiceName("%sService");
config.setBaseResultMap(true);
config.setBaseColumnList(true);
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setUrl("jdbc:mysql://localhost:3306/springboot-test-db?useUnicode=true&useSSL=false&characterEncoding=utf8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setCapitalMode(true);
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setTablePrefix("tbl_");
strategyConfig.setInclude("tbl_user");
PackageConfig pc = new PackageConfig();
pc.setParent("com.example.springboot")
.setMapper("mapper")
.setService("service")
.setController("controller")
.setEntity("bean")
.setXml("mapper");
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(config)
.setDataSource(dsc)
.setStrategy(strategyConfig)
.setPackageInfo(pc);
autoGenerator.execute();
}
}
mybatis-plus-generator 3.5.1 以上版本
添加依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
编写自动代码生成类
public class MybatisPlusGenerator {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/springboot-test-db?useUnicode=true&useSSL=false&characterEncoding=utf8";
String username = "root";
String password = "root";
List<String> tables = new ArrayList<>();
tables.add("tbl_user");
FastAutoGenerator.create(url, username, password)
.globalConfig(builder -> {
builder.fileOverride()
.disableOpenDir()
.outputDir(System.getProperty("user.dir") + "\\src\\main\\java")
.author("lfh")
.dateType(DateType.TIME_PACK)
.commentDate("yyyy-MM-dd");
})
.packageConfig(builder -> {
builder.parent("com.example.demo")
.entity("bean")
.service("service")
.serviceImpl("service.impl")
.mapper("mapper")
.xml("mapper.xml")
.controller("controller")
.other("config")
.pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "\\src\\main\\resources\\mapper"));
})
.strategyConfig(builder -> {
builder.addInclude(tables)
.addTablePrefix("tbl_")
.serviceBuilder()
.formatServiceFileName("%sService")
.formatServiceImplFileName("%sServiceImpl")
.entityBuilder()
.enableLombok()
.enableTableFieldAnnotation()
.controllerBuilder()
.formatFileName("%sController")
.enableRestStyle()
.mapperBuilder()
.superClass(BaseMapper.class)
.formatMapperFileName("%sMapper")
.enableMapperAnnotation()
.formatXmlFileName("%sMapper");
})
.execute();
}
}
详细配置可以参考官网
|