栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

springboot整合mybatis 使用HikariCP连接池

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合mybatis 使用HikariCP连接池

为什么使用HikariCP

在Springboot2.X版本,数据库的连接池官方推荐使用HikariCP,官方的原话:

Production database connections can also be auto-configured by using a poolingDataSource. Spring Boot uses the following algorithm for choosing a specific implementation:

  1. We preferHikariCPfor its performance and concurrency. If HikariCP is available, we always choose it.

  2. Otherwise, if the Tomcat poolingDataSourceis available, we use it.

  3. If neither HikariCP nor the Tomcat pooling datasource are available and ifCommons DBCP2is available, we use it.

意思是说:

  1. 我们更喜欢HikariCP的性能和并发性。如果有HikariCP,我们总是选择它

  2. 否则,如果Tomcat池数据源可用,我们将使用它。

  3. 如果HikariCP和Tomcat池数据源都不可用,如果Commons DBCP2可用,我们将使用它。

那么如何使用HikariCP呢?

如果你的springboot版本是2.X,当你使用spring-boot-starter-jdbc或者spring-boot-starter-data-jpa依赖,springboot就会自动引入HikariCP的依赖了。

使用指定的数据库连接池

如果你需要使用指定的数据库连接池,那么你需要在application.properties中配置:spring.datasource.type

环境
  • JDK: 1.8

  • Maven: 3.3.9

  • SpringBoot: 2.0.3.RELEASE

  • 开发工具:Intellij IDEA 2017.1.3

开始使用

本次的配置中我们持久层使用mybatis,使用HikariCP作为数据库连接池。

引入依赖
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                
                    org.apache.tomcat
                    tomcat-jdbc
                
            
        
        
            mysql
            mysql-connector-java
            5.1.46
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

以上的依赖就足够了,前面介绍过,只需要导入spring-boot-starter-jdbc依赖springboot就默认使用Hikari作为数据库连接池了。

创建数据表
CREATE DATAbase mytest;CREATE TABLE t_user(
  userId INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  userName VARCHAr(255) NOT NULL ,  password VARCHAr(255) NOT NULL ,
  phone VARCHAr(255) NOT NULL) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;
创建实体类
package com.winterchen.model;public class UserDomain {    private Integer userId;    private String userName;    private String password;    private String phone;    // @TODO 省略get/set}
创建Dao以及mapper映射创建Dao类

创建一个dao的包,并且在这个包下创建一个UserDao

package com.winterchen.dao;import com.winterchen.model.UserDomain;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import java.util.List;@Mapperpublic interface UserDao {    int insert(UserDomain record);    void deleteUserById(@Param("userId") Integer userId);    void updateUser(UserDomain userDomain);    List selectUsers();

}

注意:一定不要忘了使用@Mapper注解,如果没有这个注解,spring就无法扫描到这个类,导致项目启动报错。

创建Mapper映射

上一步我们创建dao数据库持久层类,由于本文使用的是xml映射的方式,所以我们需要创建一个xml映射文件。

在resources文件夹下新建一个文件夹mapper:


    
        t_user    

    
        userId,userName,password,phone    

    
        INSERT INTO        
        
            userName,password,            
                phone,            
        
        
            #{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR},            
                #{phone, jdbcType=VARCHAR},            
        
    

    
      DELETE FROM      
      WHERe
      userId = #{userId, jdbcType=INTEGER}    
    
    
        UPDATE        
        
          
              userName = #{userName, jdbcType=VARCHAR},          
          
              password = #{password, jdbcType=VARCHAR},          
          
              phone = #{phone, jdbcType=VARCHAR},          
        
        
            userId = #{userId, jdbcType=INTEGER}        
    

    
        SELECT        
        FROM        
    

注意点: 请将namespace="com.winterchen.dao.UserDao"改为你自己项目Dao的路径,以及下面方法的一些路径都要改为你自己项目的相关路径。

配置
server.port=8080#### 数据库连接池属性spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mytest?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=truespring.datasource.username=root
spring.datasource.password=root#自动提交spring.datasource.default-auto-commit=true#指定updates是否自动提交spring.datasource.auto-commit=truespring.datasource.maximum-pool-size=100
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
spring.datasource.validation-query=SELECT 1
spring.datasource.test-on-borrow=falsespring.datasource.test-while-idle=true# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒spring.datasource.time-between-eviction-runs-millis=18800# 配置一个连接在池中最小生存的时间,单位是毫秒spring.datasource.minEvictableIdleTimeMillis=300000# mybatis对应的映射文件路径mybatis.mapper-locations=classpath:mapperpublic interface UserService {    int insert(UserDomain record);    void deleteUserById(Integer userId);    void updateUser(UserDomain userDomain);    List selectUsers();

}
Service 实现层
package com.winterchen.service.impl;import com.winterchen.dao.UserDao;import com.winterchen.model.UserDomain;import com.winterchen.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserServiceImpl implements UserService {    @Autowired
    private UserDao userDao;//这里会爆红,请忽略

    @Override
    public int insert(UserDomain record) {        return userDao.insert(record);
    }    @Override
    public void deleteUserById(Integer userId) {
        userDao.deleteUserById(userId);
    }    @Override
    public void updateUser(UserDomain userDomain) {
        userDao.updateUser(userDomain);
    }    @Override
    public List selectUsers() {        return userDao.selectUsers();
    }
}



作者:Winter_Chen
链接:https://www.jianshu.com/p/89e48fb45e0e


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235197.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号