本文僅對使用MyBatis-Plus的代碼生成器配置做保存,適合使用了該插件的童鞋做參考。
內部有大量默認配置,有性趣的童鞋可以研究下源碼。
ps:官方文檔更齊全http://mp.baomidou.com/
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
package com.kichun.ucenter.service; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; import com.baomidou.mybatisplus.generator.config.rules.DbType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.io.File; import java.util.*; /** * Created by wangqichang on 2018/6/1. */ public class MabatisPlusGenerator { //生成文件所在項目路徑 private static String baseProjectPath = "D:\\Git\\strandrd_official_website\\kichun\\kichun-ucenter\\kichun-ucenter-entity" ; //基本包名 private static String basePackage= "com.kichun.ucenter" ; //作者 private static String authorName= "wangqichang" ; //要生成的表名 private static String[] tables= { "t_role" , "t_resource" , "t_role_resource" , "t_user_role" }; //table前綴 private static String prefix= "t_" ; //數據庫配置四要素 private static String driverName = "net.sf.log4jdbc.DriverSpy" ; private static String url = "jdbc:log4jdbc:mysql://127.0.0.1:3306/kichun_dev?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true" ; private static String username = "不告訴你" ; private static String password = "密碼也不告訴你" ; public static void main(String[] args) { AutoGenerator gen = new AutoGenerator(); /** * 數據庫配置 */ gen.setDataSource( new DataSourceConfig() .setDbType(DbType.MYSQL) .setDriverName(driverName) .setUrl(url) .setUsername(username) .setPassword(password) .setTypeConvert( new MySqlTypeConvert() { // 自定義數據庫表字段類型轉換【可選】 @Override public DbColumnType processTypeConvert(String fieldType) { System.out.println( "轉換類型:" + fieldType); // if ( fieldType.toLowerCase().contains( "tinyint" ) ) { // return DbColumnType.BOOLEAN; // } return super .processTypeConvert(fieldType); } })); /** * 全局配置 */ gen.setGlobalConfig( new GlobalConfig() .setOutputDir( baseProjectPath + "/src/main/java" ) //輸出目錄 .setFileOverride( true ) // 是否覆蓋文件 .setActiveRecord( true ) // 開啟 activeRecord 模式 .setEnableCache( false ) // XML 二級緩存 .setBaseResultMap( true ) // XML ResultMap .setBaseColumnList( true ) // XML columList .setOpen( false ) //生成后打開文件夾 .setAuthor(authorName) // 自定義文件命名,注意 %s 會自動填充表實體屬性! .setMapperName( "%sMapper" ) .setXmlName( "%sMapper" ) .setServiceName( "%sService" ) .setServiceImplName( "%sServiceImpl" ) .setControllerName( "%sController" ) ); /** * 策略配置 */ gen.setStrategy( new StrategyConfig() // .setCapitalMode(true)// 全局大寫命名 //.setDbColumnUnderline(true)//全局下劃線命名 .setTablePrefix( new String[]{prefix}) // 此處可以修改為您的表前綴 .setNaming(NamingStrategy.underline_to_camel) // 表名生成策略 .setInclude(tables) // 需要生成的表 .setRestControllerStyle( true ) //.setExclude(new String[]{"test"}) // 排除生成的表 // 自定義實體父類 // .setSuperEntityClass("com.baomidou.demo.TestEntity") // 自定義實體,公共字段 //.setSuperEntityColumns(new String[]{"test_id"}) //.setTableFillList(tableFillList) // 自定義 mapper 父類 默認BaseMapper //.setSuperMapperClass("com.baomidou.mybatisplus.mapper.BaseMapper") // 自定義 service 父類 默認IService // .setSuperServiceClass("com.baomidou.demo.TestService") // 自定義 service 實現類父類 默認ServiceImpl // .setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl") // 自定義 controller 父類 //.setSuperControllerClass("com.kichun."+packageName+".controller.AbstractController") // 【實體】是否生成字段常量(默認 false) // public static final String ID = "test_id"; // .setEntityColumnConstant(true) // 【實體】是否為構建者模型(默認 false) // public User setName(String name) {this.name = name; return this;} // .setEntityBuilderModel(true) // 【實體】是否為lombok模型(默認 false)<a href="https://projectlombok.org/" rel="external nofollow" >document</a> .setEntityLombokModel( true ) // Boolean類型字段是否移除is前綴處理 // .setEntityBooleanColumnRemoveIsPrefix(true) // .setRestControllerStyle(true) // .setControllerMappingHyphenStyle(true) ); /** * 包配置 */ gen.setPackageInfo( new PackageConfig() //.setModuleName("User") .setParent(basePackage) // 自定義包路徑 .setController( "controller" ) // 這里是控制器包名,默認 web .setEntity( "entity" ) .setMapper( "dao" ) .setService( "service" ) .setServiceImpl( "service.impl" ) .setXml( "mapper" ) ); /** * 注入自定義配置 */ // 注入自定義配置,可以在 VM 中使用 cfg.abc 設置的值 InjectionConfig abc = new InjectionConfig() { @Override public void initMap() { Map<String, Object> map = new HashMap<>(); map.put( "abc" , this .getConfig().getGlobalConfig().getAuthor() + "-mp" ); this .setMap(map); } }; //自定義文件輸出位置(非必須) List<FileOutConfig> fileOutList = new ArrayList<>(); fileOutList.add( new FileOutConfig( "/templates/mapper.xml.ftl" ) { @Override public String outputFile(TableInfo tableInfo) { return baseProjectPath + "/src/main/resources/mappers/" + tableInfo.getEntityName() + ".xml" ; } }); abc.setFileOutConfigList(fileOutList); gen.setCfg(abc); /** * 指定模板引擎 默認是VelocityTemplateEngine ,需要引入相關引擎依賴 */ gen.setTemplateEngine( new FreemarkerTemplateEngine()); /** * 模板配置 */ gen.setTemplate( // 關閉默認 xml 生成,調整生成 至 根目錄 new TemplateConfig().setXml( null ) // 自定義模板配置,模板可以參考源碼 /mybatis-plus/src/main/resources/template 使用 copy // 至您項目 src/main/resources/template 目錄下,模板名稱也可自定義如下配置: // .setController("..."); // .setEntity("..."); // .setMapper("..."); // .setXml("..."); // .setService("..."); // .setServiceImpl("..."); ); // 執行生成 gen.execute(); } } |
到此這篇關于MyBatis-Plus Generator配置詳解的文章就介紹到這了,更多相關MyBatis-Plus Generator配置內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/huangjinyong/p/11234268.html