spring对数据库的操作在jdbc上面做了更深层次的封装,而jdbctemplate便是spring提供的一个操作数据库的便捷工具。我们可以借助jdbctemplate来执行所有数据库操作,例如插入,更新,删除和从数据库中检索数据,并且有效避免直接使用jdbc带来的繁琐编码
点击File–>New–>Project

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

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

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

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>我这里使用的MySQL数据库,就导入MySQL数据库驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>我这里使用的是yml格式配置文件
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8
username: root
password: admin
driver-class-name: com.mysql.jdbc.Driver
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);
}运行结果

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<String, Object> map = jdbcTemplate.queryForMap("select * from sys_user where id = 1");
log.info("map数据为:" + map);
}
}运行结果

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<Map<String, Object>> list = jdbcTemplate.queryForList("select * from sys_user");
log.info("list数据为:" + list);
}
}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 + "条数据");
}
}运行结果


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 + "条数据");
}
}运行结果


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 + "条数据");
}
}运行结果


以上就是SpringBoot怎么整合JdbcTemplate的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号