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

一.先创建数据脚本,这里用的mysql数据脚本

drop table VOTE_ITEM;
drop table VOTE_OPTION;
drop table VOTE_SUBJECT;
drop table VOTE_USER;
 
 
create table VOTE_USER
(
  VU_USER_ID   int(8) primary key,  
  VU_USER_NAME VARCHAR(20) not null,  
  VU_PASSWORD  VARCHAR(20) not null,  
  VU_STATUS    int(6) not null,  
  VU_VERSION   int(8) not null  
);
   
create table VOTE_SUBJECT
(
  VS_ID    int(8) primary key,
  VS_TITLE VARCHAR(200) not null,
  VS_TYPE  int(6) not null
);
 
create table VOTE_OPTION
(
  VO_ID     int(8) primary key,
   VS_ID     int(8) not null,
  VO_OPTION VARCHAR(200) not null,
  VO_ORDER  int(8) not null
);
 
create table VOTE_ITEM
(
  VI_ID      int(8) primary key,
  VS_ID      int(8) not null,
  VO_ID      int(8) not null,
  VU_USER_ID int(8) not null
);
 
insert into VOTE_USER (VU_USER_ID, VU_USER_NAME, VU_PASSWORD, VU_STATUS, VU_VERSION)
values (1001, 'ss', 'a', 1, 1);
insert into VOTE_USER (VU_USER_ID, VU_USER_NAME, VU_PASSWORD, VU_STATUS, VU_VERSION)
values (1002, 'admin', 'a', 2, 1);
insert into VOTE_USER (VU_USER_ID, VU_USER_NAME, VU_PASSWORD, VU_STATUS, VU_VERSION)
values (1003, 'zy', 'a', 1, 0);
insert into VOTE_USER (VU_USER_ID, VU_USER_NAME, VU_PASSWORD, VU_STATUS, VU_VERSION)
values (1004, 'yc', 'a', 2, 0);
insert into VOTE_USER (VU_USER_ID, VU_USER_NAME, VU_PASSWORD, VU_STATUS, VU_VERSION)
values (1005, 'yc1', 'a', 2, 1);
insert into VOTE_USER (VU_USER_ID, VU_USER_NAME, VU_PASSWORD, VU_STATUS, VU_VERSION)
values (1006, 'yc2', 'a', 2, 1);
insert into VOTE_USER (VU_USER_ID, VU_USER_NAME, VU_PASSWORD, VU_STATUS, VU_VERSION)
values (1007, 'yc3', 'a', 2, 1);
insert into VOTE_USER (VU_USER_ID, VU_USER_NAME, VU_PASSWORD, VU_STATUS, VU_VERSION)
values (1008, 'yc4', 'a', 2, 1);
insert into VOTE_USER (VU_USER_ID, VU_USER_NAME, VU_PASSWORD, VU_STATUS, VU_VERSION)
values (1009, 'yc5', 'a', 2, 1);
insert into VOTE_USER (VU_USER_ID, VU_USER_NAME, VU_PASSWORD, VU_STATUS, VU_VERSION)
values (1010, 'yc6', 'a', 1, 0);
 
insert into VOTE_SUBJECT (VS_ID, VS_TITLE, VS_TYPE)
values (103, '选出你心目中最好的下载工具', 2);
insert into VOTE_SUBJECT (VS_ID, VS_TITLE, VS_TYPE)
values (104, '选出你心目中最好的输入法', 1);
insert into VOTE_SUBJECT (VS_ID, VS_TITLE, VS_TYPE)
values (105, '选出你心目中最好的网络聊天工具', 1);
insert into VOTE_SUBJECT (VS_ID, VS_TITLE, VS_TYPE)
values (440, '你最想区的地方', 1);
insert into VOTE_SUBJECT (VS_ID, VS_TITLE, VS_TYPE)
values (101, '选出你心目中最好的浏览器', 1);
insert into VOTE_SUBJECT (VS_ID, VS_TITLE, VS_TYPE)
values (102, '选出你心目中最好的杀毒软件', 1);
 
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (397, '腾讯QQ', 105, 1);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (398, 'MSN', 105, 2);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (380, '迅雷', 103, 1);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (399, '新浪UC', 105, 3);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (400, '飞信', 105, 4);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (401, 'Skype', 105, 5);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (402, '阿里旺旺', 105, 6);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (403, '百度HI', 105, 7);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (404, '微软IE浏览器', 101, 1);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (405, '火狐(Firefox)浏览器', 101, 2);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (381, '网际快车', 103, 2);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (382, '电驴', 103, 3);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (383, '比特精灵', 103, 4);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (384, '超级旋风', 103, 5);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (385, '搜狗拼音', 104, 1);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (386, '搜狗五笔', 104, 2);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (387, 'QQ拼音', 104, 3);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (388, 'QQ五笔', 104, 4);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (389, '谷歌拼音', 104, 5);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (390, '紫光拼音', 104, 6);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (391, '智能ABC', 104, 7);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (392, '微软拼音', 104, 8);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (393, '万能拼音', 104, 9);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (394, '万能五笔', 104, 10);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (395, '极品拼音', 104, 11);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (396, '极品五笔', 104, 12);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (406, '世界之窗浏览器', 101, 3);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (407, '360浏览器', 101, 4);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (408, '傲游浏览器', 101, 5);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (409, '谷歌(Chrome)浏览器', 101, 6);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (410, '卡巴斯基', 102, 1);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (411, '360杀毒', 102, 2);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (412, 'McAfee', 102, 3);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (413, 'AVG', 102, 4);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (414, '诺顿杀毒', 102, 5);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (415, '瑞星杀毒', 102, 6);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (416, '金山毒霸', 102, 7);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (417, '江民杀毒', 102, 8);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (418, '微点防御', 102, 9);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (419, '趋势杀毒', 102, 10);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (441, '地域', 440, 1);
insert into VOTE_OPTION (VO_ID, VO_OPTION, VS_ID, VO_ORDER)
values (442, '天堂', 440, 2);
 
insert into VOTE_ITEM (VI_ID, VO_ID, VS_ID, VU_USER_ID)
values (422, 405, 101, 1005);
insert into VOTE_ITEM (VI_ID, VO_ID, VS_ID, VU_USER_ID)
values (423, 405, 101, 1006);
insert into VOTE_ITEM (VI_ID, VO_ID, VS_ID, VU_USER_ID)
values (424, 404, 101, 1007);
insert into VOTE_ITEM (VI_ID, VO_ID, VS_ID, VU_USER_ID)
values (425, 407, 101, 1008);
insert into VOTE_ITEM (VI_ID, VO_ID, VS_ID, VU_USER_ID)
values (426, 408, 101, 1009);
insert into VOTE_ITEM (VI_ID, VO_ID, VS_ID, VU_USER_ID)
values (427, 409, 101, 1004);
insert into VOTE_ITEM (VI_ID, VO_ID, VS_ID, VU_USER_ID)
values (445, 404, 101, 1003);
insert into VOTE_ITEM (VI_ID, VO_ID, VS_ID, VU_USER_ID)
values (367, 136, 135, 1002);
insert into VOTE_ITEM (VI_ID, VO_ID, VS_ID, VU_USER_ID)
values (420, 405, 101, 1002);
insert into VOTE_ITEM (VI_ID, VO_ID, VS_ID, VU_USER_ID)
values (421, 406, 101, 1001);
 
commit;
 
select * from VOTE_USER;
select * from VOTE_SUBJECT;
select * from VOTE_OPTION;
select * from VOTE_ITEM;

Linux下操作命令:

create database vote  character set utf8;
use vote;
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="/home/a/workspace/MavenRepository/mysql/mysql-connector-java/5.1.36/mysql-connector-java-5.1.36.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、username、password -->
        <!-- <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/vote" userId="root" password="a">   
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="com.yc.vote.entity" targetProject="/usr/local/mybatisutil/src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="/usr/local/mybatisutil/src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.yc.vote.mapper" targetProject="/usr/local/mybatisutil/src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就能够) -->
        <table tableName="VOTE_ITEM" domainObjectName="Item" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="VOTE_OPTION" domainObjectName="Option" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="VOTE_SUBJECT" domainObjectName="Subject" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="VOTE_USER" domainObjectName="User" 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

上面的认为麻烦就直接输入中间那一行代码就能够跑起来了

时间: 2024-11-14 20:00:05

mybatis自己主动生成mapper,dao,映射文件的相关文章

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

一.先创建数据脚本,这里用的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 );

通过mybatis工具generatorConfig.xml自动生成实体,DAO,映射文件

简介 Mybatis属于半自动ORM,可以利用mybatis工具generatorConfig.xml自动生成DAO.实体.映射文件的方式来代替手动书写的方式,这样既提高了工作效率也可以在项目避免出现的一些细微难调试的BUG. 前提条件: 1.需要准备的第三方jar包为: mybatis-generator-core-1.3.2.jar和mysql-connector-java-5.1.39-bin.jar, 其中mybatis-generator-core-1.3.2.jar的下载地址为: h

MyBatis——Mapper.xml映射文件

Mapper.xml映射文件 转载:http://loveshisong.cn/mybatis/2015/01/22/MyBatis(%E4%B8%89)Mapper.xml%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6.html 本文结构 select 语句简介 insert update delete 简介 Parameters 参数 ResultMap select 语句简介 查询语句是 MyBatis 中最常用的元素之一,先来个例子 <select id="

Mybatis自己主动生成代码

在mybatis自己主动生成代码有两种方式: 方式一:是通过使用eclipse作为开发工具.採用maven来构建项目生成的.以下的演示是通过第一种方式. 今天来记录下mybatis-generator插件自己主动生成xml mapper和model还有dao接口,这样我们就能够把精力放在业务代码上.而不须要关心数据库的详细操作. 这里我使用eclipse作为开发工具,採用maven来构建项目. 1. 编写pom.xml文件 这里仅仅有3个jar文件,数据库使用mysql <project xml

mybatis generator自动生成Mapper文件和Model

使用Mybatis generator可以根据数据库表生成mybatis对应的Mapper和Model,生成的Example可以满足一般的单表操作,使用Mybatis的注释,利用@mbggenerated可以在下次生成的时候将自动生成的代码覆盖掉,对于手工加入的查询不影响(需要使用的eclipse的插件,intellij目前不存在类似的插件),在使用intellij中,可以使用免费的mybatis插件,可以在Mapper类中快速定位到对应的xml中,非常有利于sql查看: Mybatis配置:

Mybatis配置文件和实体类的映射文件,以及dao层的书写

mybatis-config.xml,配置文件,用于连接数据库.底层使用的是oracle数据库的emp表 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd&

mybatis Mapper XML 映射文件

传送门:mybatis官方文档 Mapper XML 文件详解 1. select <select id="selectPerson" parameterType="int" parameterMap="deprecated" resultType="hashmap" resultMap="personResultMap" flushCache="false" useCache=&

springboot 里mybatis generator逆向工程生成mapper 实体类

网上看到很多方法,效果都不明显,甚至无法执行,特意写了博客,也加深自己的印象. 下面给大家介绍一种偷懒的生成工具使用--mybatis-generator,使用此插件可以从数据库生成指定表格对应的实体类.mapper接口以及对应的xml文件(包含一些常用的方法实现). 首先是通过maven添加插件引用 新建配置文件generatorConfig.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYP

MyBatis学习(三)-XML映射文件(上)

XML映射文件即Mapper XML 有下列几个顶级元素: cache - 给定命名空间的缓存配置 cache-ref - 缓存配置的引用 resultMap - 描述结果集,是最强大也是最复杂的元素(没有之一) sql - 可复用的语句块 insert update delete select(从前往后说,本次是cache.cache-ref) MyBatis缓存机制 # 一级缓存(默认开启,SqlSession级别的缓存) 我们通过MyBatis每连接一次数据库就会创建一个SqlSessio