springboot连接数据库

mac2026-08-23  1

1.使用jdbc连接数据库

application.properties文件 #设置数据库连接 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/newjdbc?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC #后面一长串是防止乱码

2.测试类 注意使用jdbc的时候如果springboot版本不是2.20会报空指针异常的错误 3.使用jpa连接数据库

要在application里加一句 spring.main.allow-bean-definition-overriding=true 不然会报类名重复的错误

#设置数据库连接 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/newjdbc?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC spring.main.allow-bean-definition-overriding=true
最新回复(0)