美文网首页springboot
整合springboot + mybatis

整合springboot + mybatis

作者: 毛不翼 | 来源:发表于2019-09-26 09:56 被阅读0次

添加依赖

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>

Mybatis 配置

@Configuration
@MapperScan(basePackages = "com.example.dao.mapper",sqlSessionFactoryRef = "sqlSessionFactoryBean")
public class MybatisConfig {

    @Value("classpath:mapper/**/*.xml")
    String mapperLocations;

//    @Value("classpath:mybatis-config.xml")
//    String configLocation;

//  使用配置文件还是用配置注入,根据个人喜好决定,个人觉得用配置注入会更方便维护,更简单
    @Bean
    org.apache.ibatis.session.Configuration configuration(){
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setCacheEnabled(true);
        configuration.setLazyLoadingEnabled(true);
        configuration.setAggressiveLazyLoading(false);
        configuration.setMultipleResultSetsEnabled(true);
        configuration.setUseColumnLabel(true);
        configuration.setUseGeneratedKeys(true);
        configuration.setAutoMappingBehavior(AutoMappingBehavior.FULL);
        configuration.setDefaultExecutorType(ExecutorType.SIMPLE);
        configuration.setDefaultStatementTimeout(25000);
        return configuration;
    }

    @Bean
    PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialect("mysql");
        paginationInterceptor.setPageSqlId(".*Page");
        return paginationInterceptor;
    }

    @Bean
    TableShardInterceptor tableShardInterceptor(){
        return new TableShardInterceptor();
    }

    @Bean
    SqlSessionFactoryBean sqlSessionFactoryBean(DataSource masterDataSource,org.apache.ibatis.session.Configuration configuration,Interceptor[] interceptors){
        try {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(masterDataSource);
            PathMatchingResourcePatternResolver resourcePatternResolver  = new PathMatchingResourcePatternResolver();
            sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources(mapperLocations));
            sqlSessionFactoryBean.setConfiguration(configuration);
            sqlSessionFactoryBean.setPlugins(interceptors);
//            sqlSessionFactoryBean.setConfigLocation(resourcePatternResolver.getResource(configLocation));
            return sqlSessionFactoryBean;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
//    不推荐用这种方式,因为这种方式 BeanDefinitionRegistryPostProcessor的一个实现,会覆盖默认PostProcessor和产生冲突,用MapperScan注解即可。
//    @Bean
//    MapperScannerConfigurer mapperScannerConfigurer(){
//        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
//        mapperScannerConfigurer.setBasePackage("com.jd.crm.message.instructure.mybatis.mapper");
//        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
//        return mapperScannerConfigurer;
//    }
}

关于配置属性的含义

很多人大多数开始配置的mybatis的时候,很多注解或者属性的含义不是很熟悉,主要包括springboot的机制,mybatis的配置和原理,jdbc的原理,DataSource(druid)的原理等等。这里为了避免篇幅过长,就不进行说明和讲解,大家遇到问题可以在官网查阅相关文档,或者留言和私信一起交流。

相关文章

网友评论

    本文标题:整合springboot + mybatis

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