avatar

目录
总结使用Mybatis-generator遇到的坑

​ 在项目开发的时候,习惯于先定义数据库表,给表字段写好注释,方便自己也方便他人。因为使用Mybatis的话,dao、domain、xml等文件基本上没有逻辑,都是基本的数据库操作语句,也就不想自己手写了(太费劲,也容易出错,想想一个包括20多个字段的表。。。),因此想到使用自动化插件来帮助放飞自我
使用Mybatis-generator插件的方法网上都有,随便一搜就可以看到。它包括如下几个模块:

1、在欲使用Mybatis-generator插件的功能模块的pom.xml中加入以下配置:

xml
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
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 数据库 Mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

2、在该模块的resource下配置生成代码的配置文件generatorConfig.xml

xml
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
<?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>
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>

<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/vhr?characterEncoding=utf-8" userId="root"
password="ppet1205"/>

<javaModelGenerator targetPackage="cn.gov.zcy.sop.extra.search.domain"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<javaClientGenerator targetPackage="cn.gov.zcy.sop.extra.search.dao"
targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<!--
<table tableName="aggs_search" domainObjectName="AggSearch"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
-->
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
<table tableName="announcement_article" domainObjectName="AnnouncementArticle"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>

3、执行生成代码

  • 可以使用命令行执行: mvn mybatis-generator:generate 详情参见
  • 使用idea的话也可以方便点,通过点点按钮即可。

​ 正常情况下此时对于的代码就好自动生成好了。
​ 但是凡事都有意外,小编在这里就遇到几个坑,花费了好长时间,时间就是金钱啊

坑1 - 找不到jar包

错误信息如下:

xml
1
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project sop-extra-center: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Plugin org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2 or one of its dependencies could not be resolved: Could not find artifact mysql:mysql-connector-java:jar:5.1.38 in central (http://repo.maven.apache.org/maven2/) -> [Help 1]

​ 这个错误是告诉我们“mybatis-generator-maven-plugin:1.3.2”或“mysql:mysql-connector-java:jar:5.1.38”这个包没引进来。只要引进来就好了。
​ 没引进maven的原因有多种,小编这里是由于内网访问不到maven仓库,下不下来jar包。这种情况下,如果本地有jar包,也可以指定包位置,就不用从maven仓库拉包了。
​ 如本地有Mybatis-connector包了,只需要在配置文件generatorConfig.xml中加入如下配置即可

xml
1
2
<classPathEntry
location="/Users/menghaohao/.m2/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar"/>

坑2 - Mybatis-generator生成的domain实体类没有注释

​ 这个问题最初也是很无奈的,原因在于Mybatis-generator官方并不直接支持获取数据库表的注释。通过网上查询和阅读源码可以看到,代码生成注释是通过DefaultCommentGenerator类实现的,这个类继承了CommentGenerator接口,因此我们可以自己创建一个类继承CommentGenerator接口,然后重写里面相关方法就可以了(可以复制DefaultCommentGenerator中的代码过来,然后修改我们需要改动的方法)。
实现一个DefaultCommentGenerator接口如下:

java
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
package cn.gov.zcy.sop.extra.search.mq;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.internal.DefaultCommentGenerator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class MybatisGenerator extends DefaultCommentGenerator {
private Properties properties;
private Properties systemPro;
private boolean suppressDate;
private boolean suppressAllComments;
private String currentDateStr;

public MybatisGenerator() {
super();
properties = new Properties();
systemPro = System.getProperties();
suppressDate = false;
suppressAllComments = false;
currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
}

@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}

}

​ 然后在配置文件generatorConfig.xml的中修改如下配置

xml
1
2
3
4
<!-- 使用自定义的插件 -->
<commentGenerator type="com.ilovey.mybatis.comment.MyCommentGenerator">

</commentGenerator>

​ 理论上这样就可以生成带注释的实体类了。但是这个时候一般会遇到这个错误:

xml
1
Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate (default-cli) on project damon: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate failed: Cannot instantiate object of type cn.edu.xxx.xxx.xxx.MyDefaultCommentGenerator -> [Help 1]

​ 经过耐心的搜索和寻找,大致确定了错误。
​ 错误就是,mybatis-generator 的plugin有自己的classpath,我们在项目中直接继承的类和plugin不属于同一个classpath。这一点在官方文档有一点提到,但是官方文档并没有明确说明,当我们自定义的插件该放到哪里。

​ 到这里,问题就解决了。
​ 我们需要吧自己写的插件封装成一个jar,然后在plugin中添加依赖,或者安装到本地,或者项目仓库。

参考解决方案

文章作者: 海东青
文章链接: https://haohaogit.github.io/2020/07/14/%E6%80%BB%E7%BB%93%E4%BD%BF%E7%94%A8Mybatis-generator%E9%81%87%E5%88%B0%E7%9A%84%E5%9D%91/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Hexo
打赏
  • 微信
    微信
  • 支付宝
    支付宝