Spring註解配置HikariCP實戰案例

HikariCP號稱史上最快連接池,SpringBoot也已經採用HikariCP作爲默認連接池,說明HikariCP的性能強。最關鍵的一點就是,HikariCP連接池不是使用的傳統的ArrayList而是用的FastList。ArrayList操作get的時候,每次都要去檢查一遍數組角標,而FastList卻不需要去檢查,類似的還有remove方法。
下面就是一個簡單的實戰小案例,幫助剛剛瞭解到HikariCp的朋友們,初步體驗一下。

配置文件

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;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章