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

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(4237)   2023-03-28 11:29:14

       本文主要讲解在使用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
标签: mybaties
地址:https://www.leftso.com/article/661.html

相关阅读

spring boot mybatis 整合使用讲解介绍,spring boot与mybaties的使用讲解介绍。spring boot mybatis xml mapper方式的入门和通过一个...
mybaties generator 表名/字段名下划线转驼峰,Spring Boot 2.0 整合 mybaties generator 表名/字段名下划线转驼峰 idea上的配置
引言    通过之前spring boot mybatis 整合的讲解: spring boot mybaties整合  (spring boot mybaties 整合 基于Java注解方式写...
spring boot框架整合mybaties数据库暂时选用MySQL
某些情况下,我们使用mybaties时需要使用IN(虽然IN数据多了效率不高,但是少量还是可以用得)条件查询,这时候我们就需要传递参数了,下面是mybaties处理IN条件得参数使用方法首先Ma...
       本文主要讲解在使用mybaties中通过mybaties generator生成基本操作代码,然后通过 mybaties mapper 继承机制来解决某些情况下经常改表导致改map...
mybaties like参数写法 and a.link like CONCAT(CONCAT('%', #{params.link}), '%')
idea Mybatis generator插件的配置和使用,在eclipse中,我们可以在eclipse的插件市场安装一个Mybatis generator的插件。然后对Mybatis gen...
mybatis plus 逻辑删除使用说明全局逻辑值配置,application.properties# 逻辑已删除值(默认为 1) mybatis-plus.global-config.db...