一 . 原因及报错(模拟场景)
<update id="test">
UPDATE t_student SET name='33',age=22 WHERE stu_id = '1';
UPDATE t_student SET name='33',age=22 WHERE stu_id = '2';
</update>
// 报错信息
### Error updating database. Cause: java.sql.SQLSyntaxErrorException:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE t_student set name='33',age=22 WHERE stu_id = '2' at line 5
### SQL: UPDATE t_student set name=?,age=? WHERE stu_id = ? ; UPDATE t_student set name=?,age=? WHERE stu_id = ?
### Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE t_student set name='33',age=22 WHERE stu_id = '2' at line 5 ;
bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE t_student set name='33',age=22 WHERE stu_id = '2' at line 5
二 . 解决方法 : 在数据库连接配置的url上加上allowMultiQueries=true
spring:
datasource:
dynamic:
# 是否开启 SQL日志输出,生产环境建议关闭,有性能损耗
p6spy: false
hikari:
connection-timeout: 30000
max-lifetime: 1800000
max-pool-size: 15
min-idle: 5
connection-test-query: select 1
# 配置默认数据源
primary: mysql
datasource:
# 数据源-1,名称为 primary
mysql:
username: xxx
password: xxxx
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/xx?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8&autoReconnect=true&failOverReadOnly=false
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: true
testOnReturn: true
三 . 延伸学习--boot在配置数据库url上各个属性的意义及作用
MySQL 8.0后版本驱动类名称为:"com.mysql.cj.jdbc.Driver"
如果用于配置JDBC URL的机制是基于XML来配置的,请使用XML字符文字&;分隔配置参数,因为符号(&)是XML的保 留字符。
属性 | 描述 | 默认值(缺省值) | 版本 | 备注 | |
高可用和集群属性配置(High Availability and Clustering. ) | autoReconnect | 驱动程序是否应尝试重新建立过时和/或死掉的连接?如果启用,驱动程序将对在旧连接或死连接上发出的属于当前事务的查询引发异常,但将在新事务中对该连接发出的下一个查询之前尝试重新连接。不建议使用此功能,因为当应用程序不能正确处理SQLExceptions时,它会产生与会话状态和数据一致性相关的副作用,并且仅当您无法将应用程序配置为正确处理因死掉和过时的连接而导致的SQLExceptions时才使用此功能。或者,作为最后一个选项,研究将mysql服务器变量“wait_timeout”设置为高值,而不是默认的8小时。 | false | 1.1版本起 | |
autoReconnectForPools | 使用适用于连接池的重新连接策略(默认为“false”) | false | 3.1.3版本起 | ||
failOverReadOnly | 在autoReconnect模式下进行故障转移时,连接是否应设置为“只读” | true | 3.0.12版本起 | 在使用数据库连接池的情况下,且autoReconnect=true时最好设置该参数:failOverReadOnly=false | |
maxReconnects | 如果autoReconnect为true,则尝试重新连接的最大次数,默认为“3”。 | 3 | 1.1版本起 | ||
initialTimeout | 如果启用了autoReconnect,则在重新连接尝试之间等待的初始时间(以秒为单位,默认为“2”)。 | 2 | 1.1版本起 | ||
安全相关(Security) | allowMultiQueries | 是否允许使用';'在一个语句中分隔多个查询(true/false),默认为'false',不影响addBatch()和executeBatch()方法,它们依赖于rewriteBatchStatements。 | false | Since version: 3.1.1 | |
useSSL | 与服务器通信时使用ssl(true/false),连接到MySQL5.5.45+、5.6.26+或5.7.6+时默认为“false”,其他版本默认为“true | true|false(5.5.45+、5.6.26+或5.7.6+) | |||
其他 | useUnicode | 驱动程序在处理字符串时是否应使用Unicode字符编码?只应在驱动程序无法确定字符集映射时使用,或者您试图“强制”驱动程序使用MySQL本身不支持的字符集(例如UTF-8、gb2312或gbk),true / false ,默认为'true' | true | 1.1g版本起 | 必须设置为true,可能会导致中文数据乱码问题 |
characterEncoding | 如果'useUnicode'设置为true,那么驱动程序在处理字符串时应该使用什么字符编码?(默认为'自动检测') | 自动检测 | 1.1g版本起 | 当useUnicode设置为true时,指定字符编码。比如可设置为UTF-8、gb2312或gbk | |
serverTimezone | 覆盖时区的检测/映射。当服务器的时区未映射到Java时区时使用 | 3.0.2版本起 | 当数据库时区未映射到Java时区时可能导致Java代码中Date类型插入到mysql中datetime类型出现时间不一致的问题。例如: 上海:serverTimezone=Asia/Shanghai 简写:serverTimezone=CTT 北京:serverTimezone=UTC+8 或者:serverTimezone=GMT+8 |