泡面3分钟 2022-04-29 15:25 采纳率: 100%
浏览 119
已结题

mybatis generator怎么生成自定义的mapper.xml

最近自己在搞一个项目,然后想使用mybatis generator一键生成mybatis的java代码,还有xml配置,但是mybatis generator默认生成的sql相当不好用,insert、update会要求全字段set,还没有find方法,有没有人教下我这个菜,不修改mybatis generator源码,实现修改mybatis generator生成的默认sql,就像修改Dao mapper文件名样,只用继承 import org.mybatis.generator.api.PluginAdapter; 然后改改就可以实现一样,修改xml中的sql,有没有可以继承修改的方法

我想要的生成效果,如下:

<?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.cmpay.phone.dao.CustomerPopupDao">

  <resultMap id="BaseResultMap" type="com.cmpay.phone.entity.TestTableDO">
    <id column="serial_no" jdbcType="VARCHAR" property="serialNo" />
    <result column="product_code" jdbcType="VARCHAR" property="productCode" />
    <result column="effective_flag" jdbcType="VARCHAR" property="effectiveFlag" />
    <result column="describe_m" jdbcType="VARCHAR" property="describe" />
    <result column="prompts" jdbcType="VARCHAR" property="prompts" />
    <result column="image_string" jdbcType="VARCHAR" property="imageString" />
    <result column="operator_m" jdbcType="VARCHAR" property="operator" />
    <result column="operate_date" jdbcType="VARCHAR" property="operateDate" />
    <result column="operate_time" jdbcType="VARCHAR" property="operateTime" />
  </resultMap>

  <sql id="Base_Column_List">
    serial_no, product_code, effective_flag, describe_m, prompts, image_string, operator_m,
    operate_date, operate_time
  </sql>


  <select id="get" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from test_table
    where product_code = #{productCode,jdbcType=VARCHAR}
  </select>


  <delete id="delete" parameterType="java.lang.String">
    delete from test_table
    where product_code = #{productCode,jdbcType=VARCHAR}
  </delete>


  <insert id="insert" keyColumn="serial_no" keyProperty="serialNo" parameterType="com.cmpay.phone.entity.TestTableDO" useGeneratedKeys="true">
    insert into test_table
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="serialNo != null">
        serial_no,
      </if>
      <if test="productCode != null">
        product_code,
      </if>
      <if test="effectiveFlag != null">
        effective_flag,
      </if>
      <if test="describe != null">
        describe_m,
      </if>
      <if test="prompts != null">
        prompts,
      </if>
      <if test="imageString != null">
        image_string,
      </if>
      <if test="operator != null">
        operator_m,
      </if>
      <if test="operateDate != null">
        operate_date,
      </if>
      <if test="operateTime != null">
        operate_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="serialNo != null">
        #{serialNo,jdbcType=VARCHAR},
      </if>
      <if test="productCode != null">
        #{productCode,jdbcType=VARCHAR},
      </if>
      <if test="effectiveFlag != null">
        #{effectiveFlag,jdbcType=VARCHAR},
      </if>
      <if test="describe != null">
        #{describe,jdbcType=VARCHAR},
      </if>
      <if test="prompts != null">
        #{prompts,jdbcType=VARCHAR},
      </if>
      <if test="imageString != null">
        #{imageString,jdbcType=VARCHAR},
      </if>
      <if test="operator != null">
        #{operator,jdbcType=VARCHAR},
      </if>
      <if test="operateDate != null">
        #{operateDate,jdbcType=VARCHAR},
      </if>
      <if test="operateTime != null">
        #{operateTime,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>

  <update id="update" parameterType="com.cmpay.phone.entity.TestTableDO">
    update test_table
    <set>
      <if test="productCode != null">
        product_code = #{productCode,jdbcType=VARCHAR},
      </if>
      <if test="effectiveFlag != null">
        effective_flag = #{effectiveFlag,jdbcType=VARCHAR},
      </if>
      <if test="describe != null">
        describe_m = #{describe,jdbcType=VARCHAR},
      </if>
      <if test="prompts != null">
        prompts = #{prompts,jdbcType=VARCHAR},
      </if>
      <if test="imageString != null">
        image_string = #{imageString,jdbcType=VARCHAR},
      </if>
      <if test="operator != null">
        operator_m = #{operator,jdbcType=VARCHAR},
      </if>
      <if test="operateDate != null">
        operate_date = #{operateDate,jdbcType=VARCHAR},
      </if>
      <if test="operateTime != null">
        operate_time = #{operateTime,jdbcType=VARCHAR},
      </if>
    </set>
    where product_code = #{productCode,jdbcType=VARCHAR}
  </update>

</mapper>
现在mybatis generator给我默认生成的效果如下:

<?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.zc.mock.dao.TestTableDao" >
  <resultMap id="BaseResultMap" type="com.zc.mock.entity.TestTableDO" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="open_type" property="openType" jdbcType="INTEGER" />
  </resultMap>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from test_table
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.zc.mock.entity.TestTableDO" >
    insert into test_table (id, open_type)
    values (#{id,jdbcType=BIGINT}, #{openType,jdbcType=INTEGER})
  </insert>
  <update id="updateByPrimaryKey" parameterType="com.zc.mock.entity.TestTableDO" >
    update test_table
    set open_type = #{openType,jdbcType=INTEGER}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select id, open_type
    from test_table
    where id = #{id,jdbcType=BIGINT}
  </select>
  <select id="selectAll" resultMap="BaseResultMap" >
    select id, open_type
    from test_table
  </select>
</mapper>

  • 写回答

6条回答 默认 最新

  • 一起随缘 2022-04-29 16:46
    关注

    如果是仅仅是修改某一个实体类对应的单个sql,可以参考如下链接,但是如果想通过配置数据库连接信息批量生成xml文件中sql语句,估计很难实现,因为无法识别你要操作那个表以及具体哪些字段,如果非要这样实现,估计要改源码,自定义生成规则才可以。最简单的做法是项目集成mybatis-plus,这样单表的话就不用写sql了,直接继承BaseMapper后就可以调用增删改查相关的接口,而且还可以自定义sql,灵活方便https://blog.csdn.net/weixin_41547362/article/details/122862765

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 5月14日
  • 已采纳回答 5月6日
  • 赞助了问题酬金50元 4月29日
  • 创建了问题 4月29日

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器