admin 管理员组文章数量: 887021
mybatis自动生成mapper,dao,映射文件
一.先创建数据脚本,这里用的mysql数据脚本
[java] view plain copy
- 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 plain copy
- create database cinema character set utf8;
- use cinema;
- source 全路径下的脚本文件地址
generator.xml文件
[java] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis//DTD MyBatis Generator Configuration 1.0//EN" ".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 plain copy
- <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
[java] view plain copy
- @echo '开始'
- java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
- @echo '结束'
- @pause
本文标签: mybatis自动生成mapper Dao 映射文件
版权声明:本文标题:mybatis自动生成mapper,dao,映射文件 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1700295573h384462.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论