广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解如何继承Mybatis中Mapper.xml文件
  • 730
分享到

详解如何继承Mybatis中Mapper.xml文件

2024-04-02 19:04:59 730人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录引言修改扩展Ext文件修改命名空间this.currentNamespace 设置修改applicationContext.xml,让Mapper.java不被扫描引言 最近在写

引言

最近在写一个 mybatis 代码自动生成插件,用的是Mybatis来扩展,其中有一个需求就是 生成javaMapper文件和 xmlMapper文件的时候 希望另外生成一个扩展类和扩展xml文件。原文件不修改,只存放一些基本的信息,开发过程中只修改扩展的Ext文件 形式如下: SrcTestMapper.java

修改扩展Ext文件

package com.test.dao.mapper.srctest;
import com.test.dao.model.srctest.SrcTest;
import com.test.dao.model.srctest.SrcTestExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface SrcTestMapper {
    long countByExample(SrcTestExample example);
    int deleteByExample(SrcTestExample example);
    int deleteByPrimaryKey(Integer id);
    int insert(SrcTest record);
    int insertSelective(SrcTest record);
    List<SrcTest> selectByExample(SrcTestExample example);
    SrcTest selectByPrimaryKey(Integer id);
    int updateByExampleSelective(@Param("record") SrcTest record, @Param("example") SrcTestExample example);
    int updateByExample(@Param("record") SrcTest record, @Param("example") SrcTestExample example);
    int updateByPrimaryKeySelective(SrcTest record);
    int updateByPrimaryKey(SrcTest record);
}

SrcTestMapperExt.java

package com.test.dao.mapper.srctest;
import com.test.dao.model.srctest.SrcTest;
import org.apache.ibatis.annotations.Param;
import javax.annotation.Resource;
import java.util.List;

@Resource
public interface SrcTestMapperExt extends SrcTestMapper {
    List<SrcTest> selectExtTest(@Param("age") int  age);
}

SrcTestMapper.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.test.dao.mapper.srctest.SrcTestMapperExt">
  <resultMap id="BaseResultMap" type="com.test.dao.model.srctest.SrcTest">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="age" jdbcType="INTEGER" property="age" />
    <result column="ctime" jdbcType="BIGINT" property="ctime" />
  </resultMap>
<!-- 省略....-->
</mapper>

SrcTestMapperExt.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.test.dao.mapper.srctest.SrcTestMapperExt">
    <select id="selectExtTest" resultMap="BaseResultMap">
        select * from src_test where age>#{age}
    </select>
</mapper>

注意:这里返回的resultMap="BaseResultMap" 这个Map并没有再这个xml中定义,这样能使用吗?

上面是我生成的代码;并且能够正常使用;

那么SrcTestMapperExt.xml是如何继承SrcTestMapper.xml中的定义的呢?

修改命名空间

使他们的命名空间相同,namespace="com.test.dao.mapper.srctest.SrcTestMapperExt"

光这样还不够,因为这个时候你去运行的时候会报错

Caused by: org.apache.ibatis.builder.BuilderException: Wrong namespace. Expected 'com.test.dao.mapper.srctest.SrcTestMapper' but found 'com.test.dao.mapper.srctest.SrcTestMapperExt'.

因为Mybatis中是必须要 xml的文件包名和文件名必须跟 Mapper.java对应起来的 比如com.test.dao.mapper.srctest.SrcTestMapper.java这个相对应的是 com.test.dao.mapper.srctest.SrcTestMapper.xml 必须是这样子,没有例外,否则就会报错 show the code MapperBuilderAssistant

  public void setCurrentNamespace(String currentNamespace) {
    if (currentNamespace == null) {
      throw new BuilderException("The mapper element requires a namespace attribute to be specified.");
    }
    if (this.currentNamespace != null && !this.currentNamespace.equals(currentNamespace)) {
      throw new BuilderException("Wrong namespace. Expected '"
          + this.currentNamespace + "' but found '" + currentNamespace + "'.");
    }
    this.currentNamespace = currentNamespace;
  }

这个this.currentNamespace 和参数传进来的currentNamespace比较是否相等;

参数传进来的currentNamespace就是我们xml中的 <mapper namespace="com.test.dao.mapper.srctest.SrcTestMapperExt">值;

this.currentNamespace 设置

然后this.currentNamespace是从哪里设置的呢?this.currentNamespace = currentNamespace;

跟下代码:MapperAnnotationBuilder

  public void parse() {
    String resource = type.toString();
    if (!configuration.isResourceLoaded(resource)) {
      loadXmlResource();
      configuration.addLoadedResource(resource);
      assistant.setCurrentNamespace(type.getName());
      parseCache();
      parseCacheRef();
      Method[] methods = type.getMethods();
      for (Method method : methods) {
        try {
          // issue #237
          if (!method.isBridge()) {
            parseStatement(method);
          }
        } catch (IncompleteElementException e) {
          configuration.addIncompleteMethod(new MethodResolver(this, method));
        }
      }
    }
    parsePendingMethods();
  }

看到 assistant.setCurrentNamespace(type.getName()); 它获取的是 type.getName() ;

这个type的最终来源是 MapperFactoryBean

  @Override
  protected void checkDaoConfig() {
    super.checkDaoConfig();
    notNull(this.mapperInterface, "Property 'mapperInterface' is required");
    Configuration configuration = getsqlSession().getConfiguration();
    if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) {
      try {
        configuration.addMapper(this.mapperInterface);
      } catch (Exception e) {
        logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e);
        throw new IllegalArgumentException(e);
      } finally {
        ErrorContext.instance().reset();
      }
    }
  }

configuration.addMapper(this.mapperInterface);这行应该就明白了 加载mapperInterface的时候会跟相应的xml映射,并且会去检验namespace是否跟mapperInterface相等!

那么既然命名空间不能修改,那第一条不白说了?还怎么实现Mapper.xml的继承啊? 别慌,既然是这样子,那我们可以让 MapperInterface 中的SrcTestMapper.java别被加载进来就行了啊!! 只加载 MapperExt.java不就行了?

修改applicationContext.xml,让Mapper.java不被扫描

Mapper.java接口扫描配置

    <!-- Mapper接口所在包名,spring会自动查找其下的Mapper -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.dao.mapper"/>
<!--
        该属性实际上就是起到一个过滤的作用,如果设置了该属性,那么MyBatis的接口只有包含该注解,才会被扫描进去。
-->
        <property name="annotationClass" value="javax.annotation.Resource"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

basePackage 把Mapper.java扫描进去没有关系,重点是 <property name="annotationClass" value="javax.annotation.Resource"/> 这样 MapperScanner会把没有配置注解的过滤掉; 回头看我们的MapperExt.java配置文件是有加上注解的


@Resource
public interface SrcTestMapperExt extends SrcTestMapper {
    List<SrcTest> selectExtTest(@Param("age") int  age);
}

这样子之后,基本上问题就解决了,还有一个地方特别要注意一下的是.xml文件的配置

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 必须将mapper,和mapperExt也一起扫描-->
        <property name="mapperLocations" value="classpath:com/test/dao/mapper*.xml"/>
    </bean>

这样配置没有错,但是我之前的配置写成了 <property name="mapperLocations" value="classpath:com/test/dao/mapper*Mapper.xml"/>

这样子 MapperExt.xml 没有被扫描进去,在我执行单元测试的时候

  @Test
    public void selectExt(){
        List<SrcTest> tests = srcTestService.selectExtTest(9);
        System.out.println(tests.toString());
    }

err_console

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.test.dao.mapper.srctest.SrcTestMapperExt.selectExtTest

但是执行 ````srcTestService.insertSelective(srcTest);不会出错 原因就是 insertSelective是在SrcTestMapper.xml中存在 ,已经被注册到 com.test.dao.mapper.srctest.SrcTestMapperExt```命名空间了,但是selectExtTest由于没有被注册,所以报错了;

有兴趣可以下载阅读或者直接使用我整合的 自动生成扩展插件

以上就是详解如何继承Mybatis中Mapper.xml文件的详细内容,更多关于Mybatis Mapper.xml文件继承的资料请关注编程网其它相关文章!

--结束END--

本文标题: 详解如何继承Mybatis中Mapper.xml文件

本文链接: https://www.lsjlt.com/news/168754.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 详解如何继承Mybatis中Mapper.xml文件
    目录引言修改扩展Ext文件修改命名空间this.currentNamespace 设置修改applicationContext.xml,让Mapper.java不被扫描引言 最近在写...
    99+
    2022-11-13
  • Java中继承图文详解
    java继承与合成基本概念继承:可以基于已经存在的类构造一个新类。继承已经存在的类就可以复用这些类的方法和域。在此基础上,可以添加新的方法和域,从而扩充了类的功能。合成:在新类里创建原有的对象称为合成。这种方式可以重复利用现有的代码而不更改...
    99+
    2019-09-16
    Java
  • 解决Mybatis映射文件mapper.xml中的注释问题
    目录Mybatis映射文件mapper.xml的注释问题报错信息解决办法mapper.xml文件中的注释注释方式‘无效的列索引’bug和解决小结一下Mybat...
    99+
    2022-11-12
  • mybatis中mapper.xml文件的常用属性及标签讲解
    目录${}和#{}的区别常见的属性常见标签< sql >标签< where >和< if >标签< set >标签< ...
    99+
    2022-11-12
  • 在C++中如何阻止类被继承详解
    这个话题是源自于一个面试题,我在网上查了一下有不少这方面的解说!我自己整理了一下,选择了一个自认为是最优方案! 我们从最简单的开始: 首先,大家都知道要阻止类被实例化,可以通过使...
    99+
    2022-11-12
  • 一文详解如何用原型链的方式实现JS继承
    目录原型链是什么通过构造函数创建实例对象用原型链的方式实现继承方法1:Object.create方法2:直接修改 [[prototype]]方法3:使用父类的实例总结今天讲一道经典的...
    99+
    2022-11-13
  • 一篇文章带你详细理解java中类的继承与多态
    类的继承子类及其定义子类的定义使用关键字extends格式:class SubClass extends SuperClass{ ........ }子类可以继承父类的属性和方法;子类不能继承带private修饰符的属性,方法;子类不能继承...
    99+
    2017-08-28
    java入门 java 继承 多态
  • 如何正确的理解Java中的继承
    本篇文章为大家展示了如何正确的理解Java中的继承,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Java作为一面向对象的语言,具备面向对象的三大特征——继承,多态,封装。继承顾名思义,继任,承接,传...
    99+
    2023-05-31
    java ava
  • PHP中如何用Trait解决单继承问题
    这篇文章主要讲解了“PHP中如何用Trait解决单继承问题”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PHP中如何用Trait解决单继承问题”吧!我们都知道PHP是单继承语言,但是有些时候...
    99+
    2023-06-29
  • MyBatis映射文件中parameterType与resultType的用法详解
    目录resultMap resultTypeparameterType总结resultMap  表示查询结果集与java对象之间的一种关系,处理查询结果集,映射到...
    99+
    2023-05-15
    parametertype和resulttype parametertype和resulttype的区别
  • Net5 WorkService继承Quarzt及Net5如何处理文件上传功能
    这篇文章主要为大家展示了“Net5 WorkService继承Quarzt及Net5如何处理文件上传功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Net5 WorkSer...
    99+
    2023-06-29
  • 如何从一个组件的实现来深刻理解JS中的继承
    今天就跟大家聊聊有关如何从一个组件的实现来深刻理解JS中的继承,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。其实,无论是写什么语言的程序员,最终的目...
    99+
    2022-10-19
  • Mybatis是如何解析配置文件的
    本篇内容主要讲解“Mybatis是如何解析配置文件的”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Mybatis是如何解析配置文件的”吧!在以前文章中,我们把M...
    99+
    2022-10-19
  • 详解Flutter如何读写文本文件
    目录介绍示例 1:加载内容预览完整代码示例 2: Reading and Writing获取文件路径示例预览完整的代码和解释介绍 文本文件(具有 .txt扩展名)广泛用于持久存储信息...
    99+
    2022-11-13
  • 一文详解js如何用文件流下载csv文件
    本篇文章给大家带来了关于js+csv的相关知识,其中主要介绍了什么是Blob对象,怎么理解它以及如何使用文件流实现下载csv文件,感兴趣的朋友,下面一起来看一下吧,希望对大家有帮助。js使用文件流下载csv文件的实现方法理解 Blob 对象...
    99+
    2023-05-14
    javascript csv
  • 在MyBatis中如何将Mapper文件引用到resultMap中
    今天就跟大家聊聊有关在MyBatis中如何将Mapper文件引用到resultMap中,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。ClassesMapper.xml:<res...
    99+
    2023-05-31
    mybatis resultmap mapper
  • 详解如何在Java中加密和解密zip文件
    目录依赖压缩一个文件压缩多个文件压缩一个目录创建一个分割的压缩文件提取所有文件提取单个文件总结依赖 让我们先把 zip4j 依赖关系添加到我们的 pom...
    99+
    2022-11-13
  • 详解如何取消Git文件夹
    在软件开发中,版本控制是一项十分重要的工作。而Git作为目前最流行的版本控制工具,可以帮助我们轻松地追踪代码的变化,并且在多人协作时能够保证代码的一致性。但是有时候我们会遇到一些问题,比如Git文件夹的大小会不断增加,占用磁盘空间,影响机器...
    99+
    2023-10-22
  • 详解PHP如何读取大文件
    目录衡量成功我们有什么选择?逐行读取文件文件之间的管道其他流过滤器自定义流创建自定义协议和过滤器总结衡量成功 唯一能确认我们对代码所做改进是否有效的方式是:衡量一个糟糕的情况,然后对...
    99+
    2022-11-12
  • golang如何操作csv文件详解
    目录练习要求:考察点:编码:README.md附:使用Golang导出CSV数据并解决数据乱码问题CSV 格式实现方式golang实现csv数据写文件golang实现web导出csv...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作