本文主要是介绍使用Mybatis Generator插件生成纯净代码,自定义扩展注释和注释,xml映射文件,不用修改源码!!!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先看效果: (基于Mybatis-generator-core-1.4.0)
生成模型(实体): 根据MySQL库中的tb_prod_info表字段及注释自动生成.
package com.ezsyn.cloudstudy.product.dao.entity;import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;/*** 产品信息表* <p>* tb_prod_info** @author Aylven* @date 2020-04-06*/
@Getter
@Setter
@ToString
public class TbProdInfo implements Serializable {private static final long serialVersionUID = 1L;/*** 产品代码 主键*/@ApiModelProperty(value = "产品代码 主键")private String code;/*** 产品名称*/@ApiModelProperty(value = "产品名称")private String name;/*** 是否有效*/@ApiModelProperty(value = "是否有效")private Boolean enabled;/*** 创建时间*/@ApiModelProperty(value = "创建时间")private Date createTime;/*** 创建人ID*/@ApiModelProperty(value = "创建人ID")private Integer createBy;/*** 更新时间*/@ApiModelProperty(value = "更新时间")private Date updateTime;/*** 更新人ID*/@ApiModelProperty(value = "更新人ID")private Integer updateBy;
}
生成Mapper接口:生成常用的接口方法,自动生成注释,添加了@Mapper注解.
package com.ezsyn.cloudstudy.product.dao.mapper;import com.ezsyn.cloudstudy.product.dao.entity.TbProdInfo;
import org.apache.ibatis.annotations.Mapper;/*** 产品信息表* <p>* tb_prod_info** @author Aylven* @date 2020-04-06*/
@Mapper
public interface TbProdInfoMapper {/*** 根据主键删除记录*/int deleteByPrimaryKey(String code);/*** 插入全部字段*/int insert(TbProdInfo record);/*** 插入选择的字段*/int insertSelective(TbProdInfo record);/*** 根据主键查询*/TbProdInfo selectByPrimaryKey(String code);/*** 根据主键更新选择的字段*/int updateByPrimaryKeySelective(TbProdInfo record);/*** 根据主键更新全部字段*/int updateByPrimaryKey(TbProdInfo record);
}
生成mapper XML映射文件:去掉了接口方法中的jdbcType
<?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.ezsyn.cloudstudy.product.dao.mapper.TbProdInfoMapper"><resultMap id="BaseResultMap" type="com.ezsyn.cloudstudy.product.dao.entity.TbProdInfo"><id column="code" jdbcType="VARCHAR" property="code"/><result column="name" jdbcType="VARCHAR" property="name"/><result column="price" jdbcType="DECIMAL" property="price"/><result column="stock" jdbcType="INTEGER" property="stock"/><result column="enabled" jdbcType="BIT" property="enabled"/><result column="create_time" jdbcType="TIMESTAMP" property="createTime"/><result column="create_by" jdbcType="INTEGER" property="createBy"/><result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/><result column="update_by" jdbcType="INTEGER" property="updateBy"/></resultMap><sql id="Base_Column_List">code, name, price, stock, enabled, create_time, create_by, update_time, update_by</sql><select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">select<include refid="Base_Column_List"/>from tb_prod_infowhere code = #{code}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.String">deletefrom tb_prod_infowhere code = #{code}</delete><insert id="insert" parameterType="com.ezsyn.cloudstudy.product.dao.entity.TbProdInfo"><selectKey keyProperty="code" order="AFTER" resultType="java.lang.String">SELECT LAST_INSERT_ID()</selectKey>insert into tb_prod_info (code, name, price,stock, enabled, create_time,create_by, update_time, update_by)values (#{code}, #{name}, #{price},#{stock}, #{enabled}, #{createTime},#{createBy}, #{updateTime}, #{updateBy})</insert><insert id="insertSelective" parameterType="com.ezsyn.cloudstudy.product.dao.entity.TbProdInfo"><selectKey keyProperty="code" order="AFTER" resultType="java.lang.String">SELECT LAST_INSERT_ID()</selectKey>insert into tb_prod_info<trim prefix="(" suffix=")" suffixOverrides=",">code,<if test="name != null">name,</if><if test="price != null">price,</if><if test="stock != null">stock,</if><if test="enabled != null">enabled,</if><if test="createTime != null">create_time,</if><if test="createBy != null">create_by,</if><if test="updateTime != null">update_time,</if><if test="updateBy != null">
这篇关于使用Mybatis Generator插件生成纯净代码,自定义扩展注释和注释,xml映射文件,不用修改源码!!!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!