美文网首页程序员
Mybatis在spring中的解析过程

Mybatis在spring中的解析过程

作者: justlinzhihe | 来源:发表于2018-01-23 20:05 被阅读0次
        @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管理。

    相关文章

      网友评论

        本文标题:Mybatis在spring中的解析过程

        本文链接:https://www.haomeiwen.com/subject/soleaxtx.html