MyBatis Generator 使用

最近自己在写一个程序,里面有张特别大的表,有几十个字段。发现自己写实体类,写mapper文件的时候,真的是醉了。公司里面虽然也用mybatis,但是一直没有使用MyBatis Generator来生成过实体类、dao类、mapper文件。猜测可能和数据库字段命名有关,基本上不使用下划线分隔英文单词来给字段命名,所以一旦使用MyBatis Generator,生成出来的就不是驼峰形式的变量了。不过这次是我自己的程序,数据库字段命名完全可以按我自己的想法来了。所以决定使用MyBatis Generator来帮我减轻一点搬砖的工作量。

下面说一下我的使用方式:

1、下载:https://github.com/mybatis/generator/releases

从上面的下载地址下载   mybatis-generator-core-1.3.7.zip   这个文件,我下载是最新版本就是这个。

2、自己建个目录Mytabis-Generator,然后它在内部建个src文件夹

      1)在下载的zip文件里面找到 mybatis-generator-core-1.3.7.jar, 放到Mytabis-Generator里面

       2)把配置文件generatorConfig.xml 放到Mytabis-Generator里面,修改配置文件中的数据库配置、生成目录、表配置。

       3)把使用的数据库驱动放到Mytabis-Generator里面

       4)在 Mytabis-Generator 文件夹打开命令窗口,运行下面的命令,即可生成 实体类、dao和mapper文件了。

            java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite

Mytabis-Generator  文件夹内部结构图:

 

 

 

 

generatorConfig.xml  内容:

<?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>
    <!--数据库驱动-->
    <classPathEntry    location="postgresql-42.2.5.jar"/>
    <context id="tospc"    targetRuntime="MyBatis3">
		<!-- 生成的Java文件的编码 -->
		<property name="javaFileEncoding" value="UTF-8"/>
		<!-- 格式化java代码 -->
		<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
		<!-- 格式化XML代码 -->
		<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
		
        <commentGenerator>
			<!-- 
				suppressDate是去掉生成日期那行注释;
				suppressAllComments是去掉所有的注释;
				addRemarkComments是用于将数据库表字段的注释添加到java属性的注释上。
				suppressGetSetComments是去掉get set 方法的注释,这个是我修改源码添加的配置;
			-->
            <property name="suppressDate" value="true"/>
            <property name="suppressGetSetComments" value="true"/>
            <property name="addRemarkComments" value="true"/>
        </commentGenerator>
		
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="org.postgresql.Driver" connectionURL="jdbc:postgresql://127.0.0.1:5432/mydb?useUnicode=true&amp;characterEncoding=utf-8" userId="dbUserName" password="123456">
        </jdbcConnection>
		
        <javaTypeResolver>
			<!-- 
				true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型
				false:默认,
					scale>0;length>18:使用BigDecimal;
					scale=0;length[10,18]:使用Long;
					scale=0;length[5,9]:使用Integer;
					scale=0;length<5:使用Short;
			 -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
		
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="tospc.entity" targetProject="src">
			<!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
            <property name="enableSubPackages" value="false"/>
			<!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
            <property name="trimStrings" value="false"/>
        </javaModelGenerator>
		
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="tospc.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="tospc.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="t_file" domainObjectName="File" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table schema="mydbschema" tableName="t_schema_table" domainObjectName="SchemaTable" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

经过上面呢上面的步骤,就可以初步运用了,但是生成出来的实体类文件,通过修改配置,要不就是完全没有注释、要不就是很多mybatis generator生成的一些没什么实际用途的说明。

我的需求就是需要把数据库字段的注释生成到实体类中的属性注释里面去。我在网上找了很多文章,都说可以自己去实现接口来实现注释的生成。基本上都是要自己写个java类继承CommentGenerator接口。然后貌似还要根据数据库不同加个什么配置。他们用的基本上都是mysql、oracle数据库,而我用的是postgresql,所以不确定他们的方法是不是适用我的情况,加上我英语实在不怎么样,也不愿意去官网翻文档。

后面我从网上一篇文章看到  通过添加<property name="addRemarkComments" value="true"/> 这个配置,就可以把数据库的注释生成到实体类属性的注释上去。但是还是有很多mybatis generator生成的“废话”。我也不想自己建工程,自己写实现。所以就想通过直接修改源码来去掉这些“废话”。

下面是我修改源码的方法:

1、自己搭建一个maven工程

2、解压最开始下载的zip包里面的mybatis-generator-core-1.3.7-sources.jar,把源码拷贝到自己maven工程中,一定要保持包路径不变。

3、这个时候工程会报错。需要添加maven依赖:

    <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
    </properties>

  <dependencies>
	    <dependency>
	      <groupId>junit</groupId>
	      <artifactId>junit</artifactId>
	      <version>3.8.1</version>
	      <scope>test</scope>
	    </dependency>
		<dependency>
		    <groupId>log4j</groupId>
		    <artifactId>log4j</artifactId>
		    <version>1.2.17</version>
		</dependency>
		<dependency>
		    <groupId>org.slf4j</groupId>
		    <artifactId>slf4j-api</artifactId>
		    <version>1.7.25</version>
		</dependency>
		  <dependency>
		    <groupId>org.apache.logging.log4j</groupId>
		    <artifactId>log4j-api</artifactId>
		    <version>2.11.1</version>
		  </dependency>
		  <dependency>
		    <groupId>org.apache.logging.log4j</groupId>
		    <artifactId>log4j-core</artifactId>
		    <version>2.11.1</version>
		  </dependency>
		<dependency>
		    <groupId>commons-logging</groupId>
		    <artifactId>commons-logging</artifactId>
		    <version>1.2</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.ant</groupId>
		    <artifactId>ant</artifactId>
		    <version>1.9.4</version>
		</dependency>
  </dependencies>

4、右键工程properties -> Java Compiler 修改 Compiler compliance level:   为 1.8,改完后应该就不会报错了。

5、修改 org.mybatis.generator.internal.DefaultCommentGenerator,自己看有哪些“废话”是要去掉的,直接在这里修改源码即可,要删些什么,自己对照它生成的实体类,可以在类中搜索到那些注释,然后把它们去掉。这个自己去稍微弄一下就OK了。

后面,我发现get set 方法上面有行注释,感觉自己又想去掉,但说不定又想留着。所以就自己加了个配置,灵活配置get、set方法是否需要生成注释:

1、修改DefaultCommentGenerator,加个全局变量 suppressGetSetComments,并在构造中初始化

    
    private boolean suppressGetSetComments;

    private SimpleDateFormat dateFormat;

    public DefaultCommentGenerator() {
        super();
        properties = new Properties();
        suppressDate = false;
        suppressAllComments = false;
        addRemarkComments = false;
        suppressGetSetComments = false;
    }

2、在addConfigurationProperties 方法中,添加获取配置参数的代码:

suppressGetSetComments = isTrue(properties
            .getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_GET_SET_COMMENTS));

3、修改addGetterComment 、addSetterComment 这两个方法中的判断:

        if (suppressAllComments || suppressGetSetComments) {
            return;
        }

4、第2步的 PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_GET_SET_COMMENTS 常量,需要在org.mybatis.generator.config.PropertyRegistry 类中添加一个常量:

    public static final String COMMENT_GENERATOR_SUPPRESS_GET_SET_COMMENTS = "suppressGetSetComments"; //$NON-NLS-1$

5、搞完上面就可以在  generatorConfig.xml  添加  suppressGetSetComments 的配置了,见上面配置文件。

至此,我的需求就达成了。需要使用的话,就编译一下自己这个maven工程,然后取出PropertyRegistry.class 和 DefaultCommentGenerator.class 。用它们替换掉最开始的 mybatis-generator-core-1.3.7.jar 里面的对应的class文件即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值