转载自https://www.cnblogs.com/smileberry/p/4145872.html

出处: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

按 Ctrl+C 复制代码

按 Ctrl+C 复制代码

需要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。

上面配置文件中的:

<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 }

使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)相关推荐

  1. idea 集成mybatis,利用MyBatis Generator自动生成实体类、mapper文件

    最近一个老项目集成mybatis,利用 generator自动生成实体类.mapper的时候折腾了一小时,记录一下,避免以后再折腾 很简单的三步 https://gitee.com/shunangua ...

  2. 【Java MyBatis Generator】使用generator自动生成Dao,Mapping和实体文件

    具体请参照: http://blog.csdn.net/fengshizty/article/details/43086833 按照上面博客地址,下载Generator的依赖包: 如下是我的配置文件: ...

  3. SpringBoot如何自动生成实体类和Dao层以及映射文件(mybatis generator 自动生成代码)

    一.首先添加自动生成代码插件 <!-- mybatis generator 自动生成代码插件 生成时解除注释 --><plugin><groupId>org.myb ...

  4. Mybatis Generator 自动生成数据库XML(Mybatis Generator 逆向工程)

    Mybatis Generator 逆向工程自动生成XML 1.MyBatis Generator简单介绍: (1)MyBatis Generator 会生成: Dao,Model,Mapping基础 ...

  5. 使用MyBatis Generator自动生成持久层CRUD代码的两种方法

    最近在使用MyBatis,得知可以利用MyBatis Generator自动生成实体类.DAO接口和Mapping映射文件.当数据库中的表多的时候,让你不用再手写Mapping映射文件,和实体类,就可 ...

  6. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    我们这一一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池的好处我 ...

  7. mybatis generator 自动生成代码(带注释的实体类)

    使用前提: 当你开发的java 项目或新模块的数据库有N张表操作时,这时要自己写实体类.dao.SqlMapper.xml等文件,如果有多个表,就是造成时间浪费降低开发效率,所以建议使用mybatis ...

  8. IDEA使用Mybatis Generator自动生成部分代码

    IDEA使用Mybatis Generator自动生成部分代码 说明:使用Mybatis Generator代码生成配置,简单生成部分后端代码,包括(实体类,Mapper.Mapper.xml文件) ...

  9. mybatis generator自动生成sqlmap代码的不完善之处以及解决方法

    mybatis generator自动生成sqlmap代码的不完善之处以及解决方法 参考文章: (1)mybatis generator自动生成sqlmap代码的不完善之处以及解决方法 (2)http ...

  10. Eclipse使用mybatis generator自动生成代码

    一.写在前面 Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件 ...

最新文章

  1. SQL 注入真是防不胜防!
  2. 06_特征选择,特征选择的原因,sklearn特征选择API
  3. BZOJ 1012: [JSOI2008]最大数maxnumber
  4. 在写HTML和CSS时的黄金规范
  5. 二级c语言无纸化三合一_学习攻略|计算机二级考试重点及注意事项
  6. cookies,sessionStorage 和 localStorage 的区别?
  7. spring IoC/DI
  8. 将serversocket 写在按钮事件中连接不上_看 Netty 在 Dubbo 中如何应用
  9. linux 列出当前视频设备,如何获取Linux(ubuntu)上的视频捕获设备(网络摄像机)列表?(C / C ++)...
  10. php mongoclient使用,PHP使用mongoclient简单操作mongodb数据库示例
  11. 小D课堂 - 新版本微服务springcloud+Docker教程_6-01 微服务网关介绍和使用场景
  12. 一文讲清神经网络、BP神经网络、深度学习的关系
  13. 车牌识别存储云服务器,云端(服务器)车牌识别技术
  14. 你的短信接口真的安全吗?
  15. Google C++ 风格指南-转载自 http://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/
  16. 模拟量信号干扰分析及解决方案
  17. C# asp.net 连接 Sql Server数据库 Timeout expired. 错误,怎么办?
  18. C++ 基础知识throw的用法
  19. python入门爬取表情包
  20. 承德网站服务器,承德联通dns服务器地址

热门文章

  1. 利用 AVDictionary 配置参数
  2. 【Boost】boost库asio详解2——strand与io_service区别
  3. 对现有的所能找到个DDOS代码(攻击模块)做出一次分析----TCP篇
  4. c++空类实例大小不是0原因
  5. Python中is和==有什么区别?
  6. MySQL(六)InnoDB锁详解
  7. 深入理解程序执行原理
  8. LiveVideoStack Meet | 杭州:CV与流媒体将走向融合
  9. 火山引擎视频云:从toC到toB,如何将最好的技术开放出去
  10. TRTC助力高并发、高可用实时音视频互动场景落地(内含开发福利)