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

10.使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)的更多相关文章

  1. idea集成 MyBatis Generator 插件,自动生成dao,model,sql map文件

    1.集成到开发环境中 以maven管理的功能来举例,只需要将插件添加到pom.xml文件中即可.(注意此处是以plugin的方式,放在<plugins></plugins>中间 ...

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

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

  3. mybatis generator 自动生成dao层映射代码

    资源: doc url :http://www.mybatis.org/generator/ download:https://github.com/mybatis/generator/release ...

  4. java web(七): mybatis的动态sql和mybatis generator自动生成pojo类和映射文件

    前言: MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据 不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空格,还 ...

  5. 使用Mybatis Generator自动生成Mybatis相关代码

    本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码: 一.构建一个环境: 1. 首先创建一个表: CREATE TABLE pet (name VARCHAR(2 ...

  6. SpringBoot 添加mybatis generator 自动生成代码插件

    自动生成数据层代码,提高开发效率 1.pom添加插件,并指定配置文件路径 <!-- mybatis generator 自动生成代码插件 --> <plugin> <gr ...

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

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

  8. IDEA Maven Mybatis generator 自动生成代码

    IDEA Maven Mybatis generator 自动生成代码 一.安装配置maven以及在Idea中配置maven 安装过程步骤可以看上面的博文,里面介绍得很详细. 二.建数据表 DROP ...

  9. idea中mybatis generator自动生成代码配置 数据库是sqlserver

    好长时间没有写博客了,最近公司要用java语言,开始学习java,属于初学者,今天主要记录一下mybatis generator自动生成代码,首先在如下图的目录中新建两个文件,如下图 generato ...

  10. IDEA Maven Mybatis generator 自动生成代码(实例讲解)(转)

    IDEA Maven Mybatis generator 自动生成代码(实例讲解) MyBatis Generator • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的 ...

随机推荐

  1. 腾讯面试题:A.txt和B.txt两个文件,A有1亿个qq号,B有100万个,用代码实现交、并、差

    在STL中关于有序序列有这么四个算法: set_union(beg, end, beg, end2, dest);                    //求并集A∪B set_union(beg, ...

  2. 为什么倒排索引不采用zlib这样的字典压缩算法——因为没法直接使用啊

    看了下压缩算法的发展历史,根据倒排索引的数据结构特点,个人认为zstd不适合做倒排索引压缩,举例说明下: 假设有一份文档倒排列表为:[300, 302, 303, 332],对于这组倒排数据,是没法* ...

  3. C# List常识之经常被忽略的常识

    最近在接收前辈的代码,越来越会发现有很多.net已经封装好的方法可以使用,我们却不知道,然后自己去For/Foreach循环解决自己的需求问题 总的来说:当下很忧伤啊.总结了几个经常需要用却不知道的方 ...

  4. Springboot+hibernate简单的增删改查

    1.创建好项目之后在配置端口号(也可以不用配置,默认端口8080) #server server.port= server.tomcat.uri-encoding=utf- 2.配置mysql #My ...

  5. TCP排查常用命令

    1.查看TCP连接状态命令 netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"\t" ...

  6. Jquery 获取父页面下指定iframe里的指定元素

    var div1=$("#iframe1",window.parent.document).contents().find("#div1");

  7. PHP 框架Laravel Eloquent 实现身份验证

    PHP自从5.3后似乎又热度又回升, 最近了解了一下PHP框架之一Laravel, 最近最新的版本已经是4.3  基本的结构这里不讲, 要了解可以在这里看文档 http://v4.golaravel. ...

  8. JS 有趣的eval优化输入验证

    //eval就是计算字符串[可以放任何js代码]里的值 . var str1='12+3'; eval(str1); . var str2='[1,2,3]'; eval(str2[]); .eval ...

  9. 解决无法移除tomcat中的项目

    问题:启动myeclipse,tomcat提示报错,blind,但是你移除的时候无法移除,只会显示一个黄色的感叹号,此时你直接在webapp中删除时,也提示呗占用无法删除. 办法:关掉myeclips ...

  10. hibernate中的懒加载和急加载

    懒加载(FatchType.LAZY)是Hibernate为提高程序执行效率而提供的一种机制,简单说就是只有正真使用其属性的时候,数据库才会进行查询. 具体的执行过程就是:Hibernate从数据库获 ...