第一步:如果你的eclipse中沒有安裝mybatis外掛程式,需要先安裝mybatis外掛程式,外掛程式下載地址:點擊開啟連結
第二步:下載完外掛程式後,解壓為以下兩個檔案
第三步:找到eclipse的安裝目錄,將以上兩個檔案,拷貝到eclipse的安裝目錄中
如果安裝目錄中已經存在這兩個檔案,則把這兩個檔案進行合并即可。
第四步:重新啟動eclipse
點擊'New--other',即可找到mybatis在eclipse中的外掛程式
第五步:選中Mybatis Generator Configuration File 點擊 next 建立設定檔資訊
選擇要將檔案建立的位置,點擊 finish
第六步:在建立的檔案中配置響應的資訊,即可
註:設定檔解析
<pre name="code" class="html"><?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 ><!-- 引入設定檔 --><properties resource="db.properties"/><!-- 指定資料連線驅動jar地址 --><classPathEntry location="${classpath}"/><!-- 一個資料庫一個context --> <context id="context1" > <!-- 注釋 --> <commentGenerator> <property name="suppressAllComments" value="false"/><!-- 是否取消注釋 --> <property name="supperssDate" value="true"/><!-- 是否產生注釋代碼時間戳記 --> </commentGenerator> <!-- jdbc串連 --> <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}" /> <!-- 類型轉換 --> <javaTypeResolver> <!-- 是否使用bigDecimals,false可自動轉化以下類型(Long,Integer,Short,ets..) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 產生實體類地址 --> <javaModelGenerator targetPackage="com.sky.ssm.po" targetProject="${project}"> <!-- 是否在當前路徑下新加一層schema,eg: fase路徑:com.sky.ssm.po ; true路徑:com.sky.ssm.po.[shemaName] --> <property name="enableSubPackages" value="false"/> <!-- 是否針對string類型的欄位在set的時候進行trim調用 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 產生mapxml檔案 --> <sqlMapGenerator targetPackage="com.sky.ssm.mapper" targetProject="${project}"> <!-- 是否在當前路徑下新加一層schema,eg: fase路徑:com.sky.ssm.mapper ; true路徑:com.sky.ssm.mapper.[shemaName] --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 產生mapxml對應client,也就是介面dao --> <javaClientGenerator targetPackage="com.sky.ssm.mapper" targetProject="${project}" type="XMLMAPPER" > <!-- 是否在當前路徑下新加一層schema,eg: fase路徑:com.sky.ssm.mapper ; true路徑:com.sky.ssm.mapper.[shemaName] --> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 配置表資訊 --> <!-- schema即為資料庫名; tableName為對應的資料庫表 ;domainObjectName是要產生的實體類 ;enable*ByExample是否產生 example類 --> <table schema="mybatis" tableName="items" domainObjectName="Items" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <!-- 忽略列,不產生bean欄位 --><!-- <ignoreColumn column="FRED"/> --> <!-- 指定列的java資料類型 --><!-- <columnOverride column="PRICE" javaType="double" /> --> </table> </context></generatorConfiguration>
db.properties
jdbc.username = rootjdbc.password = 123456jdbc.url = jdbc:mysql://localhost:3306/mybatisjdbc.driver = com.mysql.jdbc.Driver#Mybatis Generator configuration project = SpringmvcAndMybatisclasspath = G:/MyProject/SpringmvcAndMybatis/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.20-bin.jar
第七步:逆向產生代碼
運行成功後,在響應的包中就可以看到響應的mapper和model檔案了