前言 | 使用官方網(wǎng)站的Mapper自動(dòng)生成工具mybatis-generator-core-1.3.2來生成po類和Mapper映射文件
一亩冬、什么是逆向工程
簡(jiǎn)單點(diǎn)說,就是根據(jù)數(shù)據(jù)庫表,生成對(duì)應(yīng)的po類竟贯,配置文件(mapper.xml),接口方法(mapper.java)逝钥,注意這里只生成單表增刪改查方法澄耍,多對(duì)多關(guān)聯(lián)的接口和配置要自己在配置文件添加。
企業(yè)中晌缘,逆向工程是個(gè)很常用的工具,Mybatis官方提供了逆向工程痢站,可以針對(duì)單表自動(dòng)生成mybatis代碼磷箕,比我們手動(dòng)創(chuàng)建映射文件的配置信息方便很多。
二阵难、 導(dǎo)入逆向工程
github下載逆向工程 逆向工程岳枷,將工程導(dǎo)入到eclipse
目錄結(jié)構(gòu)
三、配置文件詳解(重點(diǎn))
<?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>
<!-- context元素用于指定生成一組對(duì)象的環(huán)境呜叫。targetRuntime:此屬性用于指定生成的代碼的運(yùn)行時(shí)環(huán)境空繁。MyBatis3:*這是默認(rèn)值*-->
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--數(shù)據(jù)庫連接的信息:驅(qū)動(dòng)類、連接地址朱庆、用戶名盛泡、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ebuyshop_?allowMultiQueries=true" userId="root"
password=""root"">
</jdbcConnection>
<!-- 如使用oracle請(qǐng)參考如下 -->
<!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
userId="root"
password="root">
</jdbcConnection> -->
<!-- 默認(rèn)false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer娱颊,為 true時(shí)把JDBC DECIMAL 和
NUMERIC 類型解析為java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO類的位置 -->
<javaModelGenerator targetPackage="org.lin.entity"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
<!-- 從數(shù)據(jù)庫返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="org.lin.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="org.lin.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定數(shù)據(jù)庫表 -->
<table tableName="category" schema="" enableCountByExample="false"
enableDeleteByExample="false" enableUpdateByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="productbuy" schema="" enableCountByExample="false"
enableDeleteByExample="false" enableUpdateByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="productsale" schema="" enableCountByExample="false"
enableDeleteByExample="false" enableUpdateByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<!-- 有些表的字段需要指定java類型
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->
</context>
</generatorConfiguration>
-
注意要修改的地方:
1)傲诵、數(shù)據(jù)庫連接池的內(nèi)容(端口號(hào),數(shù)據(jù)庫名稱箱硕,用戶名拴竹,密碼)
2)、修改要生成的數(shù)據(jù)庫表
3)剧罩、pojo文件所在包路徑
4)栓拜、Mapper所在的包路徑
修改完成直接運(yùn)行GeneratorSqlmap.java文件里面的main方法即可。