依賴
<!--mybatis-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
一些配置(和代碼自動生成無關(guān))
mybatis-plus.mapper-locations=classpath:/mapper/*.xml
mybatis-plus.typeAliasesPackage=com.example.model
mybatis-plus.global-config.id-type=2
mybatis-plus.global-config.field-strategy=2
mybatis-plus.global-config.db-column-underline=true
mybatis-plus.global-config.refresh-mapper=true
mybatis-plus.global-config.key-generator=com.baomidou.mybatisplus.incrementer.OracleKeyGenerator
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false
導(dǎo)出設(shè)置類
public class MyGenerator {
public static void main(String[] args) {
String packageName = "com.example";
generateByOracleTables(packageName,"TABLE_NAME");
}
/**
* oracle
*
* @param packageName
* @param tableNames
*/
private static void generateByOracleTables(String packageName, String... tableNames) {
GlobalConfig config = new GlobalConfig();
String dbUrl = "數(shù)據(jù)庫地址";
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.ORACLE)
.setUrl(dbUrl)
.setUsername("用戶名")
.setPassword("密碼")
.setDriverName("oracle.jdbc.driver.OracleDriver");
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig
// .setTablePrefix("表名前綴(生成的實體會省略這個前綴)")
.setCapitalMode(true)//駝峰命名
.setEntityLombokModel(true)//使用lombk
.setDbColumnUnderline(true)//駝峰命名
.setRestControllerStyle(true)
.setNaming(NamingStrategy.underline_to_camel)
.setSuperEntityClass("com.example.common.SuperEntity")
.setSuperMapperClass("com.example.common.SuperMapper")
.setSuperControllerClass("com.example.common.SuperController")
.setInclude(tableNames);//修改替換成你需要的表名钾军,多個表名傳數(shù)組
config.setActiveRecord(false)
.setAuthor("作者名")
.setOutputDir("生成文件導(dǎo)出地址")
.setEnableCache(false)
.setBaseColumnList(true)
.setBaseResultMap(true)
.setFileOverride(true);
new AutoGenerator().setGlobalConfig(config)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(
new PackageConfig()
.setParent(packageName)
.setController("controller")
.setService("service")
.setServiceImpl("serviceImp")
.setEntity("model")
).execute();
}
/**
* mysql
*
* @param packageName
* @param tableNames
*/
private static void generateByTables(String packageName, String... tableNames) {
GlobalConfig config = new GlobalConfig();
String dbUrl = "數(shù)據(jù)庫地址";
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL)
.setUrl(dbUrl)
.setUsername("root")
.setPassword("123456")
.setDriverName("com.mysql.jdbc.Driver");
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig
.setCapitalMode(true)
.setEntityLombokModel(false)
.setDbColumnUnderline(true)
.setNaming(NamingStrategy.underline_to_camel)
.setSuperEntityClass("com.example.common.SuperEntity")
.setSuperMapperClass("com.example.common.SuperMapper")
.setInclude(tableNames);//修改替換成你需要的表名贮折,多個表名傳數(shù)組
config.setActiveRecord(false)
.setAuthor("作者名")
.setOutputDir("生成目錄")
.setFileOverride(true);
new AutoGenerator().setGlobalConfig(config)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(
new PackageConfig()
.setParent(packageName)
.setController("controller")
.setService("service")
.setServiceImpl("serviceImp")
.setEntity("model")
).execute();
}
}