天天看點

Myeclipse中Mybatis Generator自動建立代碼總結

聲明:本文針對基礎薄弱的小菜鳥編寫,大神勿噴。

今天為了學習mybatis自動生成代碼,折騰了兩個小時,總算解決了,為此記錄下來,希望大家日後少走彎路:

mybatis-generator的使用方法常用的有兩種,一種是直接在cmd指令行下面運作,運作完後把生成的檔案再拷貝到項目對應的位置,我感覺這個太麻煩了,而且不便于修改,做成插件直接生成映射和實體類到項目裡。插件內建方法:

一、首先是插件的下載下傳和安裝,如下圖:

複制插件包中的features和plugins,

Myeclipse中Mybatis Generator自動建立代碼總結

至myeclipse的安裝目錄下的dropins下建立自己目錄下

Myeclipse中Mybatis Generator自動建立代碼總結

重新開機Myeclipse,之後如下檢視:

Myeclipse中Mybatis Generator自動建立代碼總結

至此,說明插件安裝完成。

一、插件的使用

工程目錄下建立generator的配置檔案

Myeclipse中Mybatis Generator自動建立代碼總結

xml配置檔案内容如下,使用時去除注釋

  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE generatorConfiguration    
  3.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"    
  4.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">    
  5. <generatorConfiguration>    
  6. <!-- 資料庫驅動-->    
  7.     <classPathEntry  location="F:/SVN/lib/MySQL/mysql-connector-java-5.1.21.jar"/>    
  8.     <context id="DB2Tables"  targetRuntime="MyBatis3">    
  9.         <commentGenerator>    
  10.             <property name="suppressDate" value="true"/>    
  11.             <!-- 是否去除自動生成的注釋 true:是 : false:否 -->    
  12.             <property name="suppressAllComments" value="true"/>    
  13.         </commentGenerator>    
  14.         <!--資料庫連結URL,使用者名、密碼 -->    
  15.         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/training" userId="root" password="root">    
  16.         </jdbcConnection>    
  17.         <javaTypeResolver>    
  18.             <property name="forceBigDecimals" value="false"/>    
  19.         </javaTypeResolver>    
  20.         <!-- 生成模型的包名和位置-->    
  21.         <javaModelGenerator targetPackage="com.glodon.bean" targetProject="SpringMVC_Mybatis_Demo">    
  22.             <property name="enableSubPackages" value="true"/>    
  23.             <property name="trimStrings" value="true"/>    
  24.         </javaModelGenerator>    
  25.         <!-- 生成映射檔案的包名和位置-->    
  26.         <sqlMapGenerator targetPackage="com.glodon.mapper" targetProject="SpringMVC_Mybatis_Demo">    
  27.             <property name="enableSubPackages" value="true"/>    
  28.         </sqlMapGenerator>    
  29.         <!-- 生成DAO的包名和位置-->    
  30.         <javaClientGenerator type="XMLMAPPER" targetPackage="com.glodon.dao" targetProject="SpringMVC_Mybatis_Demo">    
  31.             <property name="enableSubPackages" value="true"/>    
  32.         </javaClientGenerator>    
  33.         <!-- 要生成哪些表-->    
  34.         <table tableName="totalass" domainObjectName="AboutDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>    
  35.         </context>    
  36. </generatorConfiguration> 

在配置檔案上右鍵單擊

Myeclipse中Mybatis Generator自動建立代碼總結

附件下載下傳位址(内有詳細使用教程):http://download.csdn.net/detail/pengyufight/9611625

參考博文位址:http://blog.csdn.net/u014481096/article/details/45025607

若有任何問題,請進企鵝群交流:589847567

繼續閱讀