您的位置:首页 > 编程语言 > Java开发

Spring Boot 连接mysql踩坑之旅

2019-01-14 16:49 417 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/h417251146/article/details/86479157

第一个错误:

           一开始配置号以后会提示找不到mysql驱动,检查maven已把驱动包下载到工程,配置的名称也对,最后发现在

 com.mysql.jdbc.Driver的后面有空格,启动时不会自动消除空格。把配置文件过了一编,将所有多余的空格去掉,不在报这个错误

第二个错误:

           上一个错误好了之后开始报Communications link failure错误,检查后发现是个低级错误,连接的数据库没有开远程权限。

第三个错误:

         上面解决后报:he server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.   的错误,是因为在数据库url后面没有配置字符串,将:useUnicode=true&characterEncoding=utf8&serverTimezone=GMT这句配置加到后面运行正常

maven配置如下:
 

[code]<!-- mysql 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

application.properties配置如下

[code]server.port=8081
########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://*******:3306/map?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.username = dingding
spring.datasource.password = 123456
spring.datasource.tomcat.max-active=100
spring.datasource.tomcat.max-idle=200
spring.datasource.tomcat.initialSize=20

########################################################
### Java Persistence Api
########################################################
# Specify the DBMS
spring.jpa.database=MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

 

第四个问题:

这个不算错误但看着难受,发现运行程序后会自动创建hibernate_sequence表,里面存储的其他表的主键,没添加新数据时都会更新这个主键值。不想每次都查询这个表。

查资料发现是因为实体的id列用了@GeneratedValue

改为@GeneratedValue(strategy = GenerationType.IDENTITY) 。

但是改完之后如果运行过程序自动创建过表,需要删除表让系统重新创建,或者去数据库设置id列自增长,否则会报错。

原因:

@GeneratedValue(strategy = GenerationType.IDENTITY) 要求数据库选择自增方式,oracle不支持此种方式。

@GeneratedValue(strategy = GenerationType.SEQUENCE)采用数据库提供的sequence机制生成主键。mysql不支持此种方式。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: