转自: https://blog.csdn.net/lisheng19870305/article/details/89539482
mybatis-generator自动生成代码插件使用详解
mybatis-generator是一款在使用mybatis框架时,自动生成model,dao和mapper的工具,很大程度上减少了业务开发人员的手动编码时间,今天自己研究了一下,也分享一下使用心得供大家简单使用。
本人使用的是maven构建,首先需要在pom.xml文件添加mybatis-generator依赖包以及插件,配置如下:
dependencies中添加:
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
在build的plugins中添加:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!-- mybatis用于生成代码的配置文件 -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--<scope>runtime</scope>-->
<version>5.1.39</version>
</dependency>
</dependencies>
</plugin>
上面就是pom.xml中需要添加的配置,接下来就是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="D:/new_repository/mysql/mysql-connector-java/5.1.44/mysql-connector-java-5.1.44.jar" /> -->
<context id="MySQLTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- <property name="suppressDate" value="false" /> -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- mysql数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/dbname" userId="*****"
password="******" />
<!-- 生成model实体类文件位置 -->
<javaModelGenerator
targetPackage="com.dto"
targetProject="D:/tmp/generator/">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成mapper.xml配置文件位置 -->
<sqlMapGenerator
targetPackage="com.dao"
targetProject="D:/tmp/generator/">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成mapper接口文件位置 -->
<javaClientGenerator
targetPackage="com.dao"
targetProject="D:/tmp/generator/"
type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 需要生成的实体类对应的表名,多个实体类复制多份该配置即可 -->
<table tableName="tableName" domainObjectName="dtoName"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
到此为止,所有的配置已完毕,如果在ecplise中使用,则右击工程,maven build,添加命令mybatis-generator:generate,代码生成完毕!
常见问题:
错误信息1:
Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project springdatajpa: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate failed: Exception getting JDBC Driver: com.mysql.cj.jdbc.Driver -> [Help 1]
解决方法1:
在plugin中添加mysql-connector-java的dependency,被证明是有效的.
<plugins>
<!-- mybatis-generator自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--<scope>runtime</scope>-->
<version>8.0.13</version>
</dependency>
</dependencies>
</plugin>
</plugins>
错误信息2:
Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project springdatajpa: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. -> [Help 1]
解决方法2:
之前的url
connectionURL="jdbc:mysql://localhost:3306/mybatis
修改generatorConfig.xml中数据库连接url为:
注意字节在xml中写&,会因为未转义而报错要将&写成&
connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8"
参考文章:https://www.cnblogs.com/handsomeye/p/6268513.html
参考文章:https://blog.csdn.net/u014330421/article/details/78805087
参考文章:https://blog.csdn.net/ShelleyLittlehero/article/details/86217272
---------------------
作者:狂奔的蜗牛Evan
来源:CSDN
原文:https://blog.csdn.net/lisheng19870305/article/details/89539482
版权声明:本文为博主原创文章,转载请附上博文链接!