除了hibernate可以生成model、xml配置之外,mybatis也提供了这一个功能。要使用生成代码的功能要用到Mybatis-Generator。
1.想要生成代码,首先要安装mybatis-Generator这个插件,安装的方式有两种,你可以在eclipse中在线安装插件,你也可以把插件的内容下载到本地再把它拷贝到eclipse的安装目录的相对应文件中去。不管是在线安装还是下载插件安装都要重启eclipse
2.安装完插件之后,你首先要新建一个generatorConfig.xml的文件,文件内容如下
<?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
6 <generatorConfiguration>
7 <classPathEntry
8 location="C:/Oracle/Middleware/wlserver_10.3/server/lib/ojdbc6.jar"/>
9 <context id="my" targetRuntime="MyBatis3">
10 <commentGenerator>
11 <property name="suppressDate" value="false"/>
12 <property name="suppressAllComments" value="true"/>
13 </commentGenerator>
14
15 <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
16 connectionURL="jdbc:oracle:thin:@172.20.16.***:1521:CARGO" userId="***"
17 password="***"/>
18
19 <javaModelGenerator targetPackage="ctas.test.entity"
20 targetProject="D:/yangjm/Code/CTAS/JAVAEE/CTAS2CCSP/src/main/java">
21 <property name="enableSubPackages" value="true"/>
22 <property name="trimStrings" value="true"/>
23 </javaModelGenerator>
24
25 <sqlMapGenerator targetPackage="ctas.test.entity.xml"
26 targetProject="D:/yangjm/Code/CTAS/JAVAEE/CTAS2CCSP/src/main/java">
27 <property name="enableSubPackages" value="true"/>
28 </sqlMapGenerator>
29
30 <javaClientGenerator targetPackage="ctas.test.mapper"
31 targetProject="D:/yangjm/Code/CTAS/JAVAEE/CTAS2CCSP/src/main/java" type="XMLMAPPER">
32 <property name="enableSubPackages" value="true"/>
33 </javaClientGenerator>
34
35 <!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill"
36 enableCountByExample="false" enableUpdateByExample="false"
37 enableDeleteByExample="false" enableSelectByExample="false"
38 selectByExampleQueryId="false"/>-->
39
40 <table tableName="CTAS_FEE_BASE" domainObjectName="FeeBase"
41 enableCountByExample="false" enableUpdateByExample="false"
42 enableDeleteByExample="false" enableSelectByExample="false"
43 selectByExampleQueryId="false">
44 <!--<columnRenamingRule searchString="^D_"
45 replaceString=""/>-->
46 </table>
47
48 </context>
49 </generatorConfiguration>
如果你想要了解xml文件中的每个配置的含义,请参考这篇文章:
http://blog.youkuaiyun.com/isea533/article/details/42102297
3.最后生成代码