標籤:
一.先建立資料指令碼,這裡用的mysql資料指令碼
drop table FILMINFO;drop table FILMTYPE;create table FILMINFO( FILMID int primary key auto_increment, FILMNAME VARCHAR(50) not null, TYPEID int not null, ACTOR VARCHAR(255), DIRECTOR VARCHAR(50), TICKETPRICE int not null);alter table FILMINFO auto_increment = 10001;create table FILMTYPE( TYPEID int primary key auto_increment, TYPENAME VARCHAR(20));alter table FILMTYPE auto_increment = 100001;insert into FILMTYPE(TYPENAME) values('愛情片');insert into FILMTYPE(TYPENAME) values('動作片');insert into FILMTYPE(TYPENAME) values('喜劇片');insert into FILMTYPE(TYPENAME) values('戰爭片');insert into FILMTYPE(TYPENAME) values('科幻片');insert into FILMTYPE(TYPENAME) values('恐怖片');insert into FILMTYPE(TYPENAME) values('動畫片');insert into FILMTYPE(TYPENAME) values('其它片');insert into FILMINFO(FILMNAME,TYPEID,ACTOR,DIRECTOR,TICKETPRICE) values('阿凡達', 10005, '薩姆·沃辛頓,佐伊·索爾達娜,西格妮·韋弗,喬·摩爾,拉茲·阿隆索,喬瓦尼·瑞比西', '詹姆斯·卡梅隆', 150);insert into FILMINFO(FILMNAME,TYPEID,ACTOR,DIRECTOR,TICKETPRICE) values('貓和老鼠', 10007, '湯姆,傑瑞', 'William Hanna,Joseph Barbera', 60);insert into FILMINFO(FILMNAME,TYPEID,ACTOR,DIRECTOR,TICKETPRICE) values('大兵小將', 10002, '成龍,王力宏,劉承俊,林鵬,徐冬梅,杜玉明', '丁晟', 50);insert into FILMINFO(FILMNAME,TYPEID,ACTOR,DIRECTOR,TICKETPRICE) values('大偵探福爾摩斯', 10002, '小羅伯特·唐尼,裘德·洛', '蓋·裡奇', 100);insert into FILMINFO(FILMNAME,TYPEID,ACTOR,DIRECTOR,TICKETPRICE) values('全城熱戀', 10001, '謝霆鋒,張學友 ,劉若英,徐若瑄,徐熙媛', '夏永康,陳國輝', 80);insert into FILMINFO(FILMNAME,TYPEID,ACTOR,DIRECTOR,TICKETPRICE) values('第九區', 10005, '沙爾托·科普雷,詹森·庫伯,威廉·艾倫·揚', '尼爾·布洛姆坎普', 100);insert into FILMINFO(FILMNAME,TYPEID,ACTOR,DIRECTOR,TICKETPRICE) values('敢死隊3', 10002, '西爾維斯特·史泰龍,傑森·斯坦森,梅爾·吉布森,李連杰,阿諾·施瓦辛格,杜夫·龍格爾', '派特裡克·休斯', 250);commit;select * from FILMTYPE;select * from FILMINFO;
Linux下操作命令:
create database cinema character set utf8;use cinema;source 全路徑下的指令檔地址
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="D:\software\lib\mysql-connector-java-5.1.21.jar" /> --><classPathEntry location="/home/a/workspace/MavenRepository/mysql/mysql-connector-java/5.1.36/mysql-connector-java-5.1.36.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/sy" userId="sypro" password="sypro"> --><jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/cinema" userId="root" password="a"></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- 產生模型的包名和位置 --><javaModelGenerator targetPackage="sy.model" targetProject="/usr/day01/src"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!-- 產生的對應檔包名和位置 --><sqlMapGenerator targetPackage="sy.mapping" targetProject="/usr/day01/src"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- 產生DAO的包名和位置 --><javaClientGenerator type="XMLMAPPER" targetPackage="sy.dao" targetProject="/usr/day01/src"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- 要產生那些表(更改tableName和domainObjectName就可以) --><table tableName="FILMINFO" domainObjectName="FilmInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /><table tableName="FILMTYPE" domainObjectName="FilmType" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /></context></generatorConfiguration>
要修改的地方,驅動包的路徑,mysql包的路徑,連結資料庫的配置,產生包的路徑,還所最後注釋上說的
<!-- 要產生那些表(更改tableName和domainObjectName就可以) -->
最後寫個運行指令碼,window下建立bat尾碼檔案,Linub下建立sh尾碼檔案
@echo '開始'java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite@echo '結束'@pause
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
mybatis自動產生mapper,dao,對應檔