这篇文章以前面对SqlSessionFactoryBean的重构为基础,先简单回顾一下做了哪些操作:

  1. 新建SqlSessionFactoryBean,初始代码和mybatis-spring相同;
  2. 重构buildSqlSessionFactory()方法,将众多的if语句抽取为一组小方法,预留自定义配置的方法,同时添加常用属性的getter方法;
  3. 提取组件工厂接口,并提供组件创建工具类SqlSessionComponetFactorys,将散落在不同地方的new Xxx()集中管理,便于组件的替换。

现在来看怎么扩展,首先创建SchemaSqlSessionFactoryBean,继承重构后的SqlSessionFactoryBean,在XML配置中同步修改为新建的类:

public class SchemaSqlSessionFactoryBean extends SqlSessionFactoryBean {

}

对于一些简单的功能扩展,比如设置默认结果类型,扫描指定的类型简称,这里就不过多讨论了,这里集中讲述怎么扩展为使用XSD校验SqlMapper配置。

一、覆盖SqlSessionFactoryBean中的doParseSqlMapperResource()方法,这个方法的作用是解析一个SqlMapper配置文件

当然,为了兼容性,需要先判断是否为DTD,如果是DTD,按原方法解析,否则按自定义方法解析:

package org.dysd.dao.mybatis.schema;

import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.session.Configuration;
import org.springframework.core.NestedIOException;
import org.springframework.core.io.Resource;
import org.springframework.util.xml.XmlValidationModeDetector;

public class SchemaSqlSessionFactoryBean extends SqlSessionFactoryBean {

    @Override
    protected void doParseSqlMapperResource(Configuration configuration, Resource mapperLocation)
            throws NestedIOException {
        int mode = detectValidationMode(mapperLocation);
        if(mode == XmlValidationModeDetector.VALIDATION_DTD){//如果是DTD,使用Mybatis官方的解析
            super.doParseSqlMapperResource(configuration, mapperLocation);
        }else{
            try {
                // 使用Schema校验
                this.doParseSqlMapperResourceWithSchema(configuration, mapperLocation);
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }
        }
    }
    
    protected void doParseSqlMapperResourceWithSchema(Configuration configuration, Resource mapperLocation){
        
    }

    private int detectValidationMode(Resource mapperLocation) throws NestedIOException {
        int mode = -1;
        try {
            XmlValidationModeDetector detector = new XmlValidationModeDetector();
            mode = detector.detectValidationMode(mapperLocation.getInputStream());
        } catch (Exception e) {
            throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
        } finally {
            ErrorContext.instance().reset();
        }
        return mode;
    }
}
View Code

相关文章:

  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
  • 2022-02-28
  • 2021-12-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-24
  • 2022-12-23
相关资源
相似解决方案