Spring’s database operations are encapsulated in a deeper way on jdbc, and JdbcTemplate is a convenient tool provided by Spring to operate the database. . We can use JdbcTemplate to perform all database operations, such as inserting, updating, deleting and retrieving data from the database, and effectively avoiding the tedious coding caused by directly using jdbc
Click File–>New–>Project
Click spring initializr, pay attention to your SDK version, and then click next
Fill in your own group and artifact. Note that the Java version is consistent with the previous SDK version. Click next
to choose yourself The dependencies to be added will be automatically added to maven when the project is created, then
click next
Fill in your project name and click finish
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency>
The MySQL database I use here, just import the MySQL database driver
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency>
I am using the yml format configuration file here
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); }
Running results
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); } }
Running results
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 + "条数据"); } }
Run results
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 + "条数据"); } }
Run results
##Delete sys_user Table of datapackage 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 + "条数据"); } }
The above is the detailed content of How SpringBoot integrates JdbcTemplate. For more information, please follow other related articles on the PHP Chinese website!