MyBatis-4 逆向工程(generator)
MyBatis的逆向工程(generator)
通过数据库中相对应的表来生成dao,bean,xml映射文件
MyBatis generator官网:“http://www.mybatis.org/generator/index.html"
1.引入 mybatis-generator 的 jar 包,并解压
2.创建 generator 的配置文件
generator.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">
<!-- generato.jar路径 -->
<generatorConfiguration>
<classPathEntry location="E:\\MyBatisGenerator" />
<!-- 加载数据库驱动 -->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 是否生成注解-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="123456">
</jdbcConnection>
<!-- 是否允许大数据 -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成实体类配置 -->
<javaModelGenerator targetPackage="com.huawei.lb.bean" targetProject="./src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成<u>mapper</u>配置文件 -->
<sqlMapGenerator targetPackage="com.huawei.lb.mapper" targetProject="./resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成操作类配置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.huawei.lb.dao" targetProject="./src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 数据库对应的表 -->
<table schema="DB2ADMIN" tableName="Users" domainObjectName="Users" enableCountByExample="flase"
enableDeleteByExample="flase" enableSelectByExample="flase" enableUpdateByExample="flase">
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
</context>
</generatorConfiguration>