SpringBoot怎么整合JdbcTemplate
作者:SunnyJourney
时间:2023-05-13
浏览:0
前言Spring对数据库的操作在jdbc上面做了更深层次的封装,而JdbcTemplate便是Spring提供的一个操作数据库的便捷工具。我们可以借助JdbcTemplate来执行所有数据库操作,例如插入,更新,删除和从数据库中检索数据,并且有效避免直接使用jdbc带来的繁琐编码初始化SpringBoot项目使用IDEA创建项目点击File–>New–>Project点击springinitializr,注意自己的SDK版本,再点击next填写自己的group、artifact,注
本文目录
前言
Spring对数据库的操作在jdbc上面做了更深层次的封装,而JdbcTemplate便是Spring提供的一个操作数据库的便捷工具。我们可以借助JdbcTemplate来执行所有数据库操作,例如插入,更新,删除和从数据库中检索数据,并且有效避免直接使用jdbc带来的繁琐编码
初始化SpringBoot项目
使用IDEA创建项目
点击File–>New–>Project

点击spring initializr,注意自己的SDK版本,再点击next

填写自己的group、artifact,注意Java version的版本和前面的SDK版本一致,点击next

选择自己要添加的依赖,项目创建的时候会自动加入到maven中,然后
点击next

填写自己的项目名,点击finish

导入JDBC依赖
org.springframework.boot spring-boot-starter-data-jdbc
导入数据库驱动
我这里使用的MySQL数据库,就导入MySQL数据库驱动
mysql mysql-connector-java 5.1.42
修改配置文件
我这里使用的是yml格式配置文件
spring: datasource: url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8 username: root password: admin driver-class-name: com.mysql.jdbc.Driver
数据库sys_user表结构

测试类代码
查询sys_user表数据量
package com.gavin.boot;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
@Slf4j
@SpringBootTest
class BootJdbcApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
//获取sys_user表数据量
Long aLong = jdbcTemplate.queryForObject("select count(*) from sys_user", Long.class);
log.info("记录总数:{}", aLong);
}运行结果

查询sys_user表一条数据
package com.gavin.boot;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
@Slf4j
@SpringBootTest
class BootJdbcApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
//获取sys_user表一条数据
Map map = jdbcTemplate.queryForMap("select * from sys_user where id = 1");
log.info("map数据为:" + map);
}
} 运行结果

查询sys_user表所有数据
package com.gavin.boot;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
@Slf4j
@SpringBootTest
class BootJdbcApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
//获取sys_user表所有数据
List新增sys_user表一条数据
package com.gavin.boot;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
@Slf4j
@SpringBootTest
class BootJdbcApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
//新增一条数据
int i = jdbcTemplate.update("insert into sys_user(user_name, phone) values(?,?)", new String[]{"小王", "13100720084"});
log.info("新增了" + i + "条数据");
}
}运行结果


修改sys_user表一条数据
package com.gavin.boot;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
@Slf4j
@SpringBootTest
class BootJdbcApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
//修改一条数据
int i2 = jdbcTemplate.update("update sys_user set user_name = ? where id = 1", new String[]{"小张"});
log.info("修改了" + i2 + "条数据");
}
}运行结果


删除sys_user表一条数据
package com.gavin.boot;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
@Slf4j
@SpringBootTest
class BootJdbcApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
//删除一条数据
int i3 = jdbcTemplate.update("delete from sys_user where id = ?", 1);
log.info("删除了" + i3 + "条数据");
}
}运行结果


作者最新文章
动漫共和国资源入口及正版地址分享
2026-11-04 12:05
xshell端口ssh SSH隧道建立方法
2026-11-03 10:11
CorelDRAW是免费软件吗?详解试用、订阅与一次性购买收费模式
2026-10-30 19:32
PDF怎么免费转成Word并保留内容?转换前后检查要点
2026-10-30 17:50
ArchiveofOurOwn小说快速入口链接
2026-10-30 10:30
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
























