编程笔记

sqlSessionFactoryBean.setMapperL

2018-05-09  本文已影响1409人  光剑书架上的书

sqlSessionFactoryBean.setMapperLocations 通配符 在配置代码中怎样使用

    @Bean(name = ["sqlSessionFactoryMicrotek"])
    @Primary
    @Throws(Exception::class)
    fun sqlSessionFactoryMicrotek(dataSourceMicrotek: DataSource,
                                  @Value("classpath:/mybatis/microtek/mybatis-config.xml")
                                  configLocation: Resource): SqlSessionFactory {
        val sqlSessionFactoryBean = SqlSessionFactoryBean()
        sqlSessionFactoryBean.setDataSource(dataSourceMicrotek)
        sqlSessionFactoryBean.setConfigLocation(configLocation)

        val resolver = PathMatchingResourcePatternResolver()
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mapperxml/microtek/*.xml"))
        return sqlSessionFactoryBean.`object`
    }

完整的代码实例如下:

package com.alibaba.microtek.datasource

import com.alibaba.boot.tddl.builder.datasource.TDataSourceBuilder
import org.apache.ibatis.session.SqlSessionFactory
import org.mybatis.spring.SqlSessionFactoryBean
import org.mybatis.spring.SqlSessionTemplate
import org.mybatis.spring.mapper.MapperScannerConfigurer
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.core.io.Resource
import javax.sql.DataSource
import org.springframework.core.io.support.PathMatchingResourcePatternResolver




@Configuration
class DatasourceConfigMicrotek {
    @Value("tddl.microtek.app")
    val appName: String? = null

    @Bean(name = ["dataSourceMicrotek"])
    @Primary
    fun dataSourceMicrotek(): DataSource {
        return TDataSourceBuilder.create().appName(appName).dynamicRule(true).sharding(false).build()
    }

    @Bean(name = ["sqlSessionFactoryMicrotek"])
    @Primary
    @Throws(Exception::class)
    fun sqlSessionFactoryMicrotek(dataSourceMicrotek: DataSource,
                                  @Value("classpath:/mybatis/microtek/mybatis-config.xml")
                                  configLocation: Resource): SqlSessionFactory {
        val sqlSessionFactoryBean = SqlSessionFactoryBean()
        sqlSessionFactoryBean.setDataSource(dataSourceMicrotek)
        sqlSessionFactoryBean.setConfigLocation(configLocation)

        val resolver = PathMatchingResourcePatternResolver()
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mapperxml/microtek/*.xml"))
        return sqlSessionFactoryBean.`object`
    }

    @Bean
    @Primary
    fun sqlSessionTemplateMicrotek(sqlSessionFactoryMicrotek: SqlSessionFactory): SqlSessionTemplate {
        return SqlSessionTemplate(sqlSessionFactoryMicrotek)
    }

    @Bean
    @Primary
    fun mapperScannerConfigurerMicrotek(): MapperScannerConfigurer {
        val mapperScannerConfigurer = MapperScannerConfigurer()
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryMicrotek")
        mapperScannerConfigurer.setBasePackage("com.alibaba.microtek.mapper.microtek")
        return mapperScannerConfigurer
    }

}
上一篇下一篇

猜你喜欢

热点阅读