SpringBoot 之 整合JDBC使用
导入相关依赖:
# pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
配置数据库连接信息:
# src/main/resources/application.yml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
# driver-class-name: com.mysql.jdbc.Driver # 已过时
driver-class-name: com.mysql.cj.jdbc.Driver
测试连接:
@SpringBootTest
public class SpringbootDataApplicationTests {
@Autowired
DataSource dataSource;
@Test
public void contextLoads() throws SQLException {
// 查看默认数据源
System.out.println(dataSource.getClass());
// 查看链接
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
简单使用示例:
# src/main/java/com/wu/controller/JdbcController.java
@RestController
public class JdbcController {
@Autowired
JdbcTemplate jdbcTemplate;
@GetMapping("/users")
public List<Map<String, Object>> index() {
String sql = "select * from user";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
return list;
}
@PostMapping("/users")
public int store() {
String sql = "insert into user(id,name) values (1,'小明')";
return jdbcTemplate.update(sql);
}
@GetMapping("/users/{id}")
public Map<String, Object> show(@PathVariable("id") int id) {
String sql = "select * from user where id=" + id;
Map<String, Object> map = jdbcTemplate.queryForObject(sql);
return map;
}
@PutMapping("/users/{id}")
public int update(@PathVariable("id") int id) {
String sql = "update user set name=? where id=" + id;
Object[] objects = new Object[1];
objects[0] = "小花";
return jdbcTemplate.update(sql, objects);
}
@DeleteMapping("/users/{id}")
public int destroy(@PathVariable("id") int id) {
String sql = "delete from user where id=?";
return jdbcTemplate.update(sql, id);
}
}
SpringBoot 之 整合JDBC使用的更多相关文章
- SpringBoot 整合jdbc和mybatis
摘要 该文章主要为记录如何在SpringBoot项目中整合JDBC和MyBatis,在整合中我会使用简单的用法和测试用例,毕竟该文章目的是为了整合,而不是教大家如何去使用.希望大家多多包涵. 通用配置 ...
- springboot之整合基本的jdbc并操作Mysql数据库
对于数据访问层,无论是SQL还是NOSQL,springboot默认采用整合spring data方式进行统一处理,添加大量自动配置,屏蔽了许多设置,引入各种xxxTemplate,xxxReposi ...
- springboot整合JDBC出现Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
今天使用springboot整合JDBC的使用,开始使用的是 com.mysql.jdbc.Driver驱动 结果运行出现此异常 那我们根据提示要求来修改即可 把驱动改成最新的com.mysql.cj ...
- 3、SpringBoot整合之SpringBoot整合JDBC
SpringBoot整合JDBC 一.创建SpringBoot项目 选择Spring Web.JDBC API.MySQL Driver 二.在pom配置文件中修改JDBC版本,导入lombok &l ...
- SpringBoot整合jdbc及整合Druid数据源
一.整合jdbc 1.创建一个springInitializr项目 勾选 web----springweb.SQL----JDBC API,MYSQL Diver 2.连接数据库 3.创建yml 4. ...
- 9 — springboot整合jdbc、druid、druid实现日志监控 — 更新完毕
1.整合jdbc.druid 1).导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...
- springboot下整合各种配置文件
本博是在springboot下整合其他中间件,比如,mq,redis,durid,日志...等等 以后遇到再更.springboot真是太便捷了,让我们赶紧涌入到springboot的怀抱吧. ap ...
- SpringBoot Druid整合,SpringBoot 集成Druid
SpringBoot Druid整合,SpringBoot 集成Druid ================================ ©Copyright 蕃薯耀 2018年4月8日 http ...
- SpringBoot+SpringData 整合入门
SpringData概述 SpringData :Spring的一个子项目.用于简化数据库访问,支持NoSQL和关系数据存储.其主要目标是使用数据库的访问变得方便快捷. SpringData 项目所支 ...
随机推荐
- spring注解事务管理
使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...
- Java Timestamp 类的使用
很简单,我们可以这样声明 Timestamp ts=new Timestamp(new Date().getTime());这样我们就可以得到时间比较具体的一个类型转换!!! 在开发web应用中,针对 ...
- oracle name
1.db_name 数据库名 SQL> connect xys/manager as sysdba 已连接. SQL> show user USER 为 "SYS" S ...
- APICloud - 提交项目 点击右键 没有git这个选项
你们是不是也遇到过这个问题,吧项目检出来后,花了很久的时间,好不容易吧项目改完,提交的时候点击鼠标右键,发现git选项没有在里面了,找不到,但是这个问题也不是很常遇到,机率很小,下面我来告诉你们吧 原 ...
- 查询表名和表备注(中文名) 及 dba_tables、all_tables和user_tables的区别
1. select a.* from ALL_TAB_COMMENTS a --查表名和表中文名select a.* from ALL_TAB_COLUMNS a --查询表字段属性select a. ...
- Java高精度基础+开根
在焦作站的acm网络赛中遇到了一个高精度开根的水题--但是那时候WA了 后面学写java补题还T了orz 所以写一篇文章来记录一下java的大整数类型的基础和开根还有一点心得体会吧 首先给那一题的题面 ...
- MySQL常见错误总结
一.1205 - Lock wait timeout exceeded; try restarting transaction
- Linux驱动实践:一起来梳理中断的前世今生(附代码)
作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...
- AtCoder Beginner Contest 173 题解
AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...
- CF80B Depression 题解
Content 有一个时针,给定时间为 \(\text{HH}\) 时 \(\text{MM}\) 分,求图中 \(\alpha\) 和 \(\beta\) 角的值. 手画勿喷/kk 数据范围:\(0 ...