mybatis-generator扩展教程系列 -- 自定义sql xml文件

本文介绍如何扩展mybatis-generator,以自定义XML映射文件的格式,去除冗余的CRUD方法,增加SQL节点间的空行和调整SQL语句的缩进,使生成的XML文件更符合日常使用习惯。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         今天抽空写一下生成器比较重要的环节,如何自定义mybatis生成器的sql xml文件,因为原生出来的格式不好看,命名也不符合我们日常使用习惯,很多冗余的sql节点,下面我直接直入主题演示代码了,还是老规矩使用之前教程延续下来的项目用例


1.先看看我们原始生成出来的mapper.xml文件,1.节点间没有空行不符合我们日常格式 2.CRUD方法太累赘,我们其实只需要insert,update,delete,select

<?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.test.dao.QfdSketchMapper">
  <resultMap id="BaseResultMap" type="com.test.domain.QfdSketch">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="courseName" jdbcType="VARCHAR" property="coursename" />
    <result column="courseReq" jdbcType="VARCHAR" property="coursereq" />
    <result column="teaId" jdbcType="VARCHAR" property="teaid" />
    <result column="teaName" jdbcType="VARCHAR" property="teaname" />
    <result column="stuId" jdbcType="VARCHAR" property="stuid" />
    <result column="stuName" jdbcType="VARCHAR" property="stuname" />
    <result column="stuPhone" jdbcType="VARCHAR" property="stuphone" />
    <result column="textbookId" jdbcType="BIGINT" property="textbookid" />
    <result column="creator" jdbcType="VARCHAR" property="creator" />
    <result column="lastModifier" jdbcType="VARCHAR" property="lastmodifier" />
    <result column="countDate" jdbcType="INTEGER" property="countdate" />
    <result column="countTime" jdbcType="INTEGER" property="counttime" />
    <result column="countStu" jdbcType="INTEGER" property="countstu" />
    <result column="sketchType" jdbcType="INTEGER" property="sketchtype" />
    <result column="courseStyle" jdbcType="INTEGER" property="coursestyle" />
    <result column="classType" jdbcType="INTEGER" property="classtype" />
    <result column="process" jdbcType="INTEGER" property="process" />
    <result column="courseStatus" jdbcType="INTEGER" property="coursestatus" />
    <result column="pay" jdbcType="VARCHAR" property="pay" />
    <result column="perPay" jdbcType="VARCHAR" property="perpay" />
    <result column="couponPay" jdbcType="VARCHAR" property="couponpay" />
    <result column="originalPay" jdbcType="VARCHAR" property="originalpay" />
    <result column="couponRule" jdbcType="INTEGER" property="couponrule" />
    <result column="beginDate" jdbcType="BIGINT" property="begindate" />
    <result column="endDate" jdbcType="BIGINT" property="enddate" />
    <result column="courseNum" jdbcType="INTEGER" property="coursenum" />
    <result column="hasNum" jdbcType="INTEGER" property="hasnum" />
    <result column="cancelNum" jdbcType="INTEGER" property="cancelnum" />
    <result column="courseNumInput" jdbcType="INTEGER" property="coursenuminput" />
    <result column="orderNum" jdbcType="INTEGER" property="ordernum" />
    <result column="fav" jdbcType="BIGINT" property="fav" />
    <result column="vipLevel" jdbcType="INTEGER" property="viplevel" />
    <result column="cancelReason" jdbcType="INTEGER" property="cancelreason" />
    <result column="createDate" jdbcType="BIGINT" property="createdate" />
    <result column="lastModDate" jdbcType="BIGINT" property="lastmoddate" />
    <result column="formatter" jdbcType="VARCHAR" property="formatter" />
    <result column="status" jdbcType="TINYINT" property="status" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    id, courseName, courseReq, teaId, teaName, stuId, stuName, stuPhone, textbookId, 
    creator, lastModifier, countDate, countTime, countStu, sketchType, courseStyle, classType, 
    process, courseStatus, pay, perPay, couponPay, originalPay, couponRule, beginDate, 
    endDate, courseNum, hasNum, cancelNum, courseNumInput, orderNum, fav, vipLevel, cancelReason, 
    createDate, lastModDate, formatter, status
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select 
    <include refid="Base_Column_List" />
    from qfd_sketch
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    delete from qfd_sketch
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.test.domain.QfdSketch">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into qfd_sketch (id, courseName, courseReq, 
      teaId, teaName, stuId, 
      stuName, stuPhone, textbookId, 
      creator, lastModifier, countDate, 
      countTime, countStu, sketchType, 
      courseStyle, classType, process, 
      courseStatus, pay, perPay, 
      couponPay, originalPay, couponRule, 
      beginDate, endDate, courseNum, 
      hasNum, cancelNum, courseNumInput, 
      orderNum, fav, vipLevel, 
      cancelReason, createDate, lastModDate, 
      formatter, status)
    values (#{id,jdbcType=BIGINT}, #{coursename,jdbcType=VARCHAR}, #{coursereq,jdbcType=VARCHAR}, 
      #{teaid,jdbcType=VARCHAR}, #{teaname,jdbcType=VARCHAR}, #{stuid,jdbcType=VARCHAR}, 
      #{stuname,jdbcType=VARCHAR}, #{stuphone,jdbcType=VARCHAR}, #{textbookid,jdbcType=BIGINT}, 
      #{creator,jdbcType=VARCHAR}, #{lastmodifier,jdbcType=VARCHAR}, #{countdate,jdbcType=INTEGER}, 
      #{counttime,jdbcType=INTEGER}, #{countstu,jdbcType=INTEGER}, #{sketchtype,jdbcType=INTEGER}, 
      #{coursestyle,jdbcType=INTEGER}, #{classtype,jdbcType=INTEGER}, #{process,jdbcType=INTEGER}, 
      #{coursestatus,jdbcType=INTEGER}, #{pay,jdbcType=VARCHAR}, #{perpay,jdbcType=VARCHAR}, 
      #{couponpay,jdbcType=VARCHAR}, #{originalpay,jdbcType=VARCHAR}, #{couponrule,jdbcType=INTEGER}, 
      #{begindate,jdbcType=BIGINT}, #{enddate,jdbcType=BIGINT}, #{coursenum,jdbcType=INTEGER}, 
      #{hasnum,jdbcType=INTEGER}, #{cancelnum,jdbcType=INTEGER}, #{coursenuminput,jdbcType=INTEGER}, 
      #{ordernum,jdbcType=INTEGER}, #{fav,jdbcType=BIGINT}, #{viplevel,jdbcType=INTEGER}, 
      #{cancelreason,jdbcType=INTEGER}, #{createdate,jdbcType=BIGINT}, #{lastmoddate,jdbcType=BIGINT}, 
      #{formatter,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT})
  </insert>
  <insert id="insertSelective" parameterType="com.test.domain.QfdSketch">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into qfd_sketch
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="coursename != null">
        courseName,
      </if>
      <if test="coursereq != null">
        courseReq,
      </if>
      <if test="teaid != null">
        teaId,
      </if>
      <if test="teaname != null">
        teaName,
      </if>
      <if test="stuid != null">
        stuId,
      </if>
      <if test="stuname != null">
        stuName,
      </if>
      <if test="stuphone != null">
        stuPhone,
      </if>
      <if test="textbookid != null">
        textbookId,
      </if>
      <if test="creator != null">
        creator,
      </if>
      <if test="lastmodifier != null">
        lastModifier,
      </if>
      <if test="countdate != null">
        countDate,
      </if>
      <if test="counttime != null">
        countTime,
      </if>
      <if test="countstu != null">
        countStu,
      </if>
      <if test="sketchtype != null">
        sketchType,
      </if>
      <if test="coursestyle != null">
        courseStyle,
      </if>
      <if test="classtype != null">
        classType,
      </if>
      <if test="process != null">
        process,
      </if>
      <if test="coursestatus != null">
        courseStatus,
      </if>
      <if test="pay != null">
        pay,
      </if>
      <if test="perpay != null">
        perPay,
      </if>
      <if test="couponpay != null">
        couponPay,
      </if>
      <if test="originalpay != null">
        originalPay,
      </if>
      <if test="couponrule != null">
        couponRule,
      </if>
      <if test="begindate != null">
        beginDate,
      </if>
      <if test="enddate != null">
        endDate,
      </if>
      <if test="coursenum != null">
        courseNum,
      </if>
      <if test="hasnum != null">
        hasNum,
      </if>
      <if test="cancelnum != null">
        cancelNum,
      </if>
      <if test="coursenuminput != null">
        courseNumInput,
      </if>
      <if test="ordernum != null">
        orderNum,
      </if>
      <if test="fav != null">
        fav,
      </if>
      <if test="viplevel != null">
        vipLevel,
      </if>
      <if test="cancelreason != null">
        cancelReason,
      </if>
      <if test="createdate != null">
        createDate,
      </if>
      <if test="lastmoddate != null">
        lastModDate,
      </if>
      <if test="formatter != null">
        formatter,
      </if>
      <if test="status != null">
        status,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="coursename != null">
        #{coursename,jdbcType=VARCHAR},
      </if>
      <if test="coursereq != null">
        #{coursereq,jdbcType=VARCHAR},
      </if>
      <if test="teaid != null">
        #{teaid,jdbcType=VARCHAR},
      </if>
      <if test="teaname != null">
        #{teaname,jdbcType=VARCHAR},
      </if>
      <if test="stuid != null">
        #{stuid,jdbcType=VARCHAR},
      </if>
      <if test="stuname != null">
        #{stuname,jdbcType=VARCHAR},
      </if>
      <if test="stuphone != null">
        #{stuphone,jdbcType=VARCHAR},
      </if>
      <if test="textbookid != null">
        #{textbookid,jdbcType=BIGINT},
      </if>
      <if test="creator != null">
        #{creator,jdbcType=VARCHAR},
      </if>
      <if test="lastmodifier != null">
        #{lastmodifier,jdbcType=VARCHAR},
      </if>
      <if test="countdate != null">
        #{countdate,jdbcType=INTEGER},
      </if>
      <if test="counttime != null">
        #{counttime,jdbcType=INTEGER},
      </if>
      <if test="countstu != null">
        #{countstu,jdbcType=INTEGER},
      </if>
      <if test="sketchtype != null">
        #{sketchtype,jdbcType=INTEGER},
      </if>
      <if test="coursestyle != null">
        #{coursestyle,jdbcType=INTEGER},
      </if>
      <if test="classtype != null">
        #{classtype,jdbcType=INTEGER},
      </if>
      <if test="process != null">
        #{process,jdbcType=INTEGER},
      </if>
      <if test="coursestatus != null">
        #{coursestatus,jdbcType=INTEGER},
      </if>
      <if test="pay != null">
        #{pay,jdbcType=VARCHAR},
      </if>
      <if test="perpay != null">
        #{perpay,jdbcType=VARCHAR},
      </if>
      <if test="couponpay != null">
        #{couponpay,jdbcType=VARCHAR},
      </if>
      <if test="originalpay != null">
        #{originalpay,jdbcType=VARCHAR},
      </if>
      <if test="couponrule != null">
        #{couponrule,jdbcType=INTEGER},
      </if>
      <if test="begindate != null">
        #{begindate,jdbcType=BIGINT},
      </if>
      <if test="enddate != null">
        #{enddate,jdbcType=BIGINT},
      </if>
      <if test="coursenum != null">
        #{coursenum,jdbcType=INTEGER},
      </if>
      <if test="hasnum != null">
        #{hasnum,jdbcType=INTEGER},
      </if>
      <if test="cancelnum != null">
        #{cancelnum,jdbcType=INTEGER},
      </if>
      <if test="coursenuminput != null">
        #{coursenuminput,jdbcType=INTEGER},
      </if>
      <if test="ordernum != null">
        #{ordernum,jdbcType=INTEGER},
      </if>
      <if test="fav != null">
        #{fav,jdbcType=BIGINT},
      </if>
      <if test="viplevel != null">
        #{viplevel,jdbcType=INTEGER},
      </if>
      <if test="cancelreason != null">
        #{cancelreason,jdbcType=INTEGER},
      </if>
      <if test="createdate != null">
        #{createdate,jdbcType=BIGINT},
      </if>
      <if test="lastmoddate != null">
        #{lastmoddate,jdbcType=BIGINT},
      </if>
      <if test="formatter != null">
        #{formatter,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=TINYINT},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.test.domain.QfdSketch">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update qfd_sketch
    <set>
      <if test="coursename != null">
        courseName = #{coursename,jdbcType=VARCHAR},
      </if>
      <if test="coursereq != null">
        courseReq = #{coursereq,jdbcType=VARCHAR},
      </if>
      <if test="teaid != null">
        teaId = #{teaid,jdbcType=VARCHAR},
      </if>
      <if test="teaname != null">
        teaName = #{teaname,jdbcType=VARCHAR},
      </if>
      <if test="stuid != null">
        stuId = #{stuid,jdbcType=VARCHAR},
      </if>
      <if test="stuname != null">
        stuName = #{stuname,jdbcType=VARCHAR},
      </if>
      <if test="stuphone != null">
        stuPhone = #{stuphone,jdbcType=VARCHAR},
      </if>
      <if test="textbookid != null">
        textbookId = #{textbookid,jdbcType=BIGINT},
      </if>
      <if test="creator != null">
        creator = #{creator,jdbcType=VARCHAR},
      </if>
      <if test="lastmodifier != null">
        lastModifier = #{lastmodifier,jdbcType=VARCHAR},
      </if>
      <if test="countdate != null">
        countDate = #{countdate,jdbcType=INTEGER},
      </if>
      <if test="counttime != null">
        countTime = #{counttime,jdbcType=INTEGER},
      </if>
      <if test="countstu != null">
        countStu = #{countstu,jdbcType=INTEGER},
      </if>
      <if test="sketchtype != null">
        sketchType = #{sketchtype,jdbcType=INTEGER},
      </if>
      <if test="coursestyle != null">
        courseStyle = #{coursestyle,jdbcType=INTEGER},
      </if>
      <if test="classtype != null">
        classType = #{classtype,jdbcType=INTEGER},
      </if>
      <if test="process != null">
        process = #{process,jdbcType=INTEGER},
      </if>
      <if test="coursestatus != null">
        courseStatus = #{coursestatus,jdbcType=INTEGER},
      </if>
      <if test="pay != null">
        pay = #{pay,jdbcType=VARCHAR},
      </if>
      <if test="perpay != null">
        perPay = #{perpay,jdbcType=VARCHAR},
      </if>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值