1.准备工作:
(1)下载mybatis-generator核心jar,地址:https://github.com/mybatis/generator/releases
windows系统下载zip包解压,提取mybatis-generator-core-1.3.6.jar
(2)将下载好的mybatis-generator-core-1.3.6.jar放于一个新的目录,路径不要带中文。在该路径下新建src文件夹,新建generatorConfig.xml,如下图1,generatorConfig.xml的内容如图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>
<!-- 数据库驱动-->
<classPathEntry location="mysql-connector-java-5.1.46-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/payment" userId="root" password="root123">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.aheizi.domain" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.aheizi.mapping" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.aheizi.Dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="pt_tran_msg3" domainObjectName="PtTranMsg3" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
(3)下载mysql连接java的数据库驱动,地址:https://dev.mysql.com/downloads/connector/j/
下载后解压提取:mysql-connector-java-5.1.46-bin.jar,如图3
图3
(4)打开命令行cmd,切到刚才上面的路径,执行命令:java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite,就会看到MyBatis Generator finished successfully.此时在src文件夹下面就生成了对应的数据库实体映射文件。