Springboot整合hikaricp mybatis

1.添加Maven依赖


    mysql
    mysql-connector-java
    runtime



    org.springframework.boot
    spring-boot-starter-jdbc
    
        
            org.apache.tomcat
            tomcat-jdbc
        
    



    com.zaxxer
    HikariCP



    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.3.2



    
        src/main/java
        
            **/*.xml
            **/*.properties
        
    

2.db.properties配置

dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
dataSource.user=root
dataSource.password=root
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=25
dataSource.prepStmtCacheSqlLimit=204
dataSource.databaseName=test
dataSource.serverName=127.0.0.1
dataSource.portNumber=3306
dataSource.minimumIdle=1
dataSource.maximumPoolSize=5

3.DataSourceConfig配置

   @Configuration
   @PropertySources(value = {@PropertySource("classpath:db.properties")})
   public class DataSourceConfig {
       @Autowired
       private Environment environment;
   
       @Bean("dataSource")
       public DataSource getDataSource() {
           Properties props = new Properties();
           props.setProperty("dataSourceClassName", environment.getRequiredProperty("dataSourceClassName"));
           props.setProperty("dataSource.user", environment.getRequiredProperty("dataSource.user"));
           props.setProperty("dataSource.password", environment.getRequiredProperty("dataSource.password"));
           props.setProperty("dataSource.databaseName", environment.getRequiredProperty("dataSource.databaseName"));
           props.setProperty("dataSource.serverName", environment.getRequiredProperty("dataSource.serverName"));
           props.setProperty("dataSource.portNumber", environment.getRequiredProperty("dataSource.portNumber"));
           props.setProperty("dataSource.cachePrepStmts", environment.getRequiredProperty("dataSource.cachePrepStmts"));
           props.setProperty("dataSource.prepStmtCacheSize", environment.getRequiredProperty("dataSource.prepStmtCacheSize"));
           props.setProperty("dataSource.prepStmtCacheSqlLimit", environment.getRequiredProperty("dataSource.prepStmtCacheSqlLimit"));
           props.setProperty("dataSource.useUnicode", "true");
           props.setProperty("dataSource.characterEncoding", "utf8");
           HikariConfig hikariConfig = new HikariConfig(props);
           String minimumIdle = environment.getRequiredProperty("dataSource.minimumIdle");
           String maximumPoolSize = environment.getRequiredProperty("dataSource.maximumPoolSize");
   
           if (StringUtils.isNotBlank(minimumIdle)) {
               hikariConfig.setMaximumPoolSize(Integer.parseInt(minimumIdle));
           }
           if (StringUtils.isNotBlank(maximumPoolSize)) {
               hikariConfig.setMinimumIdle(Integer.parseInt(maximumPoolSize));
           }
           return new HikariDataSource(hikariConfig);
       }
   }

4.MyBatisConfig配置

   @Configuration
   @MapperScan("com.boomsecret.dao.mapper")
   public class MyBatisConfig implements TransactionManagementConfigurer {
   
       @Autowired
       @Qualifier("dataSource")
       private DataSource dataSource;
   
       @Bean(name = "sqlSessionFactory")
       public SqlSessionFactory sqlSessionFactoryBean() {
           SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
           bean.setDataSource(dataSource);
           try {
               return bean.getObject();
           } catch (Exception e) {
               e.printStackTrace();
               throw new RuntimeException(e);
           }
       }
   
       @Bean
       public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
           return new SqlSessionTemplate(sqlSessionFactory);
       }
   
       @Override
       public PlatformTransactionManager annotationDrivenTransactionManager() {
           return new DataSourceTransactionManager(dataSource);
       }
   }

源码 https://gitee.com/jsjack_wang/springboot-demo dev-hikaricp-mybatis分支

你可能感兴趣的:(javaweb)