mybatis mybatis-generator 代码自动生成工具

来源:互联网 发布:网络交易的特点 编辑:程序博客网 时间:2024/04/30 09:39

一、简介

mybatis generator是很好用的mybatis自动代码生成工具。最近公司使用maven和mybatis开发项目,手动写入一个个实体类和mapper还有xml配置文件感觉会很麻烦,使用mybatis generator只需要简单的配置就能完成我们的工作,这里简述一下开发步骤。

二、开发流程

2.1 创建maven项目

我们选择开发工具创建maven项目,我这里使用myeclipse开发,建议使用eclipse或者idea开发。 
这里写图片描述

2.2 在pom配置文件中加入依赖包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>mybatis-generator-base</groupId>  <artifactId>mybatis-generator-base</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>war</packaging>  <name/>  <description/>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <dependencies>    <dependency>      <groupId>javax.servlet</groupId>      <artifactId>jstl</artifactId>      <version>1.2</version>      <scope>provided</scope>    </dependency>    <dependency>      <groupId>javax.servlet.jsp</groupId>      <artifactId>jsp-api</artifactId>      <version>2.1</version>      <scope>provided</scope>    </dependency>    <dependency>      <groupId>org.glassfish</groupId>      <artifactId>javax.annotation</artifactId>      <version>3.0.1</version>    </dependency>    <dependency>      <groupId>org.glassfish</groupId>      <artifactId>javax.ejb</artifactId>      <version>3.0.1</version>    </dependency>    <dependency>      <groupId>org.jboss.weld</groupId>      <artifactId>weld-osgi-bundle</artifactId>      <version>1.0.1-SP3</version>    </dependency>    <dependency>      <groupId>org.glassfish</groupId>      <artifactId>javax.servlet</artifactId>      <version>3.0.1</version>    </dependency>    <!--测试框架  -->    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.12</version>    </dependency>    <!-- Mysql -->    <dependency>      <groupId>org.mybatis</groupId>      <artifactId>mybatis</artifactId>      <version>3.2.7</version>    </dependency>    <!-- Mysql 依赖 -->    <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>      <version>5.1.6</version>    </dependency>    <!--生成代码插件-->    <dependency>        <groupId>org.mybatis.generator</groupId>        <artifactId>mybatis-generator-core</artifactId>        <version>1.3.2</version>        <type>jar</type>    </dependency>  </dependencies>  <build>    <plugins>      <plugin>        <artifactId>maven-war-plugin</artifactId>      </plugin>      <plugin>        <artifactId>maven-compiler-plugin</artifactId>        <configuration>          <source>1.6</source>          <target>1.6</target>        </configuration>      </plugin>    </plugins>  </build></project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

2.3 新建生成代码的配置文件mybatis-generator-config.xml

将配置文件配置在合适的位置就可以,但是要访问的到,我这里放置在resources文件下。

<?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>    <context id="prod">        <!-- RowBounds pagination -->        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />        <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" />        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />        <commentGenerator>            <property name="suppressDate" value="true" />            <property name="suppressAllComments" value="true" />        </commentGenerator>        <!-- jdbc连接 -->        <jdbcConnection driverClass="com.mysql.jdbc.Driver"            connectionURL="jdbc:mysql://127.0.0.1:3306/generator" userId="root"            password="123456" />        <javaModelGenerator targetPackage="com.mybatis.entity"            targetProject="src/main/java">            <!-- 是否针对string类型的字段在set的时候进行trim调用 -->            <property name="trimStrings" value="true" />        </javaModelGenerator>        <sqlMapGenerator targetPackage="mappers" targetProject="src/main/java" />        <javaClientGenerator targetPackage="com.mybatis.mapper"            targetProject="src/main/java" type="XMLMAPPER" />        <table tableName="wx_ranking_flow" domainObjectName="WxRankingFlow">        </table>    </context></generatorConfiguration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

(1)在javaModelGenerator标签下配置你需要生成的数据库实体的地址 
(2)在sqlMapGenerator标签下配置mysql的xml配置文件 
(3)在javaClientGenerator标签下配置mapper方法 
(4)在table标签下配置数据库的表面和生成实体的表名

2.4 新建批处理类main方法

package com.mybatis.test;import org.mybatis.generator.api.ShellRunner;public class App {    public static void main(String[] args) {        args = new String[] { "-configfile", "src\\main\\resources\\mybatis-generator-config.xml", "-overwrite" };        ShellRunner.main(args);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

这里要处理的就是我们的mybatis-generator-config.xml配置文件,路径一定要对应我们的存放路径。 
执行main方法就可以生成对应的实体和xml配置文件了。 
这里写图片描述 
我们会发现我们生成了两个实体对象,一个是数据库映射对象,一个是Example对象。Example对象就是为了方便我们执行sql操作的类,可以使用Example类进行数据库的条件查询。同时mybatis-generator还帮助我们生成了sql的CRUD等操作。

WxRankingFlowMapper接口

package com.mybatis.mapper;import java.util.List;import com.mybatis.entity.WxRankingFlow;import com.mybatis.entity.WxRankingFlowExample;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.session.RowBounds;public interface WxRankingFlowMapper {    int countByExample(WxRankingFlowExample example);    int deleteByExample(WxRankingFlowExample example);    int deleteByPrimaryKey(String rId);    int insert(WxRankingFlow record);    int insertSelective(WxRankingFlow record);    List<WxRankingFlow> selectByExampleWithRowbounds(WxRankingFlowExample example, RowBounds rowBounds);    List<WxRankingFlow> selectByExample(WxRankingFlowExample example);    WxRankingFlow selectByPrimaryKey(String rId);    int updateByExampleSelective(@Param("record") WxRankingFlow record, @Param("example") WxRankingFlowExample example);    int updateByExample(@Param("record") WxRankingFlow record, @Param("example") WxRankingFlowExample example);    int updateByPrimaryKeySelective(WxRankingFlow record);    int updateByPrimaryKey(WxRankingFlow record);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

WxRankingFlowMapper.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="com.mybatis.mapper.WxRankingFlowMapper" >  <resultMap id="BaseResultMap" type="com.mybatis.entity.WxRankingFlow" >    <id column="r_id" property="rId" jdbcType="VARCHAR" />    <result column="r_gift_date" property="rGiftDate" jdbcType="VARCHAR" />    <result column="r_sid" property="rSid" jdbcType="VARCHAR" />    <result column="r_card_no" property="rCardNo" jdbcType="VARCHAR" />    <result column="r_card_name" property="rCardName" jdbcType="VARCHAR" />    <result column="r_re_openid" property="rReOpenid" jdbcType="VARCHAR" />    <result column="r_number" property="rNumber" jdbcType="INTEGER" />    <result column="r_chain_code" property="rChainCode" jdbcType="VARCHAR" />    <result column="r_chain_id" property="rChainId" jdbcType="VARCHAR" />    <result column="r_chain_name" property="rChainName" jdbcType="VARCHAR" />    <result column="r_total_amount" property="rTotalAmount" jdbcType="INTEGER" />    <result column="r_total_count" property="rTotalCount" jdbcType="INTEGER" />    <result column="r_praise_count" property="rPraiseCount" jdbcType="INTEGER" />    <result column="r_create_time" property="rCreateTime" jdbcType="TIMESTAMP" />    <result column="r_update_time" property="rUpdateTime" jdbcType="TIMESTAMP" />  </resultMap>  <sql id="Example_Where_Clause" >    <where >      <foreach collection="oredCriteria" item="criteria" separator="or" >        <if test="criteria.valid" >          <trim prefix="(" suffix=")" prefixOverrides="and" >            <foreach collection="criteria.criteria" item="criterion" >              <choose >                <when test="criterion.noValue" >                  and ${criterion.condition}                </when>                <when test="criterion.singleValue" >                  and ${criterion.condition} #{criterion.value}                </when>                <when test="criterion.betweenValue" >                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}                </when>                <when test="criterion.listValue" >                  and ${criterion.condition}                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >                    #{listItem}                  </foreach>                </when>              </choose>            </foreach>          </trim>        </if>      </foreach>    </where>  </sql>  <sql id="Update_By_Example_Where_Clause" >    <where >      <foreach collection="example.oredCriteria" item="criteria" separator="or" >        <if test="criteria.valid" >          <trim prefix="(" suffix=")" prefixOverrides="and" >            <foreach collection="criteria.criteria" item="criterion" >              <choose >                <when test="criterion.noValue" >                  and ${criterion.condition}                </when>                <when test="criterion.singleValue" >                  and ${criterion.condition} #{criterion.value}                </when>                <when test="criterion.betweenValue" >                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}                </when>                <when test="criterion.listValue" >                  and ${criterion.condition}                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >                    #{listItem}                  </foreach>                </when>              </choose>            </foreach>          </trim>        </if>      </foreach>    </where>  </sql>  <sql id="Base_Column_List" >    r_id, r_gift_date, r_sid, r_card_no, r_card_name, r_re_openid, r_number, r_chain_code,     r_chain_id, r_chain_name, r_total_amount, r_total_count, r_praise_count, r_create_time,     r_update_time  </sql>  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.mybatis.entity.WxRankingFlowExample" >    select    <if test="distinct" >      distinct    </if>    <include refid="Base_Column_List" />    from wx_ranking_flow    <if test="_parameter != null" >      <include refid="Example_Where_Clause" />    </if>    <if test="orderByClause != null" >      order by ${orderByClause}    </if>  </select>  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >    select     <include refid="Base_Column_List" />    from wx_ranking_flow    where r_id = #{rId,jdbcType=VARCHAR}  </select>  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >    delete from wx_ranking_flow    where r_id = #{rId,jdbcType=VARCHAR}  </delete>  <delete id="deleteByExample" parameterType="com.mybatis.entity.WxRankingFlowExample" >    delete from wx_ranking_flow    <if test="_parameter != null" >      <include refid="Example_Where_Clause" />    </if>  </delete>  <insert id="insert" parameterType="com.mybatis.entity.WxRankingFlow" >    insert into wx_ranking_flow (r_id, r_gift_date, r_sid,       r_card_no, r_card_name, r_re_openid,       r_number, r_chain_code, r_chain_id,       r_chain_name, r_total_amount, r_total_count,       r_praise_count, r_create_time, r_update_time      )    values (#{rId,jdbcType=VARCHAR}, #{rGiftDate,jdbcType=VARCHAR}, #{rSid,jdbcType=VARCHAR},       #{rCardNo,jdbcType=VARCHAR}, #{rCardName,jdbcType=VARCHAR}, #{rReOpenid,jdbcType=VARCHAR},       #{rNumber,jdbcType=INTEGER}, #{rChainCode,jdbcType=VARCHAR}, #{rChainId,jdbcType=VARCHAR},       #{rChainName,jdbcType=VARCHAR}, #{rTotalAmount,jdbcType=INTEGER}, #{rTotalCount,jdbcType=INTEGER},       #{rPraiseCount,jdbcType=INTEGER}, #{rCreateTime,jdbcType=TIMESTAMP}, #{rUpdateTime,jdbcType=TIMESTAMP}      )  </insert>  <insert id="insertSelective" parameterType="com.mybatis.entity.WxRankingFlow" >    insert into wx_ranking_flow    <trim prefix="(" suffix=")" suffixOverrides="," >      <if test="rId != null" >        r_id,      </if>      <if test="rGiftDate != null" >        r_gift_date,      </if>      <if test="rSid != null" >        r_sid,      </if>      <if test="rCardNo != null" >        r_card_no,      </if>      <if test="rCardName != null" >        r_card_name,      </if>      <if test="rReOpenid != null" >        r_re_openid,      </if>      <if test="rNumber != null" >        r_number,      </if>      <if test="rChainCode != null" >        r_chain_code,      </if>      <if test="rChainId != null" >        r_chain_id,      </if>      <if test="rChainName != null" >        r_chain_name,      </if>      <if test="rTotalAmount != null" >        r_total_amount,      </if>      <if test="rTotalCount != null" >        r_total_count,      </if>      <if test="rPraiseCount != null" >        r_praise_count,      </if>      <if test="rCreateTime != null" >        r_create_time,      </if>      <if test="rUpdateTime != null" >        r_update_time,      </if>    </trim>    <trim prefix="values (" suffix=")" suffixOverrides="," >      <if test="rId != null" >        #{rId,jdbcType=VARCHAR},      </if>      <if test="rGiftDate != null" >        #{rGiftDate,jdbcType=VARCHAR},      </if>      <if test="rSid != null" >        #{rSid,jdbcType=VARCHAR},      </if>      <if test="rCardNo != null" >        #{rCardNo,jdbcType=VARCHAR},      </if>      <if test="rCardName != null" >        #{rCardName,jdbcType=VARCHAR},      </if>      <if test="rReOpenid != null" >        #{rReOpenid,jdbcType=VARCHAR},      </if>      <if test="rNumber != null" >        #{rNumber,jdbcType=INTEGER},      </if>      <if test="rChainCode != null" >        #{rChainCode,jdbcType=VARCHAR},      </if>      <if test="rChainId != null" >        #{rChainId,jdbcType=VARCHAR},      </if>      <if test="rChainName != null" >        #{rChainName,jdbcType=VARCHAR},      </if>      <if test="rTotalAmount != null" >        #{rTotalAmount,jdbcType=INTEGER},      </if>      <if test="rTotalCount != null" >        #{rTotalCount,jdbcType=INTEGER},      </if>      <if test="rPraiseCount != null" >        #{rPraiseCount,jdbcType=INTEGER},      </if>      <if test="rCreateTime != null" >        #{rCreateTime,jdbcType=TIMESTAMP},      </if>      <if test="rUpdateTime != null" >        #{rUpdateTime,jdbcType=TIMESTAMP},      </if>    </trim>  </insert>  <select id="countByExample" parameterType="com.mybatis.entity.WxRankingFlowExample" resultType="java.lang.Integer" >    select count(*) from wx_ranking_flow    <if test="_parameter != null" >      <include refid="Example_Where_Clause" />    </if>  </select>  <update id="updateByExampleSelective" parameterType="map" >    update wx_ranking_flow    <set >      <if test="record.rId != null" >        r_id = #{record.rId,jdbcType=VARCHAR},      </if>      <if test="record.rGiftDate != null" >        r_gift_date = #{record.rGiftDate,jdbcType=VARCHAR},      </if>      <if test="record.rSid != null" >        r_sid = #{record.rSid,jdbcType=VARCHAR},      </if>      <if test="record.rCardNo != null" >        r_card_no = #{record.rCardNo,jdbcType=VARCHAR},      </if>      <if test="record.rCardName != null" >        r_card_name = #{record.rCardName,jdbcType=VARCHAR},      </if>      <if test="record.rReOpenid != null" >        r_re_openid = #{record.rReOpenid,jdbcType=VARCHAR},      </if>      <if test="record.rNumber != null" >        r_number = #{record.rNumber,jdbcType=INTEGER},      </if>      <if test="record.rChainCode != null" >        r_chain_code = #{record.rChainCode,jdbcType=VARCHAR},      </if>      <if test="record.rChainId != null" >        r_chain_id = #{record.rChainId,jdbcType=VARCHAR},      </if>      <if test="record.rChainName != null" >        r_chain_name = #{record.rChainName,jdbcType=VARCHAR},      </if>      <if test="record.rTotalAmount != null" >        r_total_amount = #{record.rTotalAmount,jdbcType=INTEGER},      </if>      <if test="record.rTotalCount != null" >        r_total_count = #{record.rTotalCount,jdbcType=INTEGER},      </if>      <if test="record.rPraiseCount != null" >        r_praise_count = #{record.rPraiseCount,jdbcType=INTEGER},      </if>      <if test="record.rCreateTime != null" >        r_create_time = #{record.rCreateTime,jdbcType=TIMESTAMP},      </if>      <if test="record.rUpdateTime != null" >        r_update_time = #{record.rUpdateTime,jdbcType=TIMESTAMP},      </if>    </set>    <if test="_parameter != null" >      <include refid="Update_By_Example_Where_Clause" />    </if>  </update>  <update id="updateByExample" parameterType="map" >    update wx_ranking_flow    set r_id = #{record.rId,jdbcType=VARCHAR},      r_gift_date = #{record.rGiftDate,jdbcType=VARCHAR},      r_sid = #{record.rSid,jdbcType=VARCHAR},      r_card_no = #{record.rCardNo,jdbcType=VARCHAR},      r_card_name = #{record.rCardName,jdbcType=VARCHAR},      r_re_openid = #{record.rReOpenid,jdbcType=VARCHAR},      r_number = #{record.rNumber,jdbcType=INTEGER},      r_chain_code = #{record.rChainCode,jdbcType=VARCHAR},      r_chain_id = #{record.rChainId,jdbcType=VARCHAR},      r_chain_name = #{record.rChainName,jdbcType=VARCHAR},      r_total_amount = #{record.rTotalAmount,jdbcType=INTEGER},      r_total_count = #{record.rTotalCount,jdbcType=INTEGER},      r_praise_count = #{record.rPraiseCount,jdbcType=INTEGER},      r_create_time = #{record.rCreateTime,jdbcType=TIMESTAMP},      r_update_time = #{record.rUpdateTime,jdbcType=TIMESTAMP}    <if test="_parameter != null" >      <include refid="Update_By_Example_Where_Clause" />    </if>  </update>  <update id="updateByPrimaryKeySelective" parameterType="com.mybatis.entity.WxRankingFlow" >    update wx_ranking_flow    <set >      <if test="rGiftDate != null" >        r_gift_date = #{rGiftDate,jdbcType=VARCHAR},      </if>      <if test="rSid != null" >        r_sid = #{rSid,jdbcType=VARCHAR},      </if>      <if test="rCardNo != null" >        r_card_no = #{rCardNo,jdbcType=VARCHAR},      </if>      <if test="rCardName != null" >        r_card_name = #{rCardName,jdbcType=VARCHAR},      </if>      <if test="rReOpenid != null" >        r_re_openid = #{rReOpenid,jdbcType=VARCHAR},      </if>      <if test="rNumber != null" >        r_number = #{rNumber,jdbcType=INTEGER},      </if>      <if test="rChainCode != null" >        r_chain_code = #{rChainCode,jdbcType=VARCHAR},      </if>      <if test="rChainId != null" >        r_chain_id = #{rChainId,jdbcType=VARCHAR},      </if>      <if test="rChainName != null" >        r_chain_name = #{rChainName,jdbcType=VARCHAR},      </if>      <if test="rTotalAmount != null" >        r_total_amount = #{rTotalAmount,jdbcType=INTEGER},      </if>      <if test="rTotalCount != null" >        r_total_count = #{rTotalCount,jdbcType=INTEGER},      </if>      <if test="rPraiseCount != null" >        r_praise_count = #{rPraiseCount,jdbcType=INTEGER},      </if>      <if test="rCreateTime != null" >        r_create_time = #{rCreateTime,jdbcType=TIMESTAMP},      </if>      <if test="rUpdateTime != null" >        r_update_time = #{rUpdateTime,jdbcType=TIMESTAMP},      </if>    </set>    where r_id = #{rId,jdbcType=VARCHAR}  </update>  <update id="updateByPrimaryKey" parameterType="com.mybatis.entity.WxRankingFlow" >    update wx_ranking_flow    set r_gift_date = #{rGiftDate,jdbcType=VARCHAR},      r_sid = #{rSid,jdbcType=VARCHAR},      r_card_no = #{rCardNo,jdbcType=VARCHAR},      r_card_name = #{rCardName,jdbcType=VARCHAR},      r_re_openid = #{rReOpenid,jdbcType=VARCHAR},      r_number = #{rNumber,jdbcType=INTEGER},      r_chain_code = #{rChainCode,jdbcType=VARCHAR},      r_chain_id = #{rChainId,jdbcType=VARCHAR},      r_chain_name = #{rChainName,jdbcType=VARCHAR},      r_total_amount = #{rTotalAmount,jdbcType=INTEGER},      r_total_count = #{rTotalCount,jdbcType=INTEGER},      r_praise_count = #{rPraiseCount,jdbcType=INTEGER},      r_create_time = #{rCreateTime,jdbcType=TIMESTAMP},      r_update_time = #{rUpdateTime,jdbcType=TIMESTAMP}    where r_id = #{rId,jdbcType=VARCHAR}  </update>  <select resultMap="BaseResultMap" parameterType="com.mybatis.entity.WxRankingFlowExample" id="selectByExampleWithRowbounds" >    select    <if test="distinct" >      distinct    </if>    <include refid="Base_Column_List" />    from wx_ranking_flow    <if test="_parameter != null" >      <include refid="Example_Where_Clause" />    </if>    <if test="orderByClause != null" >      order by ${orderByClause}    </if>  </select></mapperMAC 系统和Windows 系统注意路径问题 。Windows是:\  MAC是:/
原创粉丝点击