使用反向生成器可以生成數據庫表對應的實體類和mapper映射文件:
以下是具體介紹相應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:/DMCworkspace/z_DMC/src/mysql-connector-java-5.1.7-bin.jar" /> //用於指定加載內省數據庫驅動的路徑
<context id="aim-generator-mybatis3" defaultModelType="flat"> //defaultModelType指定如何生成實體類,flat:該模型為每一張表只生成一個實體類。這個實體類包含表中的所有字段
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<property name="javaFileEncoding" value="UTF-8"/>
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.17.11:3306/dmc" userId="dmc" password="dmc">
</jdbcConnection> //加載驅動
<javaModelGenerator targetPackage="bz.sunlight.dmcEntity" targetProject="z_DMC">
<property name="trimStrings" value="true" /> //trimStrings:是否對數據庫查詢結果進行trim操作,如果設置為true就會生成類似這樣public void setUsername(String username) {this.username = username == null ? null : username.trim();}的setter方法。默認值為false。
</javaModelGenerator>
<sqlMapGenerator targetPackage="bz.sunlight.dao" targetProject="z_DMC">
</sqlMapGenerator>
<javaClientGenerator targetPackage="bz.sunlight.dao" targetProject="z_DMC" type="XMLMAPPER">
<property name="exampleMethodVisibility" value="public" />
</javaClientGenerator>
<table schema="dmc" tableName="vehicle_list"> //tableName:指定要生成的表名,可以使用SQL通配符匹配多個表。生成所有的表可以這樣設置<table tableName="%" />,可以指定的表明生成指定的實體類和映射
</table>
<!--
<table schema="dmc" tableName="vehicle" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<columnOverride column="Id" property="id" />
</table>
-->
</context>
</generatorConfiguration>
