1.创建springboot项目
创建这个不用说了吧。。。
这儿有创建springboot的教程
2.添加坐标
gradle: gradle与maven转换,gradle中的字符串,groupId,artifactId,version以冒号隔开的,maven的可以自行转换
compile 'com.zaxxer:HikariCP:3.1.0'
compile 'mysql:mysql-connector-java:5.1.47'
compile 'org.springframework.boot:spring-boot-starter-jdbc'
maven:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.1.0</version>
</dependency>
3.在resource下创建一个 application.yml文件
4.配置好HikariCP的数据源内容
spring:
datasource:
hikari:
jdbc-url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
username: root
password: mysql
driver-class-name: com.mysql.jdbc.Driver
pool-name: principalHikariCP
maximum-pool-size: 5
5.在springboot主程序包创建一个config文件夹,然后在config下面创建DataSourceConfig.java文件
6.DataSourceConfig的配置如下:
package com.cyjz.config;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DataSourceOneConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource(){
return new HikariDataSource();
}
7.配置完成!
标签:springboot,HikariCP,数据源,springframework,gradle,mysql,import,com From: https://blog.51cto.com/u_15932265/5993510