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

1、相关文件

关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases

由于我使用的是Mysql数据库,这里需要再准备一个连接mysql数据库的驱动jar包

以下是相关文件截图:

和Hibernate逆向生成一样,这里也需要一个配置文件:

generatorConfig.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="mysql-connector-java-5.0.8-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="lcw.model" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>

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

上面配置文件中的:

<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!-- 表示跟数据库表中的字段保持一致 -->
<property name="useActualColumnNames" value="true"/>
</table>

tableName和domainObjectName为必选项,分别代表数据库表名和生成的实体类名,其余的可以自定义去选择(一般情况下均为false)。

生成语句文件:

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

2、使用方法

在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。

看下效果图:

首先这个是我的数据库表:

生成相关代码:

Message.java

package lcw.model;

public class Messgae {
private Integer id; private String title; private String describe; private String content; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title == null ? null : title.trim();
} public String getDescribe() {
return describe;
} public void setDescribe(String describe) {
this.describe = describe == null ? null : describe.trim();
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content == null ? null : content.trim();
}
}

MessgaeMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="lcw.dao.MessgaeMapper" >
<resultMap id="BaseResultMap" type="lcw.model.Messgae" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="title" property="title" jdbcType="VARCHAR" />
<result column="describe" property="describe" jdbcType="VARCHAR" />
<result column="content" property="content" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, title, describe, content
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from message
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from message
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="lcw.model.Messgae" >
insert into message (id, title, describe,
content)
values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{describe,jdbcType=VARCHAR},
#{content,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="lcw.model.Messgae" >
insert into message
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="title != null" >
title,
</if>
<if test="describe != null" >
describe,
</if>
<if test="content != null" >
content,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="title != null" >
#{title,jdbcType=VARCHAR},
</if>
<if test="describe != null" >
#{describe,jdbcType=VARCHAR},
</if>
<if test="content != null" >
#{content,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="lcw.model.Messgae" >
update message
<set >
<if test="title != null" >
title = #{title,jdbcType=VARCHAR},
</if>
<if test="describe != null" >
describe = #{describe,jdbcType=VARCHAR},
</if>
<if test="content != null" >
content = #{content,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="lcw.model.Messgae" >
update message
set title = #{title,jdbcType=VARCHAR},
describe = #{describe,jdbcType=VARCHAR},
content = #{content,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>

MessgaeMapper.java

package lcw.dao;

import lcw.model.Messgae;

public interface MessgaeMapper {
int deleteByPrimaryKey(Integer id); int insert(Messgae record); int insertSelective(Messgae record); Messgae selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Messgae record); int updateByPrimaryKey(Messgae record);
}

本文转自:http://www.cnblogs.com/lichenwei/p/4145696.html

使用Mybatis-Generator自动生成Dao、Model、Mapping的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. Mybatis generator自动生成mybatis配置和类信息

    自动生成代码方式两种: 1.命令形式生成代码,详细讲解每一个配置参数. 2.Eclipse利用插件形式生成代码. 安装插件方式: eclipse插件安装地址:http://mybatis.google ...

  10. 使用MyBatis Generator自动生成MyBatis的代码

    这两天需要用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生 ...

随机推荐

  1. 大数据环境完全分布式搭建 hadoop2.4.1

    (如果想要安装视频及相关软件可以博私聊我 qq 731487514) hadoop2.0已经发布了稳定版本了,增加了很多特性,比如HDFS HA.YARN等.最新的hadoop-2.4.1又增加了YA ...

  2. LR特征维数特别大实时计算问题

    美团 https://tech.meituan.com/machinelearning-data-feature-process.html 维数灾难 待续...

  3. BZOJ.3784.树上的路径(点分治 贪心 堆)

    BZOJ \(Description\) 给定一棵\(n\)个点的带权树,求树上\(\frac{n\times(n-1)}{2}\)条路径中,长度最大的\(m\)条路径的长度. \(n\leq5000 ...

  4. BZOJ.3453.tyvj 1858 XLkxc(拉格朗日插值)

    BZOJ 题意即求\[\sum_{i=0}^n\sum_{j=1}^{a+id}\sum_{x=1}^jx^k\] 我们知道最后一个\(\sum\)是自然数幂和,设\(f(n)=\sum_{x=1}^ ...

  5. JavaScript基础笔记(九)事件

    事件 一.事件流 事件流描述的是从页面中接收事件的顺序. 一)事件冒泡 IE的事件流叫做事件冒泡,即事件开始时由最具体的元素接收,然后逐级向上传播到较为不具体的节点. 如:div------>b ...

  6. [PA2015]Rozstaw szyn

    [PA2015]Rozstaw szyn 题目大意: 一棵\(n(n\le5\times10^5)\)个点的树,其中有\(m\)个结点是叶子结点.叶子结点权值已知,你可以自己决定其余结点的权值,定义整 ...

  7. mysql:general_log 日志、数据库线程查询、数据库慢查询

    开启general log会将所有到达MySQL Server的SQL语句记录下来.一般不会开启开功能,因为log的量会非常庞大.但个别情况下可能会临时的开一会儿general log以供排障使用.  ...

  8. Android Studio 解决ADB检测不到手机导致无法连接的问题

    ADB的全称是Android Debug Bridge,是用来管理模拟器和真机的通用调试工具. 开USB调试 方法:手机设置 - 开发人员选项 - USB调试 - 勾选(开发者调试被隐藏了,在关于手机 ...

  9. javaweb数据库编程代码详细讲解

    import java.sql.*; /*默写数据库练习数据库编程及注释讲解代码*/ public class Main{ public static void main(String[]args)t ...

  10. redis:list列表类型的操作

    1. list列表类型的操作 1.1. lpush/rpush key value [value ...] 链表的头部(左侧)或尾部(右侧)插入值 语法:lpush key value [value ...