代碼生成器的使用
1.引入Maven
生成器可以采用velocity模板或freemarker模板市俊,下面采用的是freemark模板
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
<!-- freemarker 模板引擎 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
2.編寫模板
在mybatis-plus-generator-3.0.6.jar包下可以找到默認(rèn)的模板配置殊橙。如果想采用修改生成的代碼踩验,只需將相應(yīng)的模板拷貝到自己工程下的templates目錄即可蛇摸。
默認(rèn)模板位置
3.數(shù)據(jù)庫的連接配置
按基本的jdbc連就行了隐轩,不過我在連接時(shí)踩了個(gè)坑垢粮,記錄一下个束。
在連接時(shí)沒有設(shè)置時(shí)區(qū)導(dǎo)致一直在報(bào)錯(cuò)蘸炸,所以設(shè)置URL時(shí)最好帶上
jdbc:mysql://localhost:3306/" + DB_NAME + "?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC
4.生成代碼
//包名
public static final String PACKAGE_NAME = "com.yyq.demo";
//模塊名
public static final String MODULE_NAME = "code";
//數(shù)據(jù)庫名
public static final String DB_NAME = "test";
//數(shù)據(jù)庫表
public static final String TABLE_NAME = "t_user";
//獲取當(dāng)前項(xiàng)目的路徑
public static final String PROJECT_PATH = System.getProperty("user.dir");
//代碼存儲(chǔ)路徑
public static final String CODE_SAVE_PATH = PROJECT_PATH + "/src/main/java/" + StringUtils.join((PACKAGE_NAME + "." + MODULE_NAME).split("\\."), "/");
public static void main(String[] args) {
// 代碼生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(PROJECT_PATH + "/src/main/java");
gc.setAuthor("Richard");
gc.setOpen(false);
gc.setFileOverride(true);
gc.setServiceName("%sService");
gc.setBaseColumnList(true);
gc.setBaseResultMap(true);
gc.setDateType(DateType.ONLY_DATE);
mpg.setGlobalConfig(gc);
// 數(shù)據(jù)源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/" + DB_NAME + "?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(MODULE_NAME);
pc.setParent(PACKAGE_NAME);
mpg.setPackageInfo(pc);
// 自定義配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸入文件名稱
return PROJECT_PATH + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
focList.add(new FileOutConfig("/templates/service.java.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸入文件名稱
return CODE_SAVE_PATH + "/service/" + tableInfo.getEntityName() + "Service" + StringPool.DOT_JAVA;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(new TemplateConfig().setXml(null));
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setTablePrefix(new String[]{"t_"});
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setSuperEntityClass("com.yyq.demo.bean.BaseEntity");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setSuperControllerClass("com.yyq.demo.controller.BaseController");
strategy.setInclude(TABLE_NAME);
//自定義基礎(chǔ)的Entity類哑舒,公共字段 ,填入將在entity中不出現(xiàn)
strategy.setSuperEntityColumns("id","version");
strategy.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
//刪除自己不需要的目錄
deleteDir(new File(CODE_SAVE_PATH+"/service/impl"));
}
private static void deleteDir(File dir) {
if (dir.isDirectory()) {
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
deleteDir(files[i]);
}
}
dir.delete();
}
各種查詢套路
1.使用eq()時(shí)
字段名要是數(shù)據(jù)庫對應(yīng)的字段名幻馁,如wrapper.eq("login_name","test");
不能直接用于和null比較要用wrapper.isNull("param_name");
2.where()
可以寫任何的sql在里面洗鸵,可以把其他關(guān)聯(lián)表的結(jié)合起來做查詢,如
wrapper.where("id in (select o_id from t_a where name like concat('%',{0},'%'))",keyword);