使用generator自动生成mybatis model、mapper.xml、mapper等(转)
原文链接:http://www.cnblogs.com/lichenwei/p/4145696.html
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。
1、相关文件
关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases
由于我使用的是Mysql数据库,这里需要再准备一个连接mysql数据库的驱动jar包
以下是相关文件截图:
和Hibernate逆向生成一样,这里也需要一个配置文件:
generatorConfig.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE generatorConfiguration
3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
5 <generatorConfiguration>
6 <!--数据库驱动-->
7 <classPathEntry location="mysql-connector-java-5.0.8-bin.jar"/>
8 <context id="DB2Tables" targetRuntime="MyBatis3">
9 <commentGenerator>
10 <property name="suppressDate" value="true"/>
11 <property name="suppressAllComments" value="true"/>
12 </commentGenerator>
13 <!--数据库链接地址账号密码-->
14 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root">
15 </jdbcConnection>
16 <javaTypeResolver>
17 <property name="forceBigDecimals" value="false"/>
18 </javaTypeResolver>
19 <!--生成Model类存放位置-->
20 <javaModelGenerator targetPackage="lcw.model" targetProject="src">
21 <property name="enableSubPackages" value="true"/>
22 <property name="trimStrings" value="true"/>
23 </javaModelGenerator>
24 <!--生成映射文件存放位置-->
25 <sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">
26 <property name="enableSubPackages" value="true"/>
27 </sqlMapGenerator>
28 <!--生成Dao类存放位置-->
29 <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
30 <property name="enableSubPackages" value="true"/>
31 </javaClientGenerator>
32 <!--生成对应表及类名-->
33 <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
34 </context>
35 </generatorConfiguration>

需要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。
上面配置文件中的:
<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
tableName和domainObjectName为必选项,分别代表数据库表名和生成的实体类名,其余的可以自定义去选择(一般情况下均为false)。
生成语句文件:
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
2、使用方法
在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。
看下效果图:
首先这个是我的数据库表
生成相关代码:
Message.java

1 package lcw.model;
2
3 public class Messgae {
4 private Integer id;
5
6 private String title;
7
8 private String describe;
9
10 private String content;
11
12 public Integer getId() {
13 return id;
14 }
15
16 public void setId(Integer id) {
17 this.id = id;
18 }
19
20 public String getTitle() {
21 return title;
22 }
23
24 public void setTitle(String title) {
25 this.title = title == null ? null : title.trim();
26 }
27
28 public String getDescribe() {
29 return describe;
30 }
31
32 public void setDescribe(String describe) {
33 this.describe = describe == null ? null : describe.trim();
34 }
35
36 public String getContent() {
37 return content;
38 }
39
40 public void setContent(String content) {
41 this.content = content == null ? null : content.trim();
42 }
43 }

MessgaeMapper.xml

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
3 <mapper namespace="lcw.dao.MessgaeMapper" >
4 <resultMap id="BaseResultMap" type="lcw.model.Messgae" >
5 <id column="id" property="id" jdbcType="INTEGER" />
6 <result column="title" property="title" jdbcType="VARCHAR" />
7 <result column="describe" property="describe" jdbcType="VARCHAR" />
8 <result column="content" property="content" jdbcType="VARCHAR" />
9 </resultMap>
10 <sql id="Base_Column_List" >
11 id, title, describe, content
12 </sql>
13 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
14 select
15 <include refid="Base_Column_List" />
16 from message
17 where id = #{id,jdbcType=INTEGER}
18 </select>
19 <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
20 delete from message
21 where id = #{id,jdbcType=INTEGER}
22 </delete>
23 <insert id="insert" parameterType="lcw.model.Messgae" >
24 insert into message (id, title, describe,
25 content)
26 values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{describe,jdbcType=VARCHAR},
27 #{content,jdbcType=VARCHAR})
28 </insert>
29 <insert id="insertSelective" parameterType="lcw.model.Messgae" >
30 insert into message
31 <trim prefix="(" suffix=")" suffixOverrides="," >
32 <if test="id != null" >
33 id,
34 </if>
35 <if test="title != null" >
36 title,
37 </if>
38 <if test="describe != null" >
39 describe,
40 </if>
41 <if test="content != null" >
42 content,
43 </if>
44 </trim>
45 <trim prefix="values (" suffix=")" suffixOverrides="," >
46 <if test="id != null" >
47 #{id,jdbcType=INTEGER},
48 </if>
49 <if test="title != null" >
50 #{title,jdbcType=VARCHAR},
51 </if>
52 <if test="describe != null" >
53 #{describe,jdbcType=VARCHAR},
54 </if>
55 <if test="content != null" >
56 #{content,jdbcType=VARCHAR},
57 </if>
58 </trim>
59 </insert>
60 <update id="updateByPrimaryKeySelective" parameterType="lcw.model.Messgae" >
61 update message
62 <set >
63 <if test="title != null" >
64 title = #{title,jdbcType=VARCHAR},
65 </if>
66 <if test="describe != null" >
67 describe = #{describe,jdbcType=VARCHAR},
68 </if>
69 <if test="content != null" >
70 content = #{content,jdbcType=VARCHAR},
71 </if>
72 </set>
73 where id = #{id,jdbcType=INTEGER}
74 </update>
75 <update id="updateByPrimaryKey" parameterType="lcw.model.Messgae" >
76 update message
77 set title = #{title,jdbcType=VARCHAR},
78 describe = #{describe,jdbcType=VARCHAR},
79 content = #{content,jdbcType=VARCHAR}
80 where id = #{id,jdbcType=INTEGER}
81 </update>
82 </mapper>

MessgaeMapper.java

1 package lcw.dao;
2
3 import lcw.model.Messgae;
4
5 public interface MessgaeMapper {
6 int deleteByPrimaryKey(Integer id);
7
8 int insert(Messgae record);
9
10 int insertSelective(Messgae record);
11
12 Messgae selectByPrimaryKey(Integer id);
13
14 int updateByPrimaryKeySelective(Messgae record);
15
16 int updateByPrimaryKey(Messgae record);
17 }

使用generator自动生成mybatis model、mapper.xml、mapper等(转)的更多相关文章
- generator自动生成mybatis的xml配置
generator自动生成mybatis的xml配置.model.map等信息:1.下载mybatis-generator-core-1.3.2.jar包. 网址:http://code. ...
- generator自动生成mybatis配置和类信息
generator自动生成mybatis的xml配置.model.map等信息: 1.下载mybatis-generator-core-1.3.2.jar包. 网址:http://cod ...
- 使用Mybatis Generator自动生成Mybatis相关代码
本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码: 一.构建一个环境: 1. 首先创建一个表: CREATE TABLE pet (name VARCHAR(2 ...
- Mybatis generator自动生成mybatis配置和类信息
自动生成代码方式两种: 1.命令形式生成代码,详细讲解每一个配置参数. 2.Eclipse利用插件形式生成代码. 安装插件方式: eclipse插件安装地址:http://mybatis.google ...
- 使用generator自动生成Mybatis映射配置文件
在使用mybatis时,映射文件的配置非常麻烦,对于做逻辑不是很复杂,功能不是特别关键的模块的时候,我们没有必要手动书写,可以使用generator工具生成. generator工具实际上就是根据数据 ...
- 使用MyBatis Generator自动生成MyBatis的代码
这两天需要用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生 ...
- MyBatis Generator自动生成MyBatis的映射代码
MyBatis Generator大大简化了MyBatis的数据库的代码编写,有了一个配置文件,就可以直接根据表映射成实体类.Dao类和xml映射.资源地址:MyBatis项目地址:http://my ...
- SpringBoot整合Mybatis 使用generator自动生成实体类代码、Mapper代码、dao层代码
1.新建一个SpringBoot项目,并引入Mybatis和mybatis-generator相关的依赖. <dependency> <groupId>org.springfr ...
- ator自动生成mybatis配置和类信息
generator自动生成mybatis的xml配置.model.map等信息: 1.下载mybatis-generator-core-1.3.2.jar包. 网址:http://cod ...
随机推荐
- CTEX里的函数、符号及特殊字符
CTEX里的函数.符号及特殊字符 声调 语法 效果 语法 效果 语法 效果 \bar{a} \acute{a} \check{a} \grave{a} \tilde{a} \hat ...
- 转!!为什么要java环境变量配置?
1. PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找到相应的命令程序.我们需要把 jdk安装目录下的bin目录增加到现有的PATH ...
- android fragment 博客 学习记录
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和 ...
- dom event无法获取问题
下列几种情况下无法获取到event,ff浏览器没测试. 1.a标签href调用的js事件 2.调用parent中事件,即子窗口调用父窗口页面中js 3....
- robot API笔记1
robot.api 包公开的公共api机器人框架 除非另有声明,公开的api在这个包中被认为是稳定的, 从而构建外部工具时,安全使用的机器人框架. 目前暴露api: logger 模块测试库的日志的目 ...
- pyhton2 python3 语法区别
几乎所有的Python 2程序都需要一些修改才能正常地运行在Python 3的环境下.为了简化这个转换过程,Python 3自带了一个叫做2to3的实用脚本(Utility Script),这个脚本会 ...
- PHP 高并发、抢票、秒杀 解决方案
对于抢票.秒杀这种业务,我说说自己对这种高并发的理解吧,这里提出个人认为比较可行的几个方案:方案一:使用队列来实现可以基于例如MemcacheQ等这样的消息队列,具体的实现方案这么表述吧比如有100张 ...
- android:configChanges属性
对android:configChanges属性,一般认为有以下几点: 1.不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏 ...
- EasyUI tab常用
获取选中的tab的title var tab = $('#tab_Employee').tabs('getSelected'); var t=tab.panel('options').title; t ...
- Problem 2020 组合(FOJ)
Problem 2020 组合 Accept: 714 Submit: 1724Time Limit: 1000 mSec Memory Limit : 32768 KB Problem ...