在使用MyBatis Generator的时候,生成的实体类和mapper文件里出现过多份属性和sql语句,导致报错。原因是什么呢?
这是因为数据库里有多份同名表,导致逆向工程全部生成了,解决办法,在<jdbcConnection></jdbcConnection>
里添加<property name="nullCatalogMeansCurrent" value="true"/>
立马就好使了,
MyBatis Generator具体代码如下:
<?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="DB2Tables" targetRuntime="MyBatis3">
<!-- 注释构建 -->
<commentGenerator>
<!-- 去掉所有的注释 -->
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 数据库四要素 -->
<jdbcConnection connectionURL="jdbc:mysql://localhost:3306/logistics?serverTimezone=Asia/Shanghai"
driverClass="com.mysql.cj.jdbc.Driver"
password="123"
userId="root" >
<!-- MySQL 不支持 schema 或者 catalog 所以需要添加这个,官方文档有 -->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 实体类 : pojo
targetPackage : 实体类生成后存放的包
targetProject : 存放的目录一般都放在 src下面
-->
<javaModelGenerator targetPackage="cn.tmj.logistics.pojo" targetProject="mybatis-generator/src" />
<!-- 映射文件 -->
<sqlMapGenerator targetPackage="cn.tmj.logistics.mapper" targetProject="mybatis-generator/src" />
<!-- 操作接口
type 生成映射的形式
ANNOTATEDMAPPER : 纯注解的,没有xml映射
XMLMAPPER : 生成的有xml映射文件
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.tmj.logistics.mapper" targetProject="mybatis-generator/src" />
<!-- 要生成对应表的配置
tableName : 数据库表名
//如果下面全部是true,mybatis直接可以使用纯面向对象开发
enableCountByExample : 是否生成查询总数的 Example
enableDeleteByExample : 是否生成删除的 Example
enableSelectByExample : 是否生成查询集合的 Example
enableUpdateByExample : 是否生成修改的 Example
-->
<table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
<table tableName="t_role" domainObjectName="Role" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
<table tableName="t_permission" domainObjectName="Permission" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
<table tableName="t_base_data" domainObjectName="BaseData" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
</context>
</generatorConfiguration>
自己纳闷了很久,终于在官方文档找到答案
官方文档链接:
http://mybatis.org/generator/usage/mysql.html
附上图片: