1.改造源码的作用
直接用mybayis_generate连接oracle生成实体类后,字段都是大写的,且没有注释,修改源码后可以自定义大小写,字段也有注释。效果如下图:
2.下载mybatis_generate源码
链接:https://pan.baidu.com/s/14EG0bX3Cr7L9YkC1ZGOKug?pwd=1234
提取码:1234
下载完成后用idea导入后打开的结构如下:
主要用到的是mybatis-generate-core 部分的代码。
3.改造和使用代码生成实体类
a.启动类和配置文件
b.修改配置文件
<?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>
<!-- java驱动包 在本例中在同一目录,使用相对目录,具体使用时可以使用绝对路径-->
<classPathEntry
location="D:\maven\apache-maven-3.5.2\repository\com\oracle\ojdbc8\1.0.0\ojdbc8-1.0.0.jar" />
<context id="DB" targetRuntime="MyBatis3">
<!-- 生成注释 -->
<commentGenerator type="org.mybatis.generator.internal.MyCommentGenerator">
<!--去除注释 选择true时去除注释,当值为false时自动生成注释-->
<!-- <property name="suppressAllComments" value="true" />-->
<!--注释中去除日期注释 -->
<!-- <property name="suppressDate" value="true" />-->
<!--注释中添加数据库字段备注注释 -->
<!-- <property name="addRemarkComments" value="true" />-->
<!-- 文件编码 -->
<property name="javaFileEncoding" value="UTF-8" />
<!-- 忽略所有注解,使用自定义的注解生成 -->
<!-- <property name="suppressAllComments" value="true" />-->
<!-- 作者 -->
<property name="author" value="强哥是混蛋" />
<!-- 时间 value值为时间格式-->
<property name="date" value="yyyy/MM/dd" />
</commentGenerator>
<!-- 配置数据库连接,加入属性写在url串里,与普通url语句略有不同,本次连接本地的blog数据库,根据情况自行修改 -->
<jdbcConnection
driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@10.104.65.181:1521/orcl"
userId="hn_yszx0106"
password="1">
<!--MySQL 8.x 需要指定服务器的时区 -->
<!--<property name="serverTimezone" value="UTC" />-->
<!--MySQL 不支持 schema 或者 catalog 所以需要添加这个 -->
<!--参考 : http://www.mybatis.org/generator/usage/mysql.html -->
<!--<property name="nullCatalogMeansCurrent" value="true" />-->
<!-- MySQL8默认启用 SSL ,不关闭会有警告 -->
<!--<property name="useSSL" value="false" />-->
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- Model模型生成器 就是普通意义上的model 两个路径下面详解-->
<javaModelGenerator
targetPackage="com.mnn.love.dao" targetProject="src/test/java">
<!-- 对char的数据是否进行trim -->
<property name="trimStrings" value="false" />
<!-- 是否有构造函数 -->
<property name="constructorBased" value="false" />
</javaModelGenerator>
<!-- 生成mapper映射文件 xml文件-->
<!-- <sqlMapGenerator targetPackage="com.mnn.love.mapper"-->
<!-- targetProject="src/test/resources">-->
<!-- <property name="enableSubPackages" value="true" />-->
<!-- </sqlMapGenerator>-->
<!-- 代码 -->
<!-- <javaClientGenerator type="XMLMAPPER"-->
<!-- targetPackage="com.mnn.love.mapper" targetProject="src/test/java">-->
<!-- <property name="enableSubPackages" value="true" />-->
<!-- </javaClientGenerator>-->
<!-- 各个表的数据 tableName为数据中表名,domainObjectName为在java文件的表名-->
<table tableName="pay_plan_voucher" domainObjectName="PayPlanVoucher">
<property name="useActualColumnNames" value="true" />
<property name="isLowwer" value="true" />
<property name="constructorBased" value="false" />
<columnOverride column="IS_DELETED" javaType="java.lang.int" jdbcType="NUMERIC" />
</table>
</context>
</generatorConfiguration>
注意:
- 修改classPathEntry标签中驱动的位置
- context标签中的子标签commentGenerator是自定义的增加字段注释的类,路径在org.mybatis.generator.internal.MyCommentGenerator,具体代码如下:
/*
* Copyright ${license.git.copyrightYears} the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.generator.internal;
import org.apache.commons.lang3.StringUtils;
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.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.PropertyRegistry;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import static org.mybatis.generator.internal.util.StringUtility.isTrue;
public class MyCommentGenerator extends DefaultCommentGenerator {
private String author;
private String date;
private String dateContent;
private static final String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
Map<String,Map<String,String>> map = new HashMap<>();
//类上添加注释
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
topLevelClass.addJavaDocLine("/**");
String remark = introspectedTable.getRemarks();
if (StringUtils.isNotBlank(remark)) {
topLevelClass.addJavaDocLine(" * " + remark);
}
topLevelClass.addJavaDocLine(" * ");
topLevelClass.addJavaDocLine(" * " + "@author " + getAuthor());
topLevelClass.addJavaDocLine(" * " + "@date " + getDateContent());
topLevelClass.addJavaDocLine(" */");
}
public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
suppressDate = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
suppressAllComments = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
addRemarkComments = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS));
author = properties.getProperty("author");
author = author == null ? "" : author;
date = properties.getProperty("date");
date = date == null ? DEFAULT_DATE_FORMAT : date;
SimpleDateFormat sf = new SimpleDateFormat(date);
dateContent = sf.format(new Date());
}
public String getAuthor() {
return author;
}
public String getDateContent() {
return dateContent;
}
//属性上添加注释
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
String tableName = introspectedTable.getTableConfiguration().getTableName().toUpperCase();
String columnName = introspectedColumn.getActualColumnName().toUpperCase();
if (!map.containsKey(tableName)) {
try {
Context context = introspectedColumn.getContext();
Connection connection = context.getConnection();
Statement statement = connection.createStatement();
statement.execute("select * from user_col_comments where table_name='" + tableName + "'");
ResultSet resultSet = statement.getResultSet();
Map<String,String> name_comment=new HashMap<>();
while (resultSet.next()) {
String column_name = resultSet.getString("COLUMN_NAME").toUpperCase();
String commnets = resultSet.getString("COMMENTS");
name_comment.put(column_name,commnets);
}
map.put(tableName,name_comment);
} catch (SQLException e) {
e.printStackTrace();
}
}
// String remark = introspectedColumn.getRemarks();
String remark = map.get(tableName).get(columnName);
if (StringUtils.isNotBlank(remark)) {
field.addJavaDocLine("/**");
field.addJavaDocLine(" * " + remark);
field.addJavaDocLine(" */");
}
}
//get方法添加注释
@Override
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
//setter方法添加注释
@Override
public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
@Override
public void addComment(XmlElement xmlElement) {
}
}
其中addFieldComment方法是在字段上添加注释。当然也可以修改set get 方法和实体类上增加注释。
- table标签中的属性isLowwer,true表示把oracle字段变成小写,不配置或者false则仍然是大写。相关的代码修改在org.mybatis.generator.internal.db。DatabaseIntrospector类中,具体代码如下:
- 其他的标签配置见明知意,不在详解。