对于数据访问层,无论是 SQL(关系型数据库) 还是 NOSQL(非关系型数据库),Spring Boot 底层都是采用 Spring Data 的方式进行统一处理。
首先先建一个项目,引入相对应的模板! 如下图:
引入模板后,打开pom查看,即可看到如下代码:(表示已成功导入模板成功 , 否则 ,点击 ->下载资源后->再刷新一下,即可!)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
在资源包(resources)中创建application.yaml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
数据库代码如下:(全选即可运行!)
create database mybatis;
use mybatis;
create table user(
id int(20) not null primary key,
name varchar(30) default null,
pwd varchar(30) default null
)engine=innodb default charset=utf8;
insert into user (id,name,pwd) VALUES
(1,'张三','123'),
(2,'李四','456'),
(3,'王五','852');
在测试类(test)中点开xxxApplicationTests:(添加如下代码)
package com.jin;
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 Springboot05DataApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
点击(测试类)运行,若没报错 即可看到数据源和数据库连接情况!效果图如下:
与启动器(xxxApplication.java )的同级创建一个包(controller),包下有类(JDBCController.java)。结构如下:
在JDBCController 中添加如下代码:
package com.jin.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class JDBCController {
@Autowired
JdbcTemplate jdbcTemplate;
@RequestMapping("/userList")
public List<Map<String, Object>> userList() {
String sql = "select * from mybatis.user";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
return maps;
}
@RequestMapping("/addUser")
public String addUser() {
String sql = "insert into mybatis.user(id,name,pwd) values (5,'小花','741')";
jdbcTemplate.update(sql);
return "insert-ok";
}
@RequestMapping("/updateUser/{id}")
public String updateUser(@PathVariable("id") int id) {
String sql = "update mybatis.user set name=?,pwd=? where id=" + id;
Object[] objects = new Object[2];
objects[0] = "小明2";
objects[1] = "zz";
jdbcTemplate.update(sql,objects);
return "update-ok";
}
@RequestMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id") int id) {
String sql = "delete from mybatis.user where id=?";
jdbcTemplate.update(sql,id);
return "delete-ok";
}
}
运行Springboot中的启动器,快捷键(Shift+F10):
效果图如下:(修改相对于的url,即可实现数据库的增删改查功能!)
|