目录
测试类StudySpringDataApplicationTests代码:
一、SpringData简介
对于数据访问层,无论是 SQL(关系型数据库) 还是 NOSQL(非关系型数据库),Spring Boot 底层都是采用 Spring Data 的方式进行统一处理,Spring Data 也是 Spring 中与 Spring Boot、Spring Cloud 等齐名的知名项目;
1、Sping Data 官网
https://spring.io/projects/spring-data
2、数据库相关的启动器 ,可以参考官方文档
https://docs.spring.io/spring-boot/docs/2.3.3.RELEASE/reference/htmlsingle/#using-boot-starter
二、整合JDBC
1、创建新Spring Boot项目
第一步:填写基本信息
第二步:勾选JDBC API和MySQL Driver即可
2、代码演示
application.yaml配置文件:
spring:
datasource:
username: root
password: zibo123456
url: jdbc:mysql://localhost:3306/zibo?serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
测试类StudySpringDataApplicationTests代码:
package com.zibo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootTest
class StudySpringDataApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
//查看一下默认的数据源
System.out.println("默认的数据源:" + dataSource.getClass());
//获取数据库连接
Connection connection = dataSource.getConnection();
System.out.println("数据库连接:" + connection);
connection.close();
//备注
//XXX Template : SpringBoot已经配置好模板bean,拿来即用
}
}
测试结果:
3、测试执行SQL语句
第一步:导入web mvc依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
第二步:创建JDBCController类
package com.zibo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class JDBCController {
@Autowired
JdbcTemplate jdbcTemplate;
//查询所有account
@GetMapping("/accountList")
public List<Map<String,Object>> accountList(){
String sql = "select * from account";
return jdbcTemplate.queryForList(sql);
}
}
运行结果:
4、测试通过id查询
第一步:修改DBCController类
package com.zibo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class JDBCController {
@Autowired
JdbcTemplate jdbcTemplate;
//查询所有account
@GetMapping("/accountList")
public List<Map<String,Object>> accountList(){
String sql = "select * from account";
return jdbcTemplate.queryForList(sql);
}
//通过id查询
@GetMapping("/account/{id}")
public Map<String,Object> account(@PathVariable("id") int id){
String sql = "select * from account where id = " + id;
return jdbcTemplate.queryForMap(sql);
}
}
运行结果:
5、测试通过id更新
第一步:修改DBCController类
package com.zibo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class JDBCController {
@Autowired
JdbcTemplate jdbcTemplate;
//查询所有account
@GetMapping("/accountList")
public List<Map<String,Object>> accountList(){
String sql = "select * from account";
return jdbcTemplate.queryForList(sql);
}
//通过id查询
@GetMapping("/account/{id}")
public Map<String,Object> account(@PathVariable("id") int id){
String sql = "select * from account where id = " + id;
return jdbcTemplate.queryForMap(sql);
}
//通过id更新
@GetMapping("/update/{id}")
private String update(@PathVariable("id") int id){
String sql = "update account set name = ? , money = ? where id = " + id;
Object[] objects = new Object[2];
objects[0] = "二哥哥";
objects[1] = "199";
jdbcTemplate.update(sql,objects);
return "更新完毕!";
}
}
运行结果: