1.SpringBoot 默认支持如下数据源
1、com.zaxxer.hikari.HikariDataSource (Spring Boot 2.0 以上,默认使用此数据源)
2、org.apache.tomcat.jdbc.pool.DataSource
3、org.apache.commons.dbcp2.BasicDataSource
spring:
# 数据源配置
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://192.168.1.118:3306/dev_ops?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&allowMultiQueries=true&useOldAliasMetadataBehavior=true&autoReconnect=true&failOverReadOnly=false
pom.xml中添加
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
2、部分常用连接参数解释
MySQL 8.0 以上版本的数据库连接有所不同:
1、MySQL 8.0 以上版本驱动包版本 mysql-connector-java-8.0.16.jar。
2、com.mysql.jdbc.Driver 更换为 com.mysql.cj.jdbc.Driver。
参数名称 | 参数说明 | 示例 | 备注 |
type | 数据源 | com.zaxxer.hikari.HikariDataSource | |
driver-class-name | 驱动程序类名 | com.mysql.cj.jdbc.Driver com.mysql.jdbc.Driver(8.0以下) | |
username | 数据库用户名 | root | |
password | 数据库用户密码 | 123456 | |
useUnicode | 是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为true | true false | |
characterEncoding | 当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbk | UTF-8 gbk gb2313 | |
zeroDateTimeBehavior | 解决 timestamp异常 java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP | convertToNull exception round | ception:默认值,即抛出SQL state [S1009]. Cannot convert value....的异常; convertToNull:将日期转换成NULL值; round:替换成最近的日期即0001-01-01; |
useSSL | MySQL在高版本需要指明是否进行SSL连接 | true false | |
serverTimezone | mysql8.x的jdbc升级了,增加了时区属性,并且不允许为空 | Asia/Shanghai GMT%2B8 UTC | Asia/Shanghai:上海时间 GMT%2B8:北京时间 UTC:UTC是全球标准时间,北京地区早标准时间8小时 |
useJDBCCompliantTimezoneShift | 解决时区错误 | true false | |
useLegacyDatetimeCode | 这个参数是一个开关参数,默认为true,但是当其设置为false时,useTimezone, useJDBCCompliantTimezoneShift, useGmtMillisForDatetimes, and useFastDateParsing.这几个参数都会变的无效化。 | true false | |
allowMultiQueries | 1.可以在sql语句后携带分号,实现多语句执行。2.可以执行批处理,同时发出多个SQL语句 | true false | |
useOldAliasMetadataBehavior | 别名设置 | true false | |
autoReconnect | 当数据库连接异常中断时,是否自动重新连接? | true false | |
failOverReadOnly | 自动重连成功后,连接是否设置为只读? | true false | |
maxReconnects | autoReconnect设置为true时,重试连接的次数 | 1 2 3 | |
initialTimeout | autoReconnect设置为true时,两次重连之间的时间间隔,单位:秒 | 1 2 3 | |
connectTimeout | 和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4及更高版本 | 0 10000 | |
socketTimeout | socket操作(读写)超时,单位:毫秒。 0表示永不超时 | 0 10000 |
3.多数据源配置与使用
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
配置多数据源
spring:
datasource: #数据库链接相关配置
dynamic:
druid: #以下是全局默认值,可以全局更改
#监控统计拦截的filters
filters: stat
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
stat:
merge-sql: true
log-slow-sql: true
slow-sql-millis: 2000
primary: A
datasource:
A:
url: jdbc:mysql://192.168.1.118:3306/dev_ops_aa?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&allowMultiQueries=true&useOldAliasMetadataBehavior=true&autoReconnect=true&failOverReadOnly=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
B:
url: jdbc:mysql://192.168.1.118:3306/dev_ops_bb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&allowMultiQueries=true&useOldAliasMetadataBehavior=true&autoReconnect=true&failOverReadOnly=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
C:
url: jdbc:mysql://192.168.1.118:3306/dev_ops_cc?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&allowMultiQueries=true&useOldAliasMetadataBehavior=true&autoReconnect=true&failOverReadOnly=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
多数据源的使用
//不加@DS 表示使用默认的数据库,即 A
@Service
public class TeacherServiceImpl {
public boolean addTeacherWithTx(String name, Integer age) {
return teacherMapper.addTeacher(name, age);
}
}
//加在**ServiceImpl实现类上:表示这个类都使用B数据源
@Service
@DS(B)
public class TeacherServiceImpl extends ** implements ** {
public boolean addTeacherWithTx(String name, Integer age) {
return teacherMapper.addTeacher(name, age);
}
}
//加在实现类的方法上,表示这个方法使用C数据源
@Service
public class TeacherServiceImpl extends ** implements ** {
@DS(C)
public boolean sel**(String name, Integer age) {
return teacherMapper.sel**(name, age);
}
}
//加在某个mapper的get**的语句上,表示使用C数据源
@Mapper
public interface **Mapper extends BaseMapper<**> {
@DS(C)
@Select(SELECT COUNT(1) +
FROM sys_file_log +
WHERE del_flag = FALSE +
AND do_status = 3 +
AND bus_date = #{dateStr})
int get**(@Param(dateStr) String dateStr);
}
4.使用jdbc:mysql:replication 读写分离
spring:
# 数据源配置
datasource:
#还没有测试mysql8是否支持,若不支持就退回 com.mysql.jdbc.Driver
driver-class-name: com.mysql.cj.jdbc.Driver
#这个配置需要保证所有mysql的账户和密码一致
url: jdbc:mysql:replication://192.168.1.118:3306,192.168.1.119:3306,192.168.1.120:3306/dev_ops_aa?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&allowMultiQueries=true&useOldAliasMetadataBehavior=true&autoReconnect=true&failOverReadOnly=false
username: root
password: 123456
#数据库连接池配置
hikari:
##等待从连接池中获得连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒
connection-timeout: 30000
##一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟
idle-timeout: 600000
##一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒以上
max-lifetime: 1765000
pool-name: slave
maximum-pool-size: 50
url:jdbc:mysql:replication://,.../...?...=...
master库为第一个:为主从数据库中的主库
slave库为后面的多个:为主从数据库的从库
用法:
注意:@Transactional 默认的readOnly参数是false,更新操作不需要特别的改动。propagation是指的事务传播方式,默认设置是Require,指的是“本次操作需要事务支持,如果没有事务开启一个事务,如果有事务,加入到该事务中”
@Transactional (propagation=Propagation.REQUIRED, readOnly = true )
public OrderBase getOrder(String orderCode) {
findSubOrderList(orderCode);
}
@Transactional (propagation=Propagation.REQUIRED, readOnly = true )
public List<OrderSub> findSubOrderList(String orderCode) {
}
@Transactional (propagation=Propagation.REQUIRED, readOnly = false )
public void updateOrder(OrderBase orderBase) {
findSubOrderList(orderBase.getCode());
...
}
1、当外部调用getOrder时,getOrder方法的@Transaction(readOnly = true 只读 )注解生效,设置从库查询。
2、当外部调用updateOrder时,updateOrder方法的@Transaction(readOnly = false 不是只读)注解生效,设置操作主库。
注意:这两个方法都调用了findSubOrderList方法,而调用的对象是this,不是被spring事务管理器替换过的service对象,所以findSubOrderList方法上的@Transaction注解无效,会根据上文环境来查主库和从库
这种特性对于业务来说是恰当好处的,生效的事务是在最外层的方法上,可以避免在一个事务内部出现读写库不统一的情况
6.使用jdbc:mysql:loadbalance 负载均衡
spring:
# 数据源配置
datasource:
#还没有测试mysql8是否支持,若不支持就退回 com.mysql.jdbc.Driver
driver-class-name: com.mysql.cj.jdbc.Driver
#这个配置需要保证所有mysql的账户和密码一致
url: jdbc:mysql:loadbalance://192.168.1.119:3306,192.168.1.119:3306,192.168.1.120:3306/dev_ops_aa?roundRobinLoadBalance=true&failOverReadOnly=false&tcpRcvBuf=1024000&loadBalanceStrategy=bestResponseTime&loadBalanceBlacklistTimeout=300000&loadBalancePingTimeout=1000&selfDestructOnPingMaxOperations=200&queryTimeoutKillsConnection=true&loadBalanceValidateConnectionOnSwapServer=true&connectTimeout=1000&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&allowMultiQueries=true&useOldAliasMetadataBehavior=true&autoReconnect=true&failOverReadOnly=false
username: root
password: 123456
#数据库连接池配置
hikari:
##等待从连接池中获得连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒
connection-timeout: 30000
##一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟
idle-timeout: 600000
##一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒以上
max-lifetime: 1765000
pool-name: slave
maximum-pool-size: 50
添加的参数解释(官方解释里有):
参数名 | 默认值 | 设置值 | 含义 |
roundRobinLoadBalance | false | true | 采用权重轮循均衡算法 |
failOverReadOnly | true | false | 自动重连成功后,连接不设为为只读 |
tcpRcvBuf | 0 | 1024000 | TCP接收缓冲区增大到1MB |
loadBalanceStrategy | random | bestResponseTime | 默认为“random”即随机路由请求,“bestResponseTime”策略负载均衡将请求路由到对上一个事务具有最佳响应时间的主机。 |
loadBalanceBlacklistTimeout | 0 | 300000 | 如果某个节点请求时返回SQLException,那么此host将会被添加到黑名单中,在此后的timeout时间内下一次选择时将不会再被选中。 |
loadBalancePingTimeout | 0 | 1000 | 负责均衡响应ping的超时时间 |
selfDestructOnPingMaxOperations | 0 | 200 | 连接ping不通被关闭时报告阈值 |
queryTimeoutKillsConnection | false | true | 查询超时关闭连接 |
loadBalanceValidateConnectionOnSwapServer | false | true | 当在提交/回滚时交换到新的物理连接时,负载平衡连接显式检查连接处于活动状态 |
connectTimeout | 0 | 1000 | 连接超时时间 |
更多推荐