MyBatis Generator是一款mybatis自动代码生成工具,可以通过配置后自动生成文件。

MyBatis Generator有几种方法可以生成代码,下面是其中一种。

 一、官网下载 MyBatis Generator

1、下载地址:https://github.com/mybatis/generator/releases
目前最新版本是1.3.7。
在命令行下自动创建代码只需要下载mybatis-generator-core-1.3.7.zip即可。

2、压缩包解压后,主要是lib目录,进去只有三个jar文件

二、创建配置文件等

1、在lib目录里面创建配置文件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> <!--MySQL连接驱动-->
<classPathEntry location="mysql-connector-java-5.1.37.jar" /> <!--数据库链接URL,用户名、密码 -->
<context id="MySQL" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1/test"
userId="root"
password="">
</jdbcConnection> <!--是否启用java.math.BigDecimal-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="test.model" targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="test.xml" targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table> </context>
</generatorConfiguration>

2、因为的是mysql,所以在lib目录里面还需要放入mysql驱动包,如mysql-connector-java-5.1.37.jar

3、配置文件指定输出目录为src(targetProject="src"),所以需要在lib目录手动创建src目录,最终文件如下:

三、命令行下生成代码

1、在cmd命令行定位到D:\projects\mybatis-generator-core-1.3.7\lib

2、输入命令java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
正常情况下会提示操作成功。

3、查看src目录,已经生成3个子目录,并且相应文件也在里面

4、UserMapper.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="test.dao.UserMapper">
<resultMap id="BaseResultMap" type="test.model.User">
<id column="Id" jdbcType="INTEGER" property="id" />
<result column="Name" jdbcType="VARCHAR" property="name" />
<result column="Sex" jdbcType="VARCHAR" property="sex" />
<result column="Age" jdbcType="TINYINT" property="age" />
</resultMap>
<sql id="Base_Column_List">
Id, Name, Sex, Age
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where Id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from user
where Id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="test.model.User">
insert into user (Id, Name, Sex,
Age)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR},
#{age,jdbcType=TINYINT})
</insert>
<insert id="insertSelective" parameterType="test.model.User">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
Id,
</if>
<if test="name != null">
Name,
</if>
<if test="sex != null">
Sex,
</if>
<if test="age != null">
Age,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="sex != null">
#{sex,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=TINYINT},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="test.model.User">
update user
<set>
<if test="name != null">
Name = #{name,jdbcType=VARCHAR},
</if>
<if test="sex != null">
Sex = #{sex,jdbcType=VARCHAR},
</if>
<if test="age != null">
Age = #{age,jdbcType=TINYINT},
</if>
</set>
where Id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="test.model.User">
update user
set Name = #{name,jdbcType=VARCHAR},
Sex = #{sex,jdbcType=VARCHAR},
Age = #{age,jdbcType=TINYINT}
where Id = #{id,jdbcType=INTEGER}
</update>
</mapper>

5、User.java

package test.model;

public class User {
private Integer id; private String name; private String sex; private Byte age; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name == null ? null : name.trim();
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
} public Byte getAge() {
return age;
} public void setAge(Byte age) {
this.age = age;
}
}

6、UserMapper.java

package test.dao;

import test.model.User;

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

使用MyBatis Generator 1.3.7自动生成代码的更多相关文章

  1. MyBatis Generator作为maven插件自动生成增删改查代码及配置文件例子

    什么是MyBatis Generator MyBatis Generator (MBG) 是一个Mybatis的代码生成器,可以自动生成一些简单的CRUD(插入,查询,更新,删除)操作代码,model ...

  2. SpringBoot与Mybatis整合(包含generate自动生成代码工具,数据库表一对一,一对多,关联关系中间表的查询)

    链接:https://blog.csdn.net/YonJarLuo/article/details/81187239 自动生成工具只是生成很单纯的表,复杂的一对多,多对多的情况则是在建表的时候就建立 ...

  3. mybatis generator maven插件自动生成代码

    如果你正为无聊Dao代码的编写感到苦恼,如果你正为怕一个单词拼错导致Dao操作失败而感到苦恼,那么就可以考虑一些Mybatis generator这个差价,它会帮我们自动生成代码,类似于Hiberna ...

  4. MyBatis使用Generator自动生成代码

    MyBatis中,可以使用Generator自动生成代码,包括DAO层. MODEL层 .MAPPING SQL映射文件. 第一步: 配置好自动生成代码所需的XML配置文件,例如(generator. ...

  5. mybatis 自动生成代码(mybatis generator)

    pom.xml 文件配置 引入 mybatis generator <properties> <mysql.connector.version>5.1.44</mysql ...

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

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

  7. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  8. Mybatis generator 自动生成代码(2)

    最近准备开始做一个项目,需要开始手动创建sql,于是将Mybatis generator 工具功能强化了下. 首先,这里引入到版本一点的包 <dependency> <groupId ...

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

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

随机推荐

  1. eclipse反编译不起作用

    今天用eclipse安装反编译插件,分别按百度安装了好几个,但是都没起到作用.想想可能是因为我的eclipse是现在最新的版本,可能以前的方法不适用了,所以就自己折腾了一下.以下为教程: 1.首先我的 ...

  2. [No0000195]NoSQL还是SQL?这一篇讲清楚

    随着大数据时代的到来,越来越多的网站.应用系统需要支撑海量数据存储,高并发.高可用.高可扩展性等特性要求. 传统的关系型数据库在应付这些已经显得力不从心,并暴露了许多难以克服的问题. 由此,各种各样的 ...

  3. CentOS 7.6 安装 Weblogic 12

    http://download.oracle.com/otn/nt/middleware/12c/12213/fmw_12.2.1.3.0_wls_Disk1_1of1.zip java -jar f ...

  4. J2EE快速开发框架

    地址: http://git.oschina.net/blind/app 项目简介 使用Maven对项目进行模块化管理,提高项目的易开发性.扩展性. 实现了通用的系统管理模块功能,包含:用户.角色.权 ...

  5. weui开发笔记

    1.标准的weui只是一个css皮肤,当然里面有h5特性所以有一些很好的组件,比如时间选择控件.数字输入框(用于手机号等),在ios——微信中可以做到完美的展示. 2.ui框架以手机移动端为优先显示( ...

  6. [数学]MIT牛人解说数学体系

    本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/study/science/mit_math ...

  7. 最全的MonkeyRunner自动化测试从入门到精通(3)

    一.eclipse的下载安装与配置成安卓开发环境 步骤一:在官网上面进行下载eclipse,官网的网址:https://www.eclipse.org/downloads/ 步骤二:下载完成后可以在你 ...

  8. FQ原理

    笔者在nginx反向代理篇讲了正向代理和反向代理的区别,今天着重讲其中的FQ是实现原理. 一.普遍的两种方式 1.vpn vpn它将客户端的IP数据报经过加密和二次封装后转发出去,客户端通过vpn上网 ...

  9. 【托业】【新东方托业全真模拟】TEST05~06-----P5~6

    credit A with B 把A归功于B present A with B 给A赠送B proofread thoroughly 彻底地校对:exclusively 专门地:独占地:apparen ...

  10. linux 日常运维 目录

    Linux防火墙:iptables Linux 抓包工具:tcpdump linux 查看磁盘读写:iotop linux 查看磁盘读写:iostat inux 查看网卡流量:sar linux 查看 ...