【Spring Boot】008

中国的365体育投注 📅 2025-10-25 21:35:02 👤 admin 👁️ 6620 ❤️ 423
【Spring Boot】008

一、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

二、整合JDBC1、创建新Spring Boot项目第一步:填写基本信息第二步:勾选JDBC API和MySQL Driver即可2、代码演示application.yaml配置文件:代码语言:javascript代码运行次数:0运行复制spring:

datasource:

username: root

password: zibo123456

url: jdbc:mysql://localhost:3306/zibo?serverTimezone=UTC

driver-class-name: com.mysql.cj.jdbc.Driver测试类StudySpringDataApplicationTests代码:代码语言:javascript代码运行次数:0运行复制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依赖代码语言:javascript代码运行次数:0运行复制

org.springframework.boot

spring-boot-starter-web

第二步:创建JDBCController类代码语言:javascript代码运行次数:0运行复制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> accountList(){

String sql = "select * from account";

return jdbcTemplate.queryForList(sql);

}

}运行结果:4、测试通过id查询第一步:修改DBCController类代码语言:javascript代码运行次数:0运行复制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> accountList(){

String sql = "select * from account";

return jdbcTemplate.queryForList(sql);

}

//通过id查询

@GetMapping("/account/{id}")

public Map account(@PathVariable("id") int id){

String sql = "select * from account where id = " + id;

return jdbcTemplate.queryForMap(sql);

}

}运行结果:5、测试通过id更新第一步:修改DBCController类代码语言:javascript代码运行次数:0运行复制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> accountList(){

String sql = "select * from account";

return jdbcTemplate.queryForList(sql);

}

//通过id查询

@GetMapping("/account/{id}")

public Map 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 "更新完毕!";

}

}运行结果:

相关推荐

为什么要传递正能量
www365bet娱乐场

为什么要传递正能量

📅 10-25 👁️ 6015
交行的网上银行怎么开通
office365打不开

交行的网上银行怎么开通

📅 09-15 👁️ 2710
网吧包间里的青春:80元一晚的都市避风港
www365bet娱乐场

网吧包间里的青春:80元一晚的都市避风港

📅 10-23 👁️ 1778