eclipse使用mybatis generator自動生成程式碼
阿新 • • 發佈:2019-01-08
準備工作
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,生成如下相應程式碼