Eclipse使用Mybatis Generator自動生成代碼


准備工作

1. 下載mybatis generator插件:https://dl.bintray.com/mybatis/mybatis-generator/zipped/

2. 解壓zip包,並將其中的features和plugin放到eclipse的dropins下

3. 重啟eclipse,右鍵New--》other 出現如下選項則證明安裝成功

使用插件生成代碼

1. 新建一個名為test的Dynamic Web Project項目,引入Oracle數據庫驅動jar包:ojdbc14.jar

2.在項目名稱上右鍵—》new—》MyBatis Generator Configuration File,生成如下配置文件:

3.對配置文件進行配置

<?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:\generator\mysql-connector-java-5.1.34.jar" /> -->    
    <classPathEntry location="D:\Workspace\Eclipse\test\WebContent\WEB-INF\lib\ojdbc14.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressAllComments" value="true" />  
        </commentGenerator>  
        <!-- 數據庫鏈接URL、用戶名、密碼 -->  
         <!--<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/my_db?characterEncoding=utf8" userId="root" password="123456"> -->   
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="scott" password="tiger"> 
        </jdbcConnection>  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false" />  
        </javaTypeResolver>  
        <!-- 生成模型的包名和位置 -->  
        <javaModelGenerator targetPackage="com.test.model" targetProject="test">  
            <property name="enableSubPackages" value="true" />  
            <property name="trimStrings" value="true" />  
        </javaModelGenerator>  
        <!-- 生成的映射文件包名和位置 -->  
        <sqlMapGenerator targetPackage="com.test.mapping" targetProject="test">  
            <property name="enableSubPackages" value="true" />  
        </sqlMapGenerator>  
        <!-- 生成DAO的包名和位置 -->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" targetProject="test">  
            <property name="enableSubPackages" value="true" />  
        </javaClientGenerator>  
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->  
        <table tableName="admin_division" domainObjectName="AdminDivision" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />  
        <!-- <table tableName="course_info" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />  
        <table tableName="course_user_info" domainObjectName="CourseUserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> -->  
    </context>  
</generatorConfiguration>

4. 生成代碼

在generatorConfig.xml上右鍵—>Run As—>Run Mybatis Generator,即可生成代碼。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM