1、打开pom.xml文件
(1)在dependencies中加入以下dependency,即导入反向生成所需要的jar包
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.3</version>
</dependency>
(2)在build中加入以下plugin,即maven自带的插件
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--configurationFile为反向生成时所需的配置文件的位置--> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
2、在1-(2)设置的路径下添加相应的配置文件。
<?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>
<!-- mysql-connector-driver的位置,可使用maven的本地仓库 -->
<classPathEntry location="C:\Users\ccc\.m2\repository\mysql\mysql-connector-java\8.0.8-dmr\mysql-connector-java-8.0.8-dmr.jar"/>
<context id="default" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/tablename?serverTimezone=UTC" userId="root"
password="password">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="cn.edu.bnu.coastal.pojo"
targetProject=".\src\main\java">
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="cn.edu.bnu.coastal.mapper"
targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.edu.bnu.coastal.mapper"
targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="table_name"></table>
</context>
</generatorConfiguration>
3、添加maven
Command line中输入 mybatis-generator:generate -e
最后重新返回执行即可。
4、错误总结
(1)Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class is
com.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
解决方法:当使用的mysql connectors的版本较高时,配置文件中classdriver的名称应该设置为“com.mysql.cj.jdbc.Driver”,而不是之前的“com.mysql.jdbc.Driver”。
(2)java.sql.SQLException: 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.
解决方法:将connectionURL加入serverTimezone=UTC,即设置为:设置为”jdbc:mysql://localhost:3306/tablename?serverTimezone=UTC”。
(3)生成的map.xml文件中只有两个方法:
int insert(User record);
int insertSelective(User record);
且在生成的过程中报了无法找到主键的警告:
解决方法:换用低版本的mysql-connector.jar和maven的mybatis-generator-maven-plugin。