最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解如何繼承Mybatis中Mapper.xml文件

 更新時(shí)間:2022年09月30日 09:50:19   作者:石臻臻的雜貨鋪  
這篇文章主要為大家介紹了詳解如何繼承Mybatis中Mapper.xml文件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

引言

最近在寫一個(gè) Mybatis 代碼自動(dòng)生成插件,用的是Mybatis來(lái)擴(kuò)展,其中有一個(gè)需求就是 生成javaMapper文件和 xmlMapper文件的時(shí)候 希望另外生成一個(gè)擴(kuò)展類和擴(kuò)展xml文件。原文件不修改,只存放一些基本的信息,開(kāi)發(fā)過(guò)程中只修改擴(kuò)展的Ext文件 形式如下: SrcTestMapper.java

修改擴(kuò)展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;
/**
* SrcTestMapperExt接口
* Created by shirenchuang on 2018/6/30.
*/
@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" 這個(gè)Map并沒(méi)有再這個(gè)xml中定義,這樣能使用嗎?

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

那么SrcTestMapperExt.xml是如何繼承SrcTestMapper.xml中的定義的呢?

修改命名空間

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

光這樣還不夠,因?yàn)檫@個(gè)時(shí)候你去運(yùn)行的時(shí)候會(huì)報(bào)錯(cuò)

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

因?yàn)镸ybatis中是必須要 xml的文件包名和文件名必須跟 Mapper.java對(duì)應(yīng)起來(lái)的 比如com.test.dao.mapper.srctest.SrcTestMapper.java這個(gè)相對(duì)應(yīng)的是 com.test.dao.mapper.srctest.SrcTestMapper.xml 必須是這樣子,沒(méi)有例外,否則就會(huì)報(bào)錯(cuò) 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;
  }

這個(gè)this.currentNamespace 和參數(shù)傳進(jìn)來(lái)的currentNamespace比較是否相等;

參數(shù)傳進(jìn)來(lái)的currentNamespace就是我們xml中的 <mapper namespace="com.test.dao.mapper.srctest.SrcTestMapperExt">值;

this.currentNamespace 設(shè)置

然后this.currentNamespace是從哪里設(shè)置的呢?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() ;

這個(gè)type的最終來(lái)源是 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);這行應(yīng)該就明白了 加載mapperInterface的時(shí)候會(huì)跟相應(yīng)的xml映射,并且會(huì)去檢驗(yàn)namespace是否跟mapperInterface相等!

那么既然命名空間不能修改,那第一條不白說(shuō)了?還怎么實(shí)現(xiàn)Mapper.xml的繼承??? 別慌,既然是這樣子,那我們可以讓 MapperInterface 中的SrcTestMapper.java別被加載進(jìn)來(lái)就行了?。?! 只加載 MapperExt.java不就行了?

修改applicationContext.xml,讓Mapper.java不被掃描

Mapper.java接口掃描配置

    <!-- Mapper接口所在包名,Spring會(huì)自動(dòng)查找其下的Mapper -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.dao.mapper"/>
<!--
        該屬性實(shí)際上就是起到一個(gè)過(guò)濾的作用,如果設(shè)置了該屬性,那么MyBatis的接口只有包含該注解,才會(huì)被掃描進(jìn)去。
-->
        <property name="annotationClass" value="javax.annotation.Resource"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

basePackage 把Mapper.java掃描進(jìn)去沒(méi)有關(guān)系,重點(diǎn)是 <property name="annotationClass" value="javax.annotation.Resource"/> 這樣 MapperScanner會(huì)把沒(méi)有配置注解的過(guò)濾掉; 回頭看我們的MapperExt.java配置文件是有加上注解的

/**
* SrcTestMapperExt接口
* Created by shirenchuang on 2018/6/30.
*/
@Resource
public interface SrcTestMapperExt extends SrcTestMapper {
    List<SrcTest> selectExtTest(@Param("age") int  age);
}

這樣子之后,基本上問(wèn)題就解決了,還有一個(gè)地方特別要注意一下的是.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>

這樣配置沒(méi)有錯(cuò),但是我之前的配置寫成了 <property name="mapperLocations" value="classpath:com/test/dao/mapper/**/*Mapper.xml"/>

這樣子 MapperExt.xml 沒(méi)有被掃描進(jìn)去,在我執(zhí)行單元測(cè)試的時(shí)候

  @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

但是執(zhí)行 ````srcTestService.insertSelective(srcTest);不會(huì)出錯(cuò) 原因就是 insertSelective是在SrcTestMapper.xml中存在 ,已經(jīng)被注冊(cè)到 com.test.dao.mapper.srctest.SrcTestMapperExt```命名空間了,但是selectExtTest由于沒(méi)有被注冊(cè),所以報(bào)錯(cuò)了;

有興趣可以下載閱讀或者直接使用我整合的 自動(dòng)生成擴(kuò)展插件

以上就是詳解如何繼承Mybatis中Mapper.xml文件的詳細(xì)內(nèi)容,更多關(guān)于Mybatis Mapper.xml文件繼承的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 解決Java中的強(qiáng)制類型轉(zhuǎn)換和二進(jìn)制表示問(wèn)題

    解決Java中的強(qiáng)制類型轉(zhuǎn)換和二進(jìn)制表示問(wèn)題

    這篇文章主要介紹了解決Java中的強(qiáng)制類型轉(zhuǎn)換和二進(jìn)制表示問(wèn)題,需要的朋友可以參考下
    2019-05-05
  • springboot項(xiàng)目配置logback日志系統(tǒng)的實(shí)現(xiàn)

    springboot項(xiàng)目配置logback日志系統(tǒng)的實(shí)現(xiàn)

    這篇文章主要介紹了springboot項(xiàng)目配置logback日志系統(tǒng)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • java 中String.equals和==的比較

    java 中String.equals和==的比較

    這篇文章主要介紹了java 中String.equals和==的比較的相關(guān)資料,需要的朋友可以參考下
    2017-08-08
  • java中自帶有并發(fā)屬性的List總結(jié)

    java中自帶有并發(fā)屬性的List總結(jié)

    java中有很多l(xiāng)ist,但是原生支持并發(fā)的并不多,那么java中的并發(fā)list到底有哪些呢?下面小編就來(lái)給大家介紹一下ArrayList、CopyOnWriteArrayList、ConcurrentLinkedDeque這幾個(gè)吧
    2023-09-09
  • SpringBoot接值實(shí)現(xiàn)方法詳解

    SpringBoot接值實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了SpringBoot接值實(shí)現(xiàn)方法,SpringBoot接值是指在SpringBoot應(yīng)用程序中接收請(qǐng)求參數(shù),從HTTP請(qǐng)求中獲取參數(shù),并將其綁定到Java對(duì)象中進(jìn)行處理的過(guò)程,感興趣想要詳細(xì)了解可以參考下文
    2023-05-05
  • 教你用java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)(附詳細(xì)代碼)

    教你用java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)(附詳細(xì)代碼)

    教學(xué)管理系統(tǒng)很適合初學(xué)者對(duì)于所學(xué)語(yǔ)言的練習(xí),下面這篇文章主要給大家介紹了關(guān)于如何用java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)的相關(guān)資料,文中給出了詳細(xì)的實(shí)例代碼,需要的朋友可以參考下
    2023-06-06
  • Java實(shí)現(xiàn)簡(jiǎn)單文件過(guò)濾器功能

    Java實(shí)現(xiàn)簡(jiǎn)單文件過(guò)濾器功能

    下面小編就為大家分享一篇Java實(shí)現(xiàn)簡(jiǎn)單文件過(guò)濾器功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Springboot的@Cacheable注解使用方法

    Springboot的@Cacheable注解使用方法

    @Cacheable 是 Spring 框架提供的一種基于緩存的注解,它可以被應(yīng)用在方法上以指示該方法的結(jié)果需要被緩存起來(lái),緩存在哪個(gè) Cache 中以及該方法使用何種緩存鍵,以下代碼展示了如何使用 @Cacheable 注解,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • 利用Jackson實(shí)現(xiàn)數(shù)據(jù)脫敏的示例詳解

    利用Jackson實(shí)現(xiàn)數(shù)據(jù)脫敏的示例詳解

    在我們的企業(yè)項(xiàng)目中,為了保護(hù)用戶隱私,數(shù)據(jù)脫敏成了必不可少的操作,那么我們?cè)趺磧?yōu)雅的利用Jackson實(shí)現(xiàn)數(shù)據(jù)脫敏呢,本文就來(lái)和大家詳細(xì)聊聊,希望對(duì)大家有所幫助
    2023-05-05
  • 簡(jiǎn)單工廠模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    簡(jiǎn)單工廠模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了簡(jiǎn)單工廠模式的相關(guān)資料,和大家一起學(xué)習(xí)靜態(tài)工廠方法模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評(píng)論

久治县| 嫩江县| 常德市| 芦山县| 西充县| 浮梁县| 荆门市| 都江堰市| 望谟县| 巴彦县| 思茅市| 临澧县| 花莲县| 铜川市| 略阳县| 兴城市| 武安市| 孝昌县| 瑞丽市| 澎湖县| 沾益县| 双城市| 丹江口市| 乐至县| 延长县| 滦南县| 西藏| 东乌| 巴南区| 永康市| 哈尔滨市| 都江堰市| 澄城县| 绩溪县| 长子县| 百色市| 象山县| 广宁县| 连州市| 十堰市| 海晏县|