springboot-mysql-HikariCP集成

springboot-mysql-HikariCP集成

HikariCP数据库连接池,日语,‘光’,快速稳定,性能杠杠滴

 

springboot版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
</parent>

HikariCP,jar包

<!-- jdbc -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.2</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 数据库连接池 -->
<dependency>
   <groupId>com.zaxxer</groupId>
   <artifactId>HikariCP</artifactId>
</dependency>

application.properties配置:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/qikegu_demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.max-idle=10
spring.datasource.min-idle=5

#Console打印sql
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

mysql配置:

import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {

        @Bean(name = "dataSource")
        public DataSource datasource(Environment env) {
                 HikariDataSource ds = new HikariDataSource();
                 ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
                 ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
                 ds.setUsername(env.getProperty("spring.datasource.username"));
                 ds.setPassword(env.getProperty("spring.datasource.password"));
                ds.setMaximumPoolSize(Integer.parseInt(env.getProperty("spring.datasource.max-idle")));
                ds.setMinimumIdle(Integer.parseInt(env.getProperty("spring.datasource.min-idle")));
                 return ds;
        }
}

mybatis配置:

import javax.sql.DataSource;
import javax.annotation.Resource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ConditionalOnClass({ EnableTransactionManagement.class })
@AutoConfigureAfter({ DataSourceConfig.class })
@MapperScan(basePackages = { "com.szxx.mysql.**.impl" })
public class MybatisConfig {

        Logger logger = Logger.getLogger(this.getClass());

        @Resource
        private DataSource dataSource;

        /**
         * Sqlsession工厂创建
         *
         * @return
         */
        @Bean
        public SqlSessionFactory sqlSessionFactory() {

                 try {
                         logger.info("Mybatis sessionFactory load begin...");
                         SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
                         sessionFactoryBean.setDataSource(dataSource);
                         sessionFactoryBean.setTypeAliasesPackage("com.szxx.**.bean");
                         sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mybatis/*.xml"));
                         logger.info("Mybatis sessionFactory load end...");
                         return sessionFactoryBean.getObject();
                 } catch (Exception e) {
                         throw new RuntimeException(e);
                 }
        }


        /**
         * 事务配置
         *
         * @return
         */
        @Bean
        public DataSourceTransactionManager transactionManager() {
                 return new DataSourceTransactionManager(dataSource);
        }


        /**
         * sqlSessionTemplate
         *
         * @param sqlSessionFactory
         * @return
         * @throws Exception
         */

        @Bean
        public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
                 return new SqlSessionTemplate(sqlSessionFactory);
        }
}

 

 

 

SpringBoot是一种基于Spring框架的快速应用开发框架,具有简单易用、快速开发等优点。而HikariCP是一个高效的开源JDBC连接池库,具有高性能、低延迟等特点,在SpringBoot项目中将其集成,可以提高项目的性能和效率。 要在SpringBoot项目中集成HikariCP,需要在pom.xml文件中添加相关依赖,如下: ``` <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.5</version> </dependency> ``` 同时,在application.properties或application.yml文件中配置相关信息,如下: ``` spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.hikari.connectionTimeout=30000 spring.datasource.hikari.maximumPoolSize=10 ``` 其中,spring.datasource.type指定数据源类型为HikariDataSource,spring.datasource.url指定数据库连接地址,spring.datasource.username和spring.datasource.password分别指定连接用户名和密码,spring.datasource.hikari.connectionTimeout指定连接超时时间为30秒,spring.datasource.hikari.maximumPoolSize指定连接池的最大连接数量为10个。 最后,在需要使用数据源的类中,通过注解的方式注入数据源即可,如下: ``` @Autowired DataSource dataSource; ``` 以上就是SpringBoot集成HikariCP的相关步骤和配置方式,可以根据实际项目的需要进行相应调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值