springboot与mybatis整合dao层不能注入的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28632173/article/details/58066586

需要重写VFS,并将其在mybatis整合类中指定为VFS的实现类

public class SpringBootVFS extends VFS {

    private final ResourcePatternResolver resourceResolver;

    public SpringBootVFS() {
        this.resourceResolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());
    }

    @Override
    public boolean isValid() {
        return true;
    }

    @Override
    protected List<String> list(URL url, String path) throws IOException {
        Resource[] resources = resourceResolver.getResources("classpath*:" + path + "/**/*.class");
        List<String> resourcePaths = new ArrayList<String>();
        for (Resource resource : resources) {
            resourcePaths.add(preserveSubpackageName(resource.getURI(), path));
        }
        return resourcePaths;
    }

    private static String preserveSubpackageName(final URI uri, final String rootPath) {
        final String uriStr = uri.toString();
        final int start = uriStr.indexOf(rootPath);
        return uriStr.substring(start);
    }

}

指定:

  /**
     * 根据数据源创建SqlSessionFactory
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        VFS.addImplClass(SpringBootVFS.class);//这句,其余是普通配置
        fb.setDataSource(dataSource);
//        fb.setTypeAliasesPackage(env.getProperty("mybatis.type-aliases-package"));//指定基包
        fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapper-locations")));//指定xml文件位置return fb.getObject();
        fb.setConfigLocation(new DefaultResourceLoader().getResource(env.getProperty("mybatis.config-location")));
        return fb.getObject();
    }

没有更多推荐了,返回首页