MyBatis Generator 1.3.7 using automatic code generation

Mybatis MyBatis Generator is an automatic code generation tool that can be automatically generated by the configuration file.
MyBatis Generator There are several ways to generate the code, is the following one.

First, the official website to download MyBatis Generator 

1. Download: https: //github.com/mybatis/generator/releases
latest version is 1.3.7.
Automatically creates the code in the command line you can just download mybatis-generator-core-1.3.7.zip.

2, extract the archive, mainly lib directory, go in only three jar files

Second, create a profile, etc.

1, create a profile generatorConfig.xml in the lib directory, specify the database connection information, generate a model and mapping files and other information:

<?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, because the mysql, so lib directory need driver package into mysql mysql-connector-java-5.1.37.jar

3, the configuration file specifies the output directory src (targetProject = "src"), so you need to create a src directory lib directory manually, the final document as follows:


Third, the command line to generate codes

1, the row location cmd command to D: \ projects \ mybatis-generator-core-1.3.7 \ lib

2, -overwrite enter the command java -jar mybatis-generator-core- 1.3.7.jar -configfile generatorConfig.xml
   announces a successful operation under normal conditions.

3, see the src directory, it has generated three subdirectories, and the corresponding documents are inside

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);
}

 

Guess you like

Origin blog.csdn.net/gdjlc/article/details/84645084