一.eclipse Mybatis generator 外掛程式安裝
二.MyBatis generator (postgres)—–產生Dto、Dao、Mapping
一.eclipse Mybatis generator 外掛程式安裝
1.自動安裝
—-開啟eclipse,點擊Help>Software Update
—-選擇 “Available Software” 標籤,點擊 “Add Site” 按鈕
—-輸入以下資訊:
Location:http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/
—-點擊ok,自動進入 “mybatis generator Feature”
—-點擊“install”按鈕進行安裝。。。。mybatis generator 外掛程式安裝完成
2.手動安裝
—-教程http://jingyan.baidu.com/article/9faa7231506ed8473c28cbee.html
—-外掛程式下載http://jingyan.baidu.com/article/9faa7231506ed8473c28cbee.html
二.MyBatis generator (postgres)—–產生Dto、Dao、Mapping
由於MyBatis屬於一種半自動的ORM架構,所以主要的工作將是書寫Mapping對應檔,但是由於手寫對應檔很容易出錯,所以查資料發現有現成的工具可以自動產生底層模型類、Dao介面類甚至Mapping對應檔.
—–創建表
CREATE TABLE worker (
id varchar(50) NOT NULL,
username varchar(18) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
password varchar(18) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf-8;
—–配置generator config
generatorConfig.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="/home/peng.zhang/zpeng/sources/myjars/postgresql-9.4-1201.jdbc4.jar" /> <context id="context1"> <jdbcConnection driverClass="org.postgresql.Driver" connectionURL="jdbc:postgresql://127.0.0.1:5432/postgres" userId="postgres" password="123456" /> <!-- 產生模型的包名和位置--> <javaModelGenerator targetPackage="com.travelzen.entity" targetProject="MybatisTOPostgresql" /> <!-- 產生對應檔的包名和位置--> <sqlMapGenerator targetPackage="mybatis" targetProject="MybatisTOPostgresql" /> <!-- 產生DAO的包名和位置--> <javaClientGenerator targetPackage="com.travel.dao" targetProject="MybatisTOPostgresql" type="XMLMAPPER" /> <!-- 要產生哪些表--> <table tableName="worker" domainObjectName="WorkerDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <!-- <table schema="zpeng" tableName="stu" domainObjectName="Student" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"> <columnOverride column="" property="" /> </table> --> </context></generatorConfiguration>
—–產生(兩種方式)
1.右鍵設定檔運行:
2.命令:java -jar /Users/liqiu/git/study/web/soft/mybatis-generator-core-1.3.2/lib/mybatis-generator-core-1.3.2.jar -configfile /Users/liqiu/git/study/web/soft/mybatisGeneratorConfig.xml -overwrite
*無法正確產生看這裏:
http://www.cnblogs.com/AloneSword/p/3412236.html
1.XML Parser Error on line 13: XML 文檔結構必須從頭至尾包含在同一個實體內。
貌似設定檔不能加註釋。
2.Unexpected error while running MyBatis Generator. Exception getting JDBC Driver
查閱generator官方文檔發現指定Jar包路徑即可
3.eneration Warnings Occured Table configuration with catalog null, schema null, and table T_LOGIN did not resolve to any tables
忘了建表,資料庫中沒表當然解析不了………建立表,解析成功,毫無壓力