我是靠谱客的博主 阳光飞鸟,这篇文章主要介绍mybatis generator为实体类生成自定义注释(读取数据库字段的注释添加到实体类,不修改源码)1、首先定义注释生成插件2、然后为mybatisgenerator配置插件3、使用mybatis generator自动生成代码4、生成效果 ,现在分享给大家,希望可以做个参考。
我们都知道mybatis generator自动生成的注释没什么实际作用,而且还增加了代码量。如果能将注释从数据库中捞取到,不仅能很大程度上增加代码的可读性,而且减少了后期手动加注释的工作量。
1、首先定义注释生成插件
复制代码
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
95package com.dto.channellabel; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Properties; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.dom.java.Field; import org.mybatis.generator.api.dom.java.InnerClass; import org.mybatis.generator.api.dom.java.Method; import org.mybatis.generator.api.dom.java.Parameter; import org.mybatis.generator.api.dom.java.TopLevelClass; import org.mybatis.generator.internal.DefaultCommentGenerator; public class MyCommentGenerator extends DefaultCommentGenerator { private Properties properties; private Properties systemPro; private boolean suppressDate; private boolean suppressAllComments; private String currentDateStr; public MyCommentGenerator() { super(); properties = new Properties(); systemPro = System.getProperties(); suppressDate = false; suppressAllComments = false; currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date()); } public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); field.addJavaDocLine("/**"); sb.append(" * "); sb.append(introspectedColumn.getRemarks()); sb.append("n"); sb.append(" * 列名:" + introspectedColumn.getActualColumnName() + " 类型:" + introspectedColumn.getJdbcTypeName() + "(" + introspectedColumn.getLength() + ")" + " 允许空:" + introspectedColumn.isNullable() + " 缺省值:" + introspectedColumn.getDefaultValue()); field.addJavaDocLine(sb.toString()); field.addJavaDocLine(" */"); } public void addFieldComment(Field field, IntrospectedTable introspectedTable) { } public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); sb.append("/**"); sb.append("n"); sb.append(" * "); sb.append("n"); sb.append(" * @author zhangsan" + "n"); if (!suppressDate) { sb.append(" * @date " + currentDateStr + "n"); } List<Parameter> parameters = method.getParameters(); for (Parameter parameter : parameters) { sb.append(" * @param " + parameter.getName() + "n"); } sb.append(" * @return " + method.getReturnType()); sb.append("n" + " */"); method.addJavaDocLine(sb.toString()); } public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { } public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { } public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) { super.addClassComment(innerClass, introspectedTable, markAsDoNotDelete); } public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) { } public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); sb.append("/**"); sb.append("n"); sb.append("* "); sb.append("n"); sb.append("* @author zhangsan" + "n"); if (!suppressDate) { sb.append("* @date " + currentDateStr + "n"); } sb.append("* 数据表" + introspectedTable.getFullyQualifiedTableNameAtRuntime() + "映射bean,由Mybaits自动生成工具生成"); sb.append("n" + "*/"); topLevelClass.addJavaDocLine(sb.toString()); } }
2、然后为mybatisgenerator配置插件
复制代码
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<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!--导入属性配置 --> <!--<properties resource="generator.properties"></properties> --> <!--指定特定数据库的jdbc驱动jar包的位置 --> <classPathEntry location="D:maven_peizhixinxilocal_repositorylocal_repositorymysqlmysql-connector-java5.1.18mysql-connector-java-5.1.18.jar" /> <context id="default" targetRuntime="MyBatis3"> <!-- optional,旨在创建class时,对注释进行控制 --> <commentGenerator type="com.dataplatform.dto.channellabel.MyCommentGenerator"> <!-- <property name="suppressDate" value="true" /> <property name="suppressAllComments" value="true" /> --> </commentGenerator> <!--jdbc的数据库连接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://mysql.local.gene:3306/dms" userId="mmmm" password="*1234!"> </jdbcConnection> <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制 --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类 targetPackage 指定生成的model生成所在的包名 targetProject 指定在该项目下所在的路径 --> <javaModelGenerator targetPackage="com.mmmm.cms.modules3" targetProject="src/main/java"> <!-- 是否允许子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false" /> <!-- 是否对model添加 构造函数 --> <property name="constructorBased" value="false" /> <!-- 是否对类CHAR类型的列的数据进行trim操作 --> <property name="trimStrings" value="true" /> <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 --> <property name="immutable" value="false" /> </javaModelGenerator> <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 --> <sqlMapGenerator targetPackage="META-INF.mapper.cms3" targetProject="src/main/resources"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --> <javaClientGenerator targetPackage="com.mmmm.cms.dao3" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="application_detail" domainObjectName="applicationDetail" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
3、使用mybatis generator自动生成代码
由于使用的是maven项目,而且使用了了自定义的插件,所以采用 main方法启动,适用场景更对,而且能将代码生成到对应的工程目录下,免去拷贝的过程(当然也可以用maven插件、控制台、eclipse插件等多种方式启动)。
注意:当前类所在的工程要添加mybatis generator的依赖包
启动类如下
复制代码
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
30package com.mmmm.dataplatform.dto.channellabel; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; public class MyBatisGeneratorRun { public static void main(String[] args) throws Exception{ MyBatisGeneratorRun app = new MyBatisGeneratorRun(); System.out.println(app.getClass().getResource("/").getPath()); app.generator(); System.out.println(System.getProperty("user.dir")); } public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(resourceAsStream); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); for(String warning:warnings){ System.out.println(warning); } } }
再贴下项目的maven依赖,有需要的可以看下
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>party.lovey</groupId> <artifactId>generator</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> </dependencies> </project>
4、生成效果
复制代码
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263package com.mmmm.cms.modules3; import java.util.Date; /** * * @author zhangsan * @date 2018-10-11 * 数据表application_detail映射bean,由Mybaits自动生成工具生成 */ public class applicationDetail { /** * 主键 唯一标识 * 列名:id 类型:INTEGER(10) 允许空:false 缺省值:null */ private Integer id; /** * 申请原始信息表id * 列名:application_original_id 类型:INTEGER(10) 允许空:true 缺省值:null */ private Integer applicationOriginalId; /** * 机构id * 列名:org_id 类型:VARCHAR(12) 允许空:true 缺省值:null */ private String orgId; /** * 进件编码 * 列名:zg_application_no 类型:VARCHAR(15) 允许空:false 缺省值:null */ private String zgApplicationNo; /** * 渠道代码 * 列名:source 类型:VARCHAR(20) 允许空:true 缺省值:null */ private String source; /** * 申请时间 * 列名:apply_time 类型:TIMESTAMP(19) 允许空:true 缺省值:null */ private Date applyTime; /** * 申请人姓名 * 列名:apply_name 类型:VARCHAR(16) 允许空:true 缺省值:null */ private String applyName; /** * 申请人证件号 * 列名:apply_id 类型:VARCHAR(18) 允许空:true 缺省值:null */ private String applyId; /** * 申请人手机号码 * 列名:apply_phone 类型:VARCHAR(20) 允许空:true 缺省值:null */ private String applyPhone; /** * 单位地址-省 * 列名:com_province 类型:VARCHAR(60) 允许空:true 缺省值:null */ private String comProvince; /** * 单位地址-市 * 列名:com_city 类型:VARCHAR(60) 允许空:true 缺省值:null */ private String comCity; /** * 单位地址-区 * 列名:com_area 类型:VARCHAR(60) 允许空:true 缺省值:null */ private String comArea; /** * 单位地址-详细地址 * 列名:com_detail_addr 类型:VARCHAR(200) 允许空:true 缺省值:null */ private String comDetailAddr; /** * 是否是持卡人 * 列名:is_card_holder 类型:BIT(0) 允许空:true 缺省值:null */ private Boolean isCardHolder; /** * 推荐人id * 列名:recom_id 类型:VARCHAR(16) 允许空:true 缺省值:null */ private String recomId; /** * 父推荐人id * 列名:parent_recom_id 类型:VARCHAR(16) 允许空:true 缺省值:null */ private String parentRecomId; /** * 打标异常,更新打标状态【'notmark'-还没有打标 'success'-打标成功 '其他'-打标异常】 * 列名:state 类型:VARCHAR(1024) 允许空:true 缺省值:notmark */ private String state; /** * 创建时间 * 列名:create_time 类型:TIMESTAMP(19) 允许空:true 缺省值:CURRENT_TIMESTAMP */ private Date createTime; /** * 更新时间 * 列名:modify_time 类型:TIMESTAMP(19) 允许空:true 缺省值:CURRENT_TIMESTAMP */ private Date modifyTime; /** * 批次号码 * 列名:batch_no 类型:VARCHAR(16) 允许空:false 缺省值:null */ private String batchNo; /** * 扩展字段1 * 列名:ext_1 类型:VARCHAR(256) 允许空:true 缺省值:null */ private String ext1; /** * 扩展字段2 * 列名:ext_2 类型:VARCHAR(256) 允许空:true 缺省值:null */ private String ext2; /** * 扩展字段3 * 列名:ext_3 类型:VARCHAR(256) 允许空:true 缺省值:null */ private String ext3; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getApplicationOriginalId() { return applicationOriginalId; } public void setApplicationOriginalId(Integer applicationOriginalId) { this.applicationOriginalId = applicationOriginalId; } public String getOrgId() { return orgId; } public void setOrgId(String orgId) { this.orgId = orgId == null ? null : orgId.trim(); } public String getZgApplicationNo() { return zgApplicationNo; } public void setZgApplicationNo(String zgApplicationNo) { this.zgApplicationNo = zgApplicationNo == null ? null : zgApplicationNo.trim(); } public String getSource() { return source; } public void setSource(String source) { this.source = source == null ? null : source.trim(); } public Date getApplyTime() { return applyTime; } public void setApplyTime(Date applyTime) { this.applyTime = applyTime; } public String getApplyName() { return applyName; } public void setApplyName(String applyName) { this.applyName = applyName == null ? null : applyName.trim(); } public String getApplyId() { return applyId; } public void setApplyId(String applyId) { this.applyId = applyId == null ? null : applyId.trim(); } public String getApplyPhone() { return applyPhone; } public void setApplyPhone(String applyPhone) { this.applyPhone = applyPhone == null ? null : applyPhone.trim(); } public String getComProvince() { return comProvince; } public void setComProvince(String comProvince) { this.comProvince = comProvince == null ? null : comProvince.trim(); } public String getComCity() { return comCity; } public void setComCity(String comCity) { this.comCity = comCity == null ? null : comCity.trim(); } public String getComArea() { return comArea; } public void setComArea(String comArea) { this.comArea = comArea == null ? null : comArea.trim(); } public String getComDetailAddr() { return comDetailAddr; } public void setComDetailAddr(String comDetailAddr) { this.comDetailAddr = comDetailAddr == null ? null : comDetailAddr.trim(); } public Boolean getIsCardHolder() { return isCardHolder; } public void setIsCardHolder(Boolean isCardHolder) { this.isCardHolder = isCardHolder; } public String getRecomId() { return recomId; } public void setRecomId(String recomId) { this.recomId = recomId == null ? null : recomId.trim(); } public String getParentRecomId() { return parentRecomId; } public void setParentRecomId(String parentRecomId) { this.parentRecomId = parentRecomId == null ? null : parentRecomId.trim(); } public String getState() { return state; } public void setState(String state) { this.state = state == null ? null : state.trim(); } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getModifyTime() { return modifyTime; } public void setModifyTime(Date modifyTime) { this.modifyTime = modifyTime; } public String getBatchNo() { return batchNo; } public void setBatchNo(String batchNo) { this.batchNo = batchNo == null ? null : batchNo.trim(); } public String getExt1() { return ext1; } public void setExt1(String ext1) { this.ext1 = ext1 == null ? null : ext1.trim(); } public String getExt2() { return ext2; } public void setExt2(String ext2) { this.ext2 = ext2 == null ? null : ext2.trim(); } public String getExt3() { return ext3; } public void setExt3(String ext3) { this.ext3 = ext3 == null ? null : ext3.trim(); } }
最后
以上就是阳光飞鸟最近收集整理的关于mybatis generator为实体类生成自定义注释(读取数据库字段的注释添加到实体类,不修改源码)1、首先定义注释生成插件2、然后为mybatisgenerator配置插件3、使用mybatis generator自动生成代码4、生成效果 的全部内容,更多相关mybatis内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复