首页 > 代码库 > mybatis自己主动生成mapper,dao,映射文件

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=http://www.mamicode.com/"true" /> >
要改动的地方,驱动包的路径,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,映射文件