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 项目所支 ...
随机推荐
- 3.0 rust 项目路径
$ rustc --versionrustc 1.44.0 (49cae5576 2020-06-01) 将代码存在到不同的文件 main.rs mod aa; fn main() { println ...
- swagger文档
关键配置文件 spring boot demo pom.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- 【Java 基础】Java Map中的Value值如何做到可以为任意类型的值
Occasionally the average developer runs into a situation where he has to map values of arbitrary typ ...
- 『学了就忘』Linux启动引导与修复 — 74、Linux系统的修复模式(光盘修复模式)
目录 1.光盘修复模式概念 2.光盘修复模式修复系统问题 (1)准备系统光盘 (2)进入BIOS (3)修改BIOS的启动顺序 (4)进入光盘修复模式 (5)修复系统 (6)修复系统实操 (7)总结 ...
- Nginx支持php
目录 一.简介 二.配置 三.测试 四.参数 一.简介 Nginx本身只能解析html文件,但有些网页是php写的,就需要Nginx连接php,将网页解析成html再发给客户端. 配置中将.php 结 ...
- 12.16 Java继承
首先 :继承,指一个对象直接使用另一对象的属性和方法. 继承的格式: public class 子类名 entends 父类名{} /* 表示前面的子类继承父类 */ 例:public class ...
- BUGKU web刷题记录
web1 直接F12查看源码,得到flag. web2 直接输入验证码答案,长度被限制,修改可输入长度,提交后得到flag. web3 $what=$_GET['what']; echo $what; ...
- LuoguB2029 大象喝水 题解
Update \(\texttt{2021.12.4}\) 修改了原先的错误代码,给各位造成影响,在此表示很抱歉. Content 大象要喝 \(20\) 升水,但现在只有一个深 \(h\) 厘米,半 ...
- java 多线程:线程安全问题,示例DateFormat多线程执行冲突解决方案ThreadLocal、方法内变量
SimpleDateFormat多线程中执行报错 java.lang.NumberFormatException: For input string: "" import ja ...
- Postman环境变量的使用
前言 请注意,Postman新版有ui上的改动,本文使用的Postman 版本8.4.0 for Mac, ui有调整,但是功能无改变. Postman是一款接口调测的软件,服务端开发的同学肯定会对自 ...