程序员

Mybatis在spring中的解析过程

2018-01-23  本文已影响0人  justlinzhihe
    @Bean
    public DataSource dataSource(@Value("${jdbc.driver}") String jdbcDriver, @Value("${jdbc.url}") String jdbcUrl, @Value("${jdbc.username}") String username, @Value("${jdbc.password}") String password) {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(jdbcDriver);
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource,Interceptor interceptor) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factoryBean.setDataSource(dataSource);
        factoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        factoryBean.setTypeAliasesPackage("shop.dev.entity");
        return factoryBean;
    }
    @Bean
    public SqlSession sqlSession(SqlSessionFactory sqlSessionFactory) {
        SqlSessionTemplate sqlSession = new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH);
        return sqlSession;
    }
    @Bean
    public MapperScannerConfigurer scannerConfigurer() {
        MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
        scannerConfigurer.setBasePackage("shop.dev.dao");
        scannerConfigurer.setSqlSessionTemplateBeanName("sqlSession");
        return scannerConfigurer;
    }

切入点在MapperScannerConfigurer

MapperScannerConfigurer.postProcessBeanDefinitionRegistry
ClassPathMapperScanner extends ClassPathBeanDefinitionScanner
ClassPathBeanDefinitionScanner.scan
ClassPathMapperScanner.doScan
ClassPathMapperScanner.processBeanDefinitions
MapperFactoryBean.getObject
从这里开始,接下去的就是mybatis的那一套东西了。
这些定义的beans最终都会通过第一张图里的registry
纳入spring的ioc管理。
上一篇下一篇

猜你喜欢

热点阅读