天天看點

ibatis和mybatis 自動生成代碼

在myeclipse中示例

一:ibatis自動生成代碼

安裝下面的插件:http://ibatis.apache.org/tools/abator

ibatis和mybatis 自動生成代碼

配置檔案如下:abatorConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN" "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >
<abatorConfiguration >
  <abatorContext >
  <!--  資料庫連接配接的資訊:驅動類、連接配接位址、使用者名、密碼-->
    <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@192.168.4.136:1521:sgmparts" userId="flycartest" password="flycartest" >
  <!--classPathEntry 資料庫的JDBC驅動的jar包位址-->
    <classPathEntry location="E:\MyWorkSpace\flycar-parent\web-demo\lib\ojdbc6.jar" />
    </jdbcConnection>
	<!--生成java實體類對應的項目以及所在的包-->
    <javaModelGenerator targetPackage="test.model" targetProject="web-demo" />
    <!--生成mapper.xml對應的項目以及所在的包-->
    <sqlMapGenerator targetPackage="test.mapper" targetProject="web-demo" />
    <!--生成dao層java類對應的項目以及所在的包-->
    <daoGenerator targetPackage="test.dao" targetProject="web-demo" type="GENERIC-CI" />
	<!--資料相關表配置,schema即為資料庫名,tableName為表名,domainObjectName是要生成的實體類-->
    <table schema="" tableName="Student" domainObjectName="Student">    </table>
  </abatorContext>
</abatorConfiguration>
           

然後右鍵單擊選擇

ibatis和mybatis 自動生成代碼

這樣就OK了,自動生成了相關代碼

二:mybatis自動生成代碼

首先配置maven插件:pom.xml

<plugins>  
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
	</plugins>
           

然後是配置檔案: generatorConfig.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="E:\MyWorkSpace\flycar-parent\web-demo\lib\ojdbc6.jar" />  -->
    <!-- classPathEntry:資料庫的JDBC驅動的jar包位址--> 
    <classPathEntry location="E:\MyWorkSpace\mysql-connector-java-5.1.6-bin.jar" />  
  
    <context id="DB2Tables" targetRuntime="MyBatis3">  
  
        <commentGenerator>  
            <property name="suppressDate" value="true" />  
<!--             是否去除自動生成的注釋 true:是 : false:否   -->
   			<property name="suppressAllComments" value="true" />
        </commentGenerator>  
        <!--資料庫連接配接的資訊:驅動類、連接配接位址、使用者名、密碼 --> 
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
            connectionURL="jdbc:mysql://127.0.0.1:3306/develop" userId="develop" password="develop">  
        </jdbcConnection>  

        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false" />  
        </javaTypeResolver>  
  
        <javaModelGenerator targetPackage="com.raxtone.flycar.admin.entity"  
            targetProject="E:\MyWorkSpace\flycar-parent\web-demo\src\main\java"> 
            <!-- enableSubPackages:是否讓schema作為包的字尾 -->       
      		<property name="enableSubPackages" value="true" />  
   			<!-- 從資料庫傳回的值被清理前後的空格  -->   
      		<property name="trimStrings" value="true" />   
        </javaModelGenerator>  
  
        <sqlMapGenerator targetPackage="com.raxtone.flycar.admin.mapper"  
            targetProject="E:\MyWorkSpace\flycar-parent\web-demo\src\main\java">
            <!-- enableSubPackages:是否讓schema作為包的字尾 -->   
            <property name="enableSubPackages" value="false" />  
        </sqlMapGenerator>  
  
        <javaClientGenerator type="XMLMAPPER"  
            targetPackage="com.raxtone.flycar.admin.mapper" targetProject="E:\MyWorkSpace\flycar-parent\web-demo\src\main\java">  
            <!-- enableSubPackages:是否讓schema作為包的字尾 --> 
            <property name="enableSubPackages" value="true" />  
        </javaClientGenerator>  
        <!-- tableName:用于自動生成代碼的資料庫表;domainObjectName:對應于資料庫表的javaBean類名 -->

	  <!--  schema即為資料庫名, tableName為對應的資料庫表, domainObjectName是要生成的實體類,
			如果想要mapper配置檔案加入sql的where條件查詢, 可以将enableCountByExample等設為true,
			這樣就會生成一個對應domainObjectName的Example類, enableCountByExample等設為false時,
			就不會生成對應的Example類了. -->
	        <table tableName="t_apply_person" domainObjectName="ApplyPerson" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >  </table>  
    </context>  
</generatorConfiguration> 
           

然後執行下圖操作

mybatis-generator:generate

ibatis和mybatis 自動生成代碼