Tips:Mybatis generator官网 http://www.mybatis.org/generator/configreference/commentGenerator.html
Mybatis的逆向工程的作用是可由表帮我们生成bean,dao,xml映射文件
1. 引入Mybatis generator的jar包
获取jar包
将此jar包放到lib文件夹下,并将其build path
2.创建并获取generator配置文件
接下来,修改generator.xml配置文件,具体内容及释义见下代码
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 <!-- generator配置文件 --> 6 <generatorConfiguration> 7 <!-- mysql驱动jar所在位置 --> 8 <classPathEntry location="D:\\jarorpackage\\mysql\\mysql-connector-java-5.1.47.jar" /> 9 10 <!-- 数据源的信息 --> 11 <context id="DB2Tables" targetRuntime="MyBatis3"> 12 <commentGenerator> 13 <property name="suppressAllComments" value="true" /> 14 </commentGenerator> 15 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 16 connectionURL="jdbc:mysql://localhost:3306/mybatis" 17 userId="root" 18 password="root"> 19 </jdbcConnection> 20 21 <javaTypeResolver > 22 <property name="forceBigDecimals" value="false" /> 23 </javaTypeResolver> 24 25 <!-- 生成的实体类所在位置 --> 26 <javaModelGenerator targetPackage="com.zhiyou100.zhl.bean" targetProject="./src"> 27 <property name="enableSubPackages" value="true" /> 28 <property name="trimStrings" value="true" /> 29 </javaModelGenerator> 30 31 <!-- 生成的映射文件所在位置 --> 32 <sqlMapGenerator targetPackage="com.zhiyou100.zhl.mapper" targetProject="./resources"> 33 <property name="enableSubPackages" value="true" /> 34 </sqlMapGenerator> 35 36 <!-- 生成的Dao文件所在位置 --> 37 <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou100.zhl.dao" targetProject="./src"> 38 <property name="enableSubPackages" value="true" /> 39 </javaClientGenerator> 40 41 <!-- 某张表与实体类的对象关系 42 schema:该表所在的数据库 43 tableName:表名 44 domainObjectName:实体类名 45 --> 46 <table schema="mybatis" tableName="users" domainObjectName="Users" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false"> 47 <property name="useActualColumnNames" value="true"/> 48 <generatedKey column="ID" sqlStatement="DB2" identity="true" /> 49 <columnOverride column="DATE_FIELD" property="startDate" /> 50 <ignoreColumn column="FRED" /> 51 <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> 52 </table> 53 54 </context> 55 </generatorConfiguration>
3.获取并运行generator文件
最后运行此代码,即可生成bean,dao,xml映射文件。