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

来源:互联网 发布:淘宝售后客服工作时间 编辑:程序博客网 时间:2024/04/29 14:41

需要重写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();    }
0 0
原创粉丝点击