聲明:本文針對基礎薄弱的小菜鳥編寫,大神勿噴。
今天為了學習mybatis自動生成代碼,折騰了兩個小時,總算解決了,為此記錄下來,希望大家日後少走彎路:
mybatis-generator的使用方法常用的有兩種,一種是直接在cmd指令行下面運作,運作完後把生成的檔案再拷貝到項目對應的位置,我感覺這個太麻煩了,而且不便于修改,做成插件直接生成映射和實體類到項目裡。插件內建方法:
一、首先是插件的下載下傳和安裝,如下圖:
複制插件包中的features和plugins,
至myeclipse的安裝目錄下的dropins下建立自己目錄下
重新開機Myeclipse,之後如下檢視:
至此,說明插件安裝完成。
一、插件的使用
工程目錄下建立generator的配置檔案
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="F:/SVN/lib/MySQL/mysql-connector-java-5.1.21.jar"/>
- <context id="DB2Tables" targetRuntime="MyBatis3">
- <commentGenerator>
- <property name="suppressDate" value="true"/>
- <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
- <property name="suppressAllComments" value="true"/>
- </commentGenerator>
- <!--資料庫連結URL,使用者名、密碼 -->
- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/training" userId="root" password="root">
- </jdbcConnection>
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false"/>
- </javaTypeResolver>
- <!-- 生成模型的包名和位置-->
- <javaModelGenerator targetPackage="com.glodon.bean" targetProject="SpringMVC_Mybatis_Demo">
- <property name="enableSubPackages" value="true"/>
- <property name="trimStrings" value="true"/>
- </javaModelGenerator>
- <!-- 生成映射檔案的包名和位置-->
- <sqlMapGenerator targetPackage="com.glodon.mapper" targetProject="SpringMVC_Mybatis_Demo">
- <property name="enableSubPackages" value="true"/>
- </sqlMapGenerator>
- <!-- 生成DAO的包名和位置-->
- <javaClientGenerator type="XMLMAPPER" targetPackage="com.glodon.dao" targetProject="SpringMVC_Mybatis_Demo">
- <property name="enableSubPackages" value="true"/>
- </javaClientGenerator>
- <!-- 要生成哪些表-->
- <table tableName="totalass" domainObjectName="AboutDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
- </context>
- </generatorConfiguration>
在配置檔案上右鍵單擊
附件下載下傳位址(内有詳細使用教程):http://download.csdn.net/detail/pengyufight/9611625
參考博文位址:http://blog.csdn.net/u014481096/article/details/45025607