码迷,mamicode.com
首页 > 移动开发 > 详细

mybatis自动生成mapper,dao,映射文件

时间:2015-08-13 12:20:38      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

一.先创建数据脚本,这里用的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,映射文件

标签:

原文地址:http://blog.csdn.net/ac_great/article/details/47606697

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!