mybatis自动生成mapper,dao,映射文件
2015-08-13 16:06
357 查看
一.先创建数据脚本,这里用的mysql数据脚本
[java] view
plaincopy
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下操作命令:
[plain] view
plaincopy
create database cinema character set utf8;
use cinema;
source 全路径下的脚本文件地址
generator.xml文件
[java] view
plaincopy
<?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包的路径,链接数据库的配置,生成包的路径,还所最后注释上说的
[java] view
plaincopy
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
最后写个运行脚本,window下创建bat后缀文件,Linub下创建sh后缀文件
[java] view
plaincopy
@echo '开始'
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
@echo '结束'
@pause
[java] view
plaincopy
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下操作命令:
[plain] view
plaincopy
create database cinema character set utf8;
use cinema;
source 全路径下的脚本文件地址
generator.xml文件
[java] view
plaincopy
<?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包的路径,链接数据库的配置,生成包的路径,还所最后注释上说的
[java] view
plaincopy
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
最后写个运行脚本,window下创建bat后缀文件,Linub下创建sh后缀文件
[java] view
plaincopy
@echo '开始'
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
@echo '结束'
@pause
相关文章推荐
- Android SQL基础入门
- iOS 静态库和动态库.a和framework
- iOS开发 关于终端-4CocoaPods安装和使用(图文并茂版)
- android四种启动模式
- Android基于mAppWidget实现手绘地图(三)--环境搭建
- PlistBuddy a good friend to handle iOS plist file
- [置顶] android jni 调用
- android Activity中如何获取Context对象
- webapp div click事件不能响应的解决办法
- Android 文件加密及解密手册,方便查询记忆
- android SQLite使用SQLiteOpenHelper类对数据库进行操作
- 在Android中applicationId与package name的那些事
- Android PopupWindow实现,类似于iOS的选择栏
- Android PopupWindow实现,类似于iOS的选择栏
- android获得屏幕高度和宽度
- iOS tabbar 上面更换任意图
- ios 7 statusbar 状态栏 跟随navigationbar的颜色而改变
- Android 高仿 QQ5.0 侧滑菜单效果
- 学习android编程之路(7)- gallery+baseAdapter+ImageSwitcher数据从assets中获取
- 隐藏android的自带键盘