最近自己在搞一个项目,然后想使用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>