mybatis_自动生成工具

1. 下载mybatis-generator-core工具包:

mybatis-generator-core.1.3.1下载路径:http://www.cnd8.com/downinfo/8521.html

解压后目录如下:

 mybatis_自动生成工具_第1张图片

mybatis-generator-core最新版本为1.3.5,下载路径:

https://github.com/mybatis/generator/releases

解压后目录如下:

 mybatis_自动生成工具_第2张图片

2. 加包

1.3.5lib目录内容如下:

 

添加mysql-connector-java-5.1.38.jar

 

3. 创建xml文件

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>

    

    <classPathEntry    location="mysql-connector-java-5.1.38.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/test" userId="root" password="123qwe">

        jdbcConnection>

        <javaTypeResolver>

            <property name="forceBigDecimals" value="false"/>

        javaTypeResolver>

        

        <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>

        

        <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">

            <property name="enableSubPackages" value="true"/>

        javaClientGenerator>

        

        <table tableName="t_person" domainObjectName="Person" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>

    context>

generatorConfiguration>

其中需要修改的都已标记,targetProject为生成的文件路径,tableName为表名,domainObjectName为实体类名

 

4. 执行命令

Cmd定位到lib目录:

执行命令:java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite

 mybatis_自动生成工具_第3张图片

此时在src目录下生成了lcw目录,其下有三个目录:

 

 

5. 查看结果:

1.model

package lcw.model;

import java.math.BigDecimal;

public class Person {

    private Integer id;

    private String name;

    private String password;

    private BigDecimal account;

    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 getPassword() {

        return password;

    }

    public void setPassword(String password) {

        this.password = password == null ? null : password.trim();

    }

    public BigDecimal getAccount() {

        return account;

    }

    public void setAccount(BigDecimal account) {

        this.account = account;

    }

}

 

2.接口文件

package lcw.dao;

import lcw.model.Person;

public interface PersonMapper {

    int deleteByPrimaryKey(Integer id);

    int insert(Person record);

    int insertSelective(Person record);

    Person selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Person record);

    int updateByPrimaryKey(Person record);

}

 

2.映射文件

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.PersonMapper">

  <resultMap id="BaseResultMap" type="lcw.model.Person">

    <id column="id" jdbcType="INTEGER" property="id" />

    <result column="name" jdbcType="VARCHAR" property="name" />

    <result column="password" jdbcType="VARCHAR" property="password" />

    <result column="account" jdbcType="DECIMAL" property="account" />

  resultMap>

  <sql id="Base_Column_List">

    id, name, password, account

  sql>

  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">

    select

    <include refid="Base_Column_List" />

    from t_person

    where id = #{id,jdbcType=INTEGER}

  select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">

    delete from t_person

    where id = #{id,jdbcType=INTEGER}

  delete>

  <insert id="insert" parameterType="lcw.model.Person">

    insert into t_person (id, name, password,

      account)

    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},

      #{account,jdbcType=DECIMAL})

  insert>

  <insert id="insertSelective" parameterType="lcw.model.Person">

    insert into t_person

    <trim prefix="(" suffix=")" suffixOverrides=",">

      <if test="id != null">

        id,

      if>

      <if test="name != null">

        name,

      if>

      <if test="password != null">

        password,

      if>

      <if test="account != null">

        account,

      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="password != null">

        #{password,jdbcType=VARCHAR},

      if>

      <if test="account != null">

        #{account,jdbcType=DECIMAL},

      if>

    trim>

  insert>

  <update id="updateByPrimaryKeySelective" parameterType="lcw.model.Person">

    update t_person

    <set>

      <if test="name != null">

        name = #{name,jdbcType=VARCHAR},

      if>

      <if test="password != null">

        password = #{password,jdbcType=VARCHAR},

      if>

      <if test="account != null">

        account = #{account,jdbcType=DECIMAL},

      if>

    set>

    where id = #{id,jdbcType=INTEGER}

  update>

  <update id="updateByPrimaryKey" parameterType="lcw.model.Person">

    update t_person

    set name = #{name,jdbcType=VARCHAR},

      password = #{password,jdbcType=VARCHAR},

      account = #{account,jdbcType=DECIMAL}

    where id = #{id,jdbcType=INTEGER}

  update>

mapper>

你可能感兴趣的:(mybatis)