美文网首页编程笔记
sqlSessionFactoryBean.setMapperL

sqlSessionFactoryBean.setMapperL

作者: 光剑书架上的书 | 来源:发表于2018-05-09 18:30 被阅读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
        }
    
    }
    

    相关文章

      网友评论

        本文标题:sqlSessionFactoryBean.setMapperL

        本文链接:https://www.haomeiwen.com/subject/yfyorftx.html