Spring注解配置HikariCP实战案例

本文介绍HikariCP——一款号称史上最快的数据库连接池,并通过一个实战案例展示其在SpringBoot项目中的配置与使用。文章详细讲解了如何在pom.xml中引入依赖,配置连接池属性,以及如何通过Java代码实现数据库连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HikariCP号称史上最快连接池,SpringBoot也已经采用HikariCP作为默认连接池,说明HikariCP的性能强。最关键的一点就是,HikariCP连接池不是使用的传统的ArrayList而是用的FastList。ArrayList操作get的时候,每次都要去检查一遍数组角标,而FastList却不需要去检查,类似的还有remove方法。
下面就是一个简单的实战小案例,帮助刚刚了解到HikariCp的朋友们,初步体验一下。
pom文件


    <properties>
        <hikari.version>2.6.1</hikari.version>
    </properties>
    
  <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>${hikari.version}</version>
            <scope>compile</scope>
        </dependency>

配置文件 hirariCP.properties

hikariCP.driver=com.mysql.jdbc.Driver
hikariCP.url=jdbc:mysql:///test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
hikariCP.username=root
hikariCP.password=
hikariCP.cachePrepStmts=true
hikariCP.prepStmtCacheSize=250
hikariCP.prepStmtCacheSqlLimit=2048

bean

@Component
@PropertySource("classpath:hikariCP.properties")
public class HikariInfo {

 @Value("${hikariCP.driver}")
 private String driver;

 @Value("${hikariCP.url}")
 private String url;

 @Value("${hikariCP.username}")
 private String username;

 @Value("${hikariCP.password}")
 private String password;

 @Value("${hikariCP.cachePrepStmts}")
 private boolean cachePrepStmts;

 @Value("${hikariCP.prepStmtCacheSize}")
 private int prepStmtCacheSize;

 @Value("${hikariCP.prepStmtCacheSqlLimit}")
 private int prepStmtCacheSqlLimit;

 public String getDriver() {
  return driver;
 }

 public String getUrl() {
  return url;
 }

 public String getUsername() {
  return username;
 }

 public String getPassword() {
  return password;
 }

 public boolean getCachePrepStmts() {
  return cachePrepStmts;
 }

 public int getPrepStmtCacheSize() {
  return prepStmtCacheSize;
 }

 public int getPrepStmtCacheSqlLimit() {
  return prepStmtCacheSqlLimit;
 }
}

配置连接池

@Configuration
public class HikariConfig {

    @Resource
    private HikariInfo hikariInfo;

    @Bean
    public HikariDataSource getDataSource(){
        HikariDataSource hikariDataSource=new HikariDataSource();
        hikariDataSource.setDriverClassName(hikariInfo.getDriver());
        hikariDataSource.setJdbcUrl(hikariInfo.getUrl());
        hikariDataSource.setUsername(hikariInfo.getUsername());
        hikariDataSource.setPassword(hikariInfo.getPassword());
        hikariDataSource.addDataSourceProperty("cachePrepStmts",hikariInfo.getCachePrepStmts());
        hikariDataSource.addDataSourceProperty("prepStmtCacheSize",hikariInfo.getPrepStmtCacheSize());
        hikariDataSource.addDataSourceProperty("prepStmtCacheSqlLimit",hikariInfo.getPrepStmtCacheSqlLimit());
        return hikariDataSource;
    }

    @Bean
    public JdbcTemplate getJdbcTemplate(HikariDataSource hikariDataSource){
        return new JdbcTemplate(hikariDataSource);
    }

}

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 根据id查找
     * @param t_userId
     * @return
     */
    public User findByUserId(String t_userId){
        String sql="SELECT * FROM t_user WHERE t_userId = ? ";
        User user=null;
        try {
            user=jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),t_userId);
        } catch (DataAccessException e) {
            return null;
        }
        return user;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值