springboot sessionFactory 方式 配置 mybaits 扫描多个 mapper xml

static final String MAPPER_LOCATION1 = "classpath:mybatis/dsno1/*/*.xml";   //扫描 目录1
static final String MAPPER_LOCATION2 = "classpath:mybatis/dsno2/*/*.xml";	//扫描 目录2
@Bean(name = "masterSqlSessionFactory")
    @Primary //www.1b23.com
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(resolveMapperLocations());
        sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(MasterDataSourceConfig.CONFIG_LOCATION));
        sessionFactory.setTypeAliasesPackage(MasterDataSourceConfig.TYPE_ALIASES_PACKAGE);
        return sessionFactory.getObject();
    }
    
    /**加载多个mapper路径
     * @return
     */www.1b23.com
    public Resource[] resolveMapperLocations() {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        List<String> mapperLocations = new ArrayList<>();
        mapperLocations.add(MasterDataSourceConfig.MAPPER_LOCATION1);
        mapperLocations.add(MasterDataSourceConfig.MAPPER_LOCATION2);
        List<Resource> resources = new ArrayList<Resource>();
        if (mapperLocations != null) {
            for (String mapperLocation : mapperLocations) {
                try {
                    Resource[] mappers = resourceResolver.getResources(mapperLocation);
                    resources.addAll(Arrays.asList(mappers));
                } catch (IOException e) {
                }
            }
        }
        return resources.toArray(new Resource[resources.size()]);
    }
 

官网 http://www.fhadmin.org/ 

springboot sessionFactory 方式 配置 mybaits 扫描多个 mapper xml

相关推荐