mybatis與spring集成

  • xml配置

Mybatis的所有操作都是基于一個SqlSession的旨椒,而SqlSession是由SqlSessionFactory來產(chǎn)生的综慎,SqlSessionFactory又是由SqlSessionFactoryBuilder來生成的示惊。但是Mybatis-Spring是基于SqlSessionFactoryBean的愉镰。在使用Mybatis-Spring的時候,我們也需要SqlSession阔拳,而且這個SqlSession是內(nèi)嵌在程序中的类嗤,一般不需要我們直接訪問。SqlSession也是由SqlSessionFactory來產(chǎn)生的货裹,但是Mybatis-Spring給我們封裝了一個SqlSessionFactoryBean精偿,在這個bean里面還是通過SqlSessionFactoryBuilder來建立對應(yīng)的SqlSessionFactory,進(jìn)而獲取到對應(yīng)的SqlSession搔预。通過SqlSessionFactoryBean我們可以通過對其指定一些屬性來提供Mybatis的一些配置信息

通過MapperFactoryBean可以獲取到我們想要的Mapper對象拯田。MapperFactoryBean實現(xiàn)了Spring的FactoryBean接口甩十,所以MapperFactoryBean是通過FactoryBean接口中定義的getObject方法來獲取對應(yīng)的Mapper對象的吭产。在定義一個MapperFactoryBean的時候有兩個屬性需要我們注入臣淤,一個是Mybatis-Spring用來生成實現(xiàn)了SqlSession接口的SqlSessionTemplate對象的sqlSessionFactory窃爷;另一個就是我們所要返回的對應(yīng)的Mapper接口了。

定義好相應(yīng)Mapper接口對應(yīng)的MapperFactoryBean之后寺董,我們就可以把我們對應(yīng)的Mapper接口注入到由Spring管理的bean對象中了刻剥,比如Service bean對象。這樣當(dāng)我們需要使用到相應(yīng)的Mapper接口時御吞,MapperFactoryBean會從它的getObject方法中獲取對應(yīng)的Mapper接口陶珠,而getObject內(nèi)部還是通過我們注入的屬性調(diào)用SqlSession接口的getMapper(Mapper接口)方法來返回對應(yīng)的Mapper接口的享钞。這樣就通過把SqlSessionFactory和相應(yīng)的Mapper接口交給Spring管理實現(xiàn)了Mybatis跟Spring的整合

<bean id="sqlSessionFactory"   
    class="org.mybatis.spring.SqlSessionFactoryBean">  
    <property name="dataSource" ref="datasource"></property>  
    <property name="configLocation" value="classpath:context/mybatis-config.xml"></property>  
    <!-- 
    mapperLocations:通過正則表達(dá)式,支持mybatis動態(tài)掃描添加mapper不用像ibatis暑脆,用一個還要蛋疼滴添加一個include
    -->
    <property name="mapperLocations" value="classpath*:/com/tx/demo/**/*SqlMap.xml" />
    <!-- 
    typeHandlersPackage: 由于mybatis默認(rèn)入?yún)⑷绻麨榭仗砺穑譀]有指定jdbcType時會拋出異常份名,在這里通過配置一些默認(rèn)的類型空值插入的handle,以便處理mybatis的默認(rèn)類型為空的情況鲤孵。
    例如NullAbleStringTypeHandle通過實現(xiàn)當(dāng)String字符串中為null是調(diào)用ps.setString(i,null)其他常用類型雷同辰如。
    -->  
    <property name="typeHandlersPackage" value="com.tx.core.mybatis.handler"></property>
    <!-- 
    failFast:開啟后將在啟動時檢查設(shè)定的parameterMap,resultMap是否存在,是否合法鹰椒。個人建議設(shè)置為true,這樣可以盡快定位解決問題漆际。不然在調(diào)用過程中發(fā)現(xiàn)錯誤夺饲,會影響問題定位。
    --> 
    <property name="failFast" value="true"></property>  
    <property name="plugins">  
        <array>  
            <bean class="com.tx.core.mybatis.interceptor.PagedDiclectStatementHandlerInterceptor">  
                <property name="dialect">  
                    <bean class="org.hibernate.dialect.PostgreSQLDialect"></bean>  
                </property>  
            </bean>  
        </array>  
    </property>  
</bean>  
<!--
myBatisExceptionTranslator:用以支持spring的異常轉(zhuǎn)換擂找,通過配置該translator可以將mybatis異常轉(zhuǎn)換為spring中定義的DataAccessException贯涎。
-->
<bean id="myBatisExceptionTranslator" class="org.mybatis.spring.MyBatisExceptionTranslator">  
    <property name="dataSource">  
        <ref bean="datasource"></ref>  
    </property>  
</bean>  
  
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">  
    <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>  
    <constructor-arg name="executorType" ref="SIMPLE"></constructor-arg>  
    <constructor-arg name="exceptionTranslator" ref="myBatisExceptionTranslator"></constructor-arg>  
</bean>  
  
<bean id="myBatisDaoSupport" class="com.tx.core.mybatis.support.MyBatisDaoSupport">  
    <property name="sqlSessionTemplate">  
        <ref bean="sqlSessionTemplate"/>  
    </property>  
</bean>
  • 自動掃描配置
<!-- 采用自動掃描方式創(chuàng)建mapper bean(單個更新模式) -->  
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    <property name="basePackage" value="com.xxx.dao" />  
    <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateSimple" />  
    <property name="markerInterface" value="com.xxx.dao.SimpleDao" />  
</bean>  
       
<!-- 采用自動掃描方式創(chuàng)建mapper bean(批量更新模式) -->  
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    <property name="basePackage" value="com.xxx.dao" />  
    <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateBatch" />  
    <property name="markerInterface" value="com.xxx.dao.BatchDao" />  
</bean>
  • mapperScannerConfigurer解析
    • 實現(xiàn)BeanDefinitionRegistryPostProcessor接口塘雳,實現(xiàn)
 MapperScannerConfigurer implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware
    • 重寫postProcessBeanDefinitionRegistry方法 通過ClassPathMapperScanner掃描包
  @Override
  public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
    if (this.processPropertyPlaceHolders) {
      processPropertyPlaceHolders();
    }

    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
    scanner.setAddToConfig(this.addToConfig);
    scanner.setAnnotationClass(this.annotationClass);
    scanner.setMarkerInterface(this.markerInterface);
    scanner.setSqlSessionFactory(this.sqlSessionFactory);
    scanner.setSqlSessionTemplate(this.sqlSessionTemplate);
    scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);
    scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);
    scanner.setResourceLoader(this.applicationContext);
    scanner.setBeanNameGenerator(this.nameGenerator);
    //過濾  可以通過接口過濾  或者注解過濾
    scanner.registerFilters();
    scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
  }
  @Override
  public Set<BeanDefinitionHolder> doScan(String... basePackages) {
    //掃描包败明,注冊beandefinition
    Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);

    if (beanDefinitions.isEmpty()) {
      logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration.");
    } else {
      processBeanDefinitions(beanDefinitions);
    }

    return beanDefinitions;
  }
  private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
    GenericBeanDefinition definition;
    for (BeanDefinitionHolder holder : beanDefinitions) {
      definition = (GenericBeanDefinition) holder.getBeanDefinition();

      if (logger.isDebugEnabled()) {
        logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() 
          + "' and '" + definition.getBeanClassName() + "' mapperInterface");
      }

      
      //  MapperFactoryBean 實現(xiàn)factoryBean接口
      definition.getPropertyValues().add("mapperInterface", definition.getBeanClassName());
      //設(shè)置bean的class類型
      definition.setBeanClass(this.mapperFactoryBean.getClass());

      definition.getPropertyValues().add("addToConfig", this.addToConfig);

      boolean explicitFactoryUsed = false;
      if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {
        definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));
        explicitFactoryUsed = true;
      } else if (this.sqlSessionFactory != null) {
        definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory);
        explicitFactoryUsed = true;
      }

      if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {
        if (explicitFactoryUsed) {
          logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
        }
        definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName));
        explicitFactoryUsed = true;
      } else if (this.sqlSessionTemplate != null) {
        if (explicitFactoryUsed) {
          logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
        }
        definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate);
        explicitFactoryUsed = true;
      }

      if (!explicitFactoryUsed) {
        if (logger.isDebugEnabled()) {
          logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName() + "'.");
        }
        definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
      }
    }
  }
//通過spring調(diào)用此方法,注冊mapper實例
 public T getObject() throws Exception {
        return this.getSqlSession().getMapper(this.mapperInterface);
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末讳嘱,一起剝皮案震驚了整個濱河市呢燥,隨后出現(xiàn)的幾起案子寓娩,更是在濱河造成了極大的恐慌辆脸,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件焊夸,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)使鹅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門昌抠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人裁厅,你說我怎么就攤上這事侨艾。” “怎么了袋励?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵姻成,是天一觀的道長。 經(jīng)常有香客問我均牢,道長才睹,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任垮庐,我火速辦了婚禮坞琴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘寒亥。我一直安慰自己,他們只是感情好溉奕,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布加勤。 她就那樣靜靜地躺著,像睡著了一般叠国。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上煎饼,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天,我揣著相機(jī)與錄音筒溃,去河邊找鬼。 笑死怜奖,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的迁央。 我是一名探鬼主播滥崩,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蜂科!你這毒婦竟也來了短条?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤贡定,失蹤者是張志新(化名)和其女友劉穎可都,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體汹粤,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡嘱兼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了汇四。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡序宦,死狀恐怖背苦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情秕噪,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布腌巾,位于F島的核電站澈蝙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏灯荧。R本人自食惡果不足惜盐杂,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望撕贞。 院中可真熱鬧,春花似錦捏膨、人聲如沸食侮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽眉尸。三九已至巨双,卻和暖如春霉祸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背丝蹭。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留镜沽,地道東北人贱田。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親统阿。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容