博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot_06 SpringBoot集成mySQL
阅读量:3967 次
发布时间:2019-05-24

本文共 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
> result = jdbcTemplate.queryForList("select * from user"); System.out.println("query result is"+result.size()); System.out.println("success"); } @Test public void testMySqlForUpdate(){
jdbcTemplate.execute("update user set password='111111' where cardnum='102'"); System.out.println("success"); }

运行就会发现,已经可以对数据库进行操作了。增删改查都可以实现。(当然需要自己去建库,建表)

在连接时,我遇到的问题:

(1)在导入依赖的时候,一定要注意版本,我第一次就是因为没有注意版本号与我的数据库是否对应,导致报错。

(2)在我第一次错误的时候发现,最新版的jdbc,driver参数好像有改动,变成了

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver。
但是是否可以运行成功,我就无法测试了,因为我的数据库版本低。

(3)一定不要忘了加@Autowired注解。

转载地址:http://iayki.baihongyu.com/

你可能感兴趣的文章
Flutter UI基础 - GridView
查看>>
Flutter UI - 打造一个圆形滑块(Slider)
查看>>
Flutter UI基础 - 分割线效果实现
查看>>
Flutter UI基础 - DecoratedBox组件
查看>>
Flutter UI基础 - 使用InkWell给任意Widget添加点击事件
查看>>
OC WKWebView的使用
查看>>
Flutter UI基础 - Image.asset 图片铺满布局
查看>>
Flutter UI基础 - Row、Column详解
查看>>
Flutter UI基础 - 添加背景图片
查看>>
Flutter UI基础 - 布局之Row/Column/Stack
查看>>
Flutter UI基础 - 层叠布局Stack的使用
查看>>
Flutter UI基础 - webview 使用和交互
查看>>
Flutter UI基础 - 时间选择器
查看>>
Idea - 创建Java类时,自动在文件头中添加作者和创建时间
查看>>
Docker - ASP.NET Core Docker部署
查看>>
Docker - 容器运行 .Net Core
查看>>
Django - TypeError: __init__() missing 1 required positional argument: ‘on_delete‘ 的解决办法
查看>>
Go - 解决 go get 超时问题
查看>>
Go - goose 数据库迁移工具
查看>>
SQL - SQL Server 之遍历数据集合的几种方法
查看>>