Mybatis Mapper.xml继承机制

Mapper.xml继承机制

github地址

Mybatis实际上隐藏了一个功能:Mapper.xml可以继承,这个在官方文档中并没有提到过,不过在这个issue (commit)里提到过。

Statement覆盖

利用Mapper.xml的继承机制,我们可以做到ChildMapper覆盖ParentMapper中selectinsertdeleteupdate。下面举例说明:

Interface:

@MybatisMapper
public interface ParentMapper {

  String selectFoo();

  String selectBar();
}

@MybatisMapper
public interface ChildMapper extends ParentMapper {

  String selectLoo();

}

Mapper.xml:

<mapper namespace="me.chanjar.mybatislearn.inherit.statement.ParentMapper">

  <select id="selectFoo" resultType="String">
    SELECT 'Foo From Parent'
    FROM dual
  </select>

  <select id="selectBar" resultType="String">
    SELECT 'Bar From Parent'
    FROM dual
  </select>

</mapper>

<mapper namespace="me.chanjar.mybatislearn.inherit.statement.ChildMapper">

  <!-- 覆盖 Parent.selectFoo的定义 -->
  <select id="selectFoo" resultType="String">
    SELECT 'Foo From Child'
    FROM dual
  </select>

  <!-- 不覆盖 Parent.selectBar的定义 -->

  <!-- 自己的 Child.selectLoo的定义 -->
  <select id="selectLoo" resultType="String">
    SELECT 'Loo From Child'
    FROM dual
  </select>

</mapper>

规律可以总结为:

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

相关代码:Java代码测试代码配置文件

ResultMap覆盖

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

下面举例一个给ITEM表添加字段,但是不修改原来的ItemMapper的例子:

Model:

public class Item {

  private Integer id;
  private String title;
  // setter and getter ...
}

public class ItemEx extends Item {

  private String name;
  // setter and getter ...

}

Interface:

@MybatisMapper
public interface ItemMapper {

  Item getById(@Param("id") Long id);

}
@MybatisMapper
public interface ItemExMapper extends ItemMapper {

}

Mapper.xml:

<mapper namespace="me.chanjar.mybatislearn.inherit.resultmap.ItemMapper">

  <select id="getById" resultMap="Item">
    select
      *
    from item where id = #{id}
  </select>

  <resultMap id="Item" type="me.chanjar.mybatislearn.inherit.resultmap.Item" autoMapping="true">
    <id property="id" column="id" />
  </resultMap>

</mapper>

<mapper namespace="me.chanjar.mybatislearn.inherit.resultmap.ItemExMapper">

  <!-- 如果这里不写一遍,就会用到ItemMapper.getById的定义,resultMap就不会是ItemEx-->
  <select id="getById" resultMap="Item">
    select
    *
    from item where id = #{id}
  </select>

  <resultMap id="Item" type="me.chanjar.mybatislearn.inherit.resultmap.ItemEx" autoMapping="true">
    <id property="id" column="id" />
  </resultMap>

</mapper>

相关代码:Java代码测试代码配置文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 整合 MyBatis-Plus 的步骤如下: 1.添加 MyBatis-Plus 和 MyBatis-Spring-Boot-Starter 依赖。在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency> ``` 2.添加数据库配置。在 application.yml 文件中添加以下配置: ``` spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false username: root password: root ``` 3.创建 Mapper 接口。在 mapper 接口上添加 @Mapper 注解,并且继承 BaseMapper 接口,例如: ``` @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 4.创建 Mapper XML 文件。在 resources 目录下创建 mapper 目录,并在该目录下创建与 Mapper 接口同名的 XML 文件,例如:UserMapper.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="com.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> <result column="email" property="email" /> </resultMap> <select id="selectById" resultMap="BaseResultMap"> select * from user where id=#{id} </select> </mapper> ``` 5.在 Service 中调用 Mapper 接口。例如: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectById(id); } } ``` 这样,Spring Boot 就成功整合了 MyBatis-Plus 和 Mapper XML 文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值