本文共 1312 字,大约阅读时间需要 4 分钟。
本人是个新手,写下博客用于自我复习、自我总结。 如有错误之处,请各位大佬指出。
1)导入依赖
mysql mysql-connector-java 5.1.37 org.springframework.boot spring-boot-starter-jdbc
2)添加配置文件,配置数据库和其他参数
在resource文件夹下,修改application.properties配置文件并输入参数(需要根据电脑自行去调整)
spring.datasource.url=jdbc:mysql://127.0.0.1:3308/testspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3)测试是否连接成功(在test.java中)
@Autowired private JdbcTemplate jdbcTemplate; @Test public void testMySQL() { List
运行就会发现,已经可以对数据库进行操作了。增删改查都可以实现。(当然需要自己去建库,建表)
在连接时,我遇到的问题:
(1)在导入依赖的时候,一定要注意版本,我第一次就是因为没有注意版本号与我的数据库是否对应,导致报错。
(2)在我第一次错误的时候发现,最新版的jdbc,driver参数好像有改动,变成了
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver。 但是是否可以运行成功,我就无法测试了,因为我的数据库版本低。(3)一定不要忘了加@Autowired注解。
转载地址:http://iayki.baihongyu.com/