mybaties mapper 继承机制解决改表改mapper的苦恼

       本文主要讲解在使用mybaties中通过mybaties generator生成基本操作代码,然后通过 mybaties mapper 继承机制来解决某些情况下经常改表导致改mapper文件的苦恼。
 

好了废话少说上代码了

首先是 mapper java文件的继承
 

$title(ArticleMapper.java)
public interface ArticleMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Article record);

    int insertSelective(Article record);

    Article selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Article record);

    int updateByPrimaryKey(Article record);
}

以上mapper文件为mybaties generator生成的代码。

$title(ArticleExtMapper.java)
public interface ArticleExtMapper extends ArticleMapper {
    Article getLast(@Param("id") Integer id, @Param("type") Integer type);

    Article getNext(@Param("id") Integer id, @Param("type") Integer type);

    List<Article> getList(@Param("title") String title, @Param("category") String category, @Param("type") Integer type);
}

以上为业务使用中扩展的一些应用查询方法
 

下面上mapper xml文件的使用

$title(ArticleMapper.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="net.ifok.ocms.business.mapper.ArticleMapper">
  <resultMap id="BaseResultMap" type="net.ifok.ocms.business.model.Article">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="image" jdbcType="VARCHAR" property="image" />
    <result column="url" jdbcType="VARCHAR" property="url" />
    <result column="content" jdbcType="VARCHAR" property="content" />
    <result column="content_text" jdbcType="VARCHAR" property="contentText" />
    <result column="category" jdbcType="VARCHAR" property="category" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="keywords" jdbcType="VARCHAR" property="keywords" />
    <result column="description" jdbcType="VARCHAR" property="description" />
    <result column="customer" jdbcType="VARCHAR" property="customer" />
  </resultMap>
  <sql id="Base_Column_List">
    id, title, image, url, content, content_text, category, type, create_time, update_time, 
    keywords, description, customer
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from article
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from article
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="net.ifok.ocms.business.model.Article">
    insert into article (id, title, image, 
      url, content, content_text, 
      category, type, create_time, 
      update_time, keywords, description, 
      customer)
    values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{image,jdbcType=VARCHAR}, 
      #{url,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}, #{contentText,jdbcType=VARCHAR}, 
      #{category,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, 
      #{updateTime,jdbcType=TIMESTAMP}, #{keywords,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, 
      #{customer,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="net.ifok.ocms.business.model.Article">
    insert into article
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="title != null">
        title,
      </if>
      <if test="image != null">
        image,
      </if>
      <if test="url != null">
        url,
      </if>
      <if test="content != null">
        content,
      </if>
      <if test="contentText != null">
        content_text,
      </if>
      <if test="category != null">
        category,
      </if>
      <if test="type != null">
        type,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="keywords != null">
        keywords,
      </if>
      <if test="description != null">
        description,
      </if>
      <if test="customer != null">
        customer,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="title != null">
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="image != null">
        #{image,jdbcType=VARCHAR},
      </if>
      <if test="url != null">
        #{url,jdbcType=VARCHAR},
      </if>
      <if test="content != null">
        #{content,jdbcType=VARCHAR},
      </if>
      <if test="contentText != null">
        #{contentText,jdbcType=VARCHAR},
      </if>
      <if test="category != null">
        #{category,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="keywords != null">
        #{keywords,jdbcType=VARCHAR},
      </if>
      <if test="description != null">
        #{description,jdbcType=VARCHAR},
      </if>
      <if test="customer != null">
        #{customer,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="net.ifok.ocms.business.model.Article">
    update article
    <set>
      <if test="title != null">
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="image != null">
        image = #{image,jdbcType=VARCHAR},
      </if>
      <if test="url != null">
        url = #{url,jdbcType=VARCHAR},
      </if>
      <if test="content != null">
        content = #{content,jdbcType=VARCHAR},
      </if>
      <if test="contentText != null">
        content_text = #{contentText,jdbcType=VARCHAR},
      </if>
      <if test="category != null">
        category = #{category,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        type = #{type,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="keywords != null">
        keywords = #{keywords,jdbcType=VARCHAR},
      </if>
      <if test="description != null">
        description = #{description,jdbcType=VARCHAR},
      </if>
      <if test="customer != null">
        customer = #{customer,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="net.ifok.ocms.business.model.Article">
    update article
    set title = #{title,jdbcType=VARCHAR},
      image = #{image,jdbcType=VARCHAR},
      url = #{url,jdbcType=VARCHAR},
      content = #{content,jdbcType=VARCHAR},
      content_text = #{contentText,jdbcType=VARCHAR},
      category = #{category,jdbcType=VARCHAR},
      type = #{type,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      keywords = #{keywords,jdbcType=VARCHAR},
      description = #{description,jdbcType=VARCHAR},
      customer = #{customer,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

以上为mybaties generator生成的代码。
 

$title(ArticleExtMapper.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="net.ifok.ocms.business.mapper.ext.ArticleExtMapper">
  <resultMap id="BaseResultMap" type="net.ifok.ocms.business.model.Article" extends="net.ifok.ocms.business.mapper.ArticleMapper.BaseResultMap">
  </resultMap>

  <select id="getLast" resultMap="BaseResultMap">
    select * FROM article where type=${type} and id  <![CDATA[ < ]]> #{id} order by id desc limit 1
  </select>
  <select id="getNext" resultMap="BaseResultMap">
    select * FROM article where type=#{type} and id <![CDATA[ > ]]>#{id} order by id asc limit 1
  </select>
  <select id="getList" resultMap="BaseResultMap">
    SELECT * from article where 1=1
    and type=#{type}
    <if test="title!=null">
      and title like  CONCAT(CONCAT('%', #{title}), '%')
    </if>
    <if test="category !=null and category !=''">
      and category=#{category}
    </if>
    order by create_time desc
  </select>
</mapper>

以上为扩展查询

事项说明:

  1. ArticleMapper中有,ArticleExtMapper.xml中没有,ArticleExtMapper沿用ArticleMapper中的定义
  2. ArticleMapper中有,ArticleExtMapper.xml中也有,ArticleExtMapper使用ArticleExtMapper.xml中的定义
  3. ArticleMapper中没有,ArticleExtMapper.xml中有,ArticleExtMapper使用ArticleExtMapper.xml中的定义

 

ResultMap覆盖

Mapper.xml继承机制只针对statement有效,对于sqlresultMap是无效的。
如果要在ArticleExtMapper.xml中覆盖这些,必须要先覆盖ParentMapper.xml中的statement,然后让这些statement使用新的sqlresultMap等。

 

总结

通过上面的将扩展的部分分开来写,后续改表,生成部分的代码直接覆盖即可,不担心扩展的不见了,二使用的时候直接使用扩展的mapper即可,扩展的mapper包含所有的方法。over ,Enjoy太开心

原创文章,作者:745907710,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/243659.html

(0)
上一篇 2022年4月11日 21:47
下一篇 2022年4月11日 21:47

相关推荐

发表回复

登录后才能评论