<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--引用之后就不需要在引用mybatis -plus 了,以免发生冲突-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
package com.example.untils;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ Author :Wilson
* @ Date :Created in 17:01 2018/12/19
* @ Description:${description}
* @ Modified By:
* @Version: $version$
*/
public class MpGenerator {
private static String url = "jdbc:mysql://localhost:3306/boot?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
private static String user = "root";
private static String password = "root";
private static String dirverName = "com.mysql.cj.jdbc.Driver";
private static String author = "cj";
private static String outputDir = "src/main/java"; //文件生成路径
private static String packageName = "com.example";//文件生成所放的包名
public static void main(String[] args) {
//数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL)
.setUrl(url)
.setUsername(user)
.setPassword(password)
.setDriverName(dirverName);
//策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig
.setCapitalMode(true) // 全局大写命名 ORACLE 注意
//.setEntityLombokModel(false)
.setNaming(NamingStrategy.underline_to_camel); // 表名生成策略
//全局配置
GlobalConfig config = new GlobalConfig();
config.setActiveRecord(false) // 不需要ActiveRecord特性的请改为false
.setEnableCache(false) // XML 二级缓存
.setAuthor(author)
.setOutputDir(outputDir) //生成路径
.setFileOverride(true) //是否覆盖
.setServiceName("%sService")
.setEntityName("%Mo"); //自定义文件命名, %s 会自动填充表实体属性
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent(packageName);
// 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
//调整xml 生成目录
List<FileOutConfig> focList= new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return "src/main/resources/mybatis/" + tableInfo.getEntityName() + ".xml";
}
});
cfg.setFileOutConfigList(focList);
// 关闭默认 xml 生成,调整生成 至 根目录
TemplateConfig tc = new TemplateConfig();
tc.setXml(null); // 上面调整xml 生成目录,这里一定要关闭才能生效
new AutoGenerator().setGlobalConfig(config)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo( pc
).setCfg(cfg)
.setTemplate(tc)
.execute();
}
}