使用MyBatis Generator自动生成代码详解

MyBatis是一款开源且优秀的半自动化持久层框架。它为我们节省了大量的事务代码,使我们更加专注于业务逻辑的实现。

本博客将详细介绍如何通过Mybatis自动生成实体类,Dao接口和Mapping映射文件。

1.准备工作

在D盘新建一个文件夹autoCode,其内再建个src文件夹,导入以下jar包,并用记事本或eclipse新建个generatorConfig.xml文件。
这里写图片描述

2.在数据库中创建t_role表

MyBatis会根据这张表自动生成实体类(Role),Dao接口(RoleMapper)和Mapping映射文件(RoleMapper.xml),注意id要设置为主键,否则生成的方法中只有insert方法。
这里写图片描述

3.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.21.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:3306/mybatis" userId="root" password="admin">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成模型类存放位置-->
        <javaModelGenerator targetPackage="edu.pojo" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="edu.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="edu.IDao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <table tableName="t_role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

4.注释的地方需要按照实际情况更改。

<table tableName="t_role" domainObjectName="Role" 
        enableCountByExample="false" enableUpdateByExample="false" 
        enableDeleteByExample="false" enableSelectByExample="false" 
        selectByExampleQueryId="false"></table>  

其中tableName与domainObjectName必填,tableName表示数据库表名称(如t_role),domainObjectName表示Java中的实体类(如User)

5.打开PowerShell窗口

在当前目录下,按住shift键后,再按鼠标右键,会出现这个弹框,点击“在此处打开PowerShell窗口”
这里写图片描述

6.在PowerShell窗口中,输入以下命令:

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

这里写图片描述

7.查看生成的文件,如下:

这里写图片描述

Role.java

package edu.pojo;

public class Role {
    private Long id;

    private String roleName;

    private String note;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName == null ? null : roleName.trim();
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note == null ? null : note.trim();
    }
}

RoleMapper.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="edu.IDao.RoleMapper" >
  <resultMap id="BaseResultMap" type="edu.pojo.Role" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="role_name" property="roleName" jdbcType="VARCHAR" />
    <result column="note" property="note" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, role_name, note
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from t_role
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from t_role
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="edu.pojo.Role" >
    insert into t_role (id, role_name, note
      )
    values (#{id,jdbcType=BIGINT}, #{roleName,jdbcType=VARCHAR}, #{note,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="edu.pojo.Role" >
    insert into t_role
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="roleName != null" >
        role_name,
      </if>
      <if test="note != null" >
        note,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="roleName != null" >
        #{roleName,jdbcType=VARCHAR},
      </if>
      <if test="note != null" >
        #{note,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="edu.pojo.Role" >
    update t_role
    <set >
      <if test="roleName != null" >
        role_name = #{roleName,jdbcType=VARCHAR},
      </if>
      <if test="note != null" >
        note = #{note,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="edu.pojo.Role" >
    update t_role
    set role_name = #{roleName,jdbcType=VARCHAR},
      note = #{note,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>

RoleMapper.java

package edu.IDao;

import edu.pojo.Role;

public interface RoleMapper {
    int deleteByPrimaryKey(Long id);

    int insert(Role record);

    int insertSelective(Role record);

    Role selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Role record);

    int updateByPrimaryKey(Role record);
}

8.自动生成代码后,将其copy到项目中即可。

注意,数据库表中的id应设置为主键,否则自动生成的方法中只有insert方法。


参考博客:https://blog.csdn.net/zhshulin/article/details/23912615

猜你喜欢

转载自blog.csdn.net/qq_39039017/article/details/80495490