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

自闭症网瘾萝莉.ら 提交于 2020-08-16 04:03:27
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()]);
    }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!