SpringBoot怎么整合JdbcTemplate
前言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 + "条数据");
}
}运行结果


Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















