使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件

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

1、相关文件
关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases
由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar包
以下是相关文件截图:
使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
和Hibernate逆向生成一样,这里也需要一个配置文件:
generatorConfig.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="pers.lbw.digitalmall.dao.UserMapper">
  <resultMap id="BaseResultMap" type="pers.lbw.digitalmall.beans.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="nickname" jdbcType="VARCHAR" property="nickname" />
    <result column="sex" jdbcType="INTEGER" property="sex" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="birthday" jdbcType="DATE" property="birthday" />
    <result column="place" jdbcType="INTEGER" property="place" />
    <result column="street" jdbcType="VARCHAR" property="street" />
    <result column="role" jdbcType="INTEGER" property="role" />
  </resultMap>
  <sql id="Base_Column_List">
    id, username, password, name, nickname, sex, phone, email, birthday, place, street, 
    role
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user_table
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user_table
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="pers.lbw.digitalmall.beans.User">
    insert into user_table (id, username, password, 
      name, nickname, sex, 
      phone, email, birthday, 
      place, street, role
      )
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{name,jdbcType=VARCHAR}, #{nickname,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER}, 
      #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE}, 
      #{place,jdbcType=INTEGER}, #{street,jdbcType=VARCHAR}, #{role,jdbcType=INTEGER}
      )
  </insert>
  <insert id="insertSelective" parameterType="pers.lbw.digitalmall.beans.User">
    insert into user_table
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="name != null">
        name,
      </if>
      <if test="nickname != null">
        nickname,
      </if>
      <if test="sex != null">
        sex,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="birthday != null">
        birthday,
      </if>
      <if test="place != null">
        place,
      </if>
      <if test="street != null">
        street,
      </if>
      <if test="role != null">
        role,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="nickname != null">
        #{nickname,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        #{sex,jdbcType=INTEGER},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null">
        #{birthday,jdbcType=DATE},
      </if>
      <if test="place != null">
        #{place,jdbcType=INTEGER},
      </if>
      <if test="street != null">
        #{street,jdbcType=VARCHAR},
      </if>
      <if test="role != null">
        #{role,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="pers.lbw.digitalmall.beans.User">
    update user_table
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="nickname != null">
        nickname = #{nickname,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        sex = #{sex,jdbcType=INTEGER},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null">
        birthday = #{birthday,jdbcType=DATE},
      </if>
      <if test="place != null">
        place = #{place,jdbcType=INTEGER},
      </if>
      <if test="street != null">
        street = #{street,jdbcType=VARCHAR},
      </if>
      <if test="role != null">
        role = #{role,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="pers.lbw.digitalmall.beans.User">
    update user_table
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      name = #{name,jdbcType=VARCHAR},
      nickname = #{nickname,jdbcType=VARCHAR},
      sex = #{sex,jdbcType=INTEGER},
      phone = #{phone,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      birthday = #{birthday,jdbcType=DATE},
      place = #{place,jdbcType=INTEGER},
      street = #{street,jdbcType=VARCHAR},
      role = #{role,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

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

上面配置文件中的:

<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

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

生成语句bat文件:

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

最后目录:
使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
2、使用方法
运行bat文件就可以了