AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各個模塊的代碼,極大的提升了開發效率。
具體實實現以及配置解析如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
package mybatis_plus; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringPool; 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.TableFill; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.DateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class CodeGenerator { public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append( "請輸入" + tip + ":" ); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotBlank(ipt)) { return ipt; } } throw new MybatisPlusException( "請輸入正確的" + tip + "!" ); } public static void main(String[] args) { // 構建代碼生成器 AutoGenerator mpg = new AutoGenerator(); //配置策略 //1.全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty( "user.dir" ); gc.setOutputDir(projectPath + "/src/main/java" ); gc.setAuthor( "heroMps" ); gc.setOpen( false ); gc.setFileOverride( false ); //是否覆蓋 gc.setServiceName( "%sService" ); //去除Service前面的I gc.setIdType(IdType.ID_WORKER); gc.setDateType(DateType.ONLY_DATE); // gc.setSwagger2(true); //實體屬性 Swagger2 注解 mpg.setGlobalConfig(gc); //2.設置數據源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl( "jdbc:mysql://127.0.0.1:3306/mybatis_plus?useUnicode=true&useSSL=false&characterEncoding=utf8" ); // dsc.setSchemaName("public"); dsc.setDriverName( "com.mysql.jdbc.Driver" ); dsc.setUsername( "root" ); dsc.setPassword( "root" ); dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc); //3.包配置 PackageConfig pc = new PackageConfig(); // pc.setModuleName("blog"); pc.setParent( "mybatis_plus" ); pc.setEntity( "entity" ); pc.setMapper( "mapper" ); pc.setService( "service" ); pc.setController( "controller" ); mpg.setPackageInfo(pc); // 自定義配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker // String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 velocity String templatePath = "/templates/mapper.xml.vm" ; // 自定義輸出配置 List<FileOutConfig> focList = new ArrayList<>(); // 自定義配置會被優先輸出 focList.add( new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // 自定義輸出文件名 , 如果你 Entity 設置了前后綴、此處注意 xml 的名稱會跟著發生變化??! return projectPath + "/src/main/resources/mapper/" + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); /* cfg.setFileCreate(new IFileCreate() { @Override public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) { // 判斷自定義文件夾是否需要創建 checkDir("調用默認方法創建的目錄,自定義目錄用"); if (fileType == FileType.MAPPER) { // 已經生成 mapper 文件判斷存在,不想重新生成返回 false return !new File(filePath).exists(); } // 允許生成模板文件 return true; } }); */ cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); // 配置自定義輸出模板 //指定自定義模板路徑,注意不要帶上.ftl/.vm, 會根據使用的模板引擎自動識別 // templateConfig.setEntity("templates/entity2.java"); // templateConfig.setService(); // templateConfig.setController(); templateConfig.setXml( null ); mpg.setTemplate(templateConfig); //4.策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude(scanner( "表名,多個英文逗號分割" ).split( "," )); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel( true ); strategy.setLogicDeleteFieldName( "deleted" ); //自動填充設置 TableFill create_time = new TableFill( "create_time" , FieldFill.INSERT); TableFill update_time = new TableFill( "update_time" , FieldFill.INSERT_UPDATE); ArrayList<TableFill> list = new ArrayList<>(); list.add(create_time); list.add(update_time); strategy.setTableFillList(list); //5.樂觀鎖配置 strategy.setVersionFieldName( "version" ); strategy.setRestControllerStyle( true ); strategy.setControllerMappingHyphenStyle( true ); mpg.setStrategy(strategy); mpg.execute(); } } |
生成目錄如下:
測試查詢
到此這篇關于springboot整合mybatis-plus代碼生成器的文章就介紹到這了,更多相關springboot整合mybatis-plus內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/heromps/article/details/114002390