Spring JdbcTemplate
是JDBC核心包中的中心类。它简化了 JDBC 与 Spring 的使用,并有助于避免常见错误。在此页面上,我们将学习使用它的queryForObject
方法。
JdbcTemplate.queryForObject不同参数的方法。
1.
<T> T queryForObject(String sql, Class<T> requiredType) throws DataAccessException
执行给定的 SQL 并返回所需类型的对象。
2.
<T> T queryForObject(String sql, Class<T> requiredType, @Nullable Object... args) throws DataAccessException
查询给定的 SQL 以从 SQL 创建预准备语句和要绑定到查询的参数列表,需要一个结果对象。
3.
<T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType) throws DataAccessException
查询给定的 SQL 以从 SQL 创建预准备语句和要绑定到查询的参数列表,需要一个结果对象。
4.
<T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper) throws DataAccessException
查询给定的 SQL,用于从 SQL 创建预准备语句和要绑定到查询的参数列表,通过 RowMapper将单个结果行映射到结果对象。
5.
<T> T queryForObject(String sql, RowMapper<T> rowMapper) throws DataAccessException
执行给定静态 SQL 的查询,通过RowMapper将单个结果行映射到结果对象。
6.
<T> T queryForObject(String sql, RowMapper<T> rowMapper, @Nullable Object... args) throws DataAccessException
查询给定 SQL 以从 SQL 创建预准备语句和要绑定到查询的参数列表,通过RowMapper将单个结果行映射到结果对象。
参数:
sql是要执行的 SQL 查询。
requiredType 是结果对象应匹配的类型。
args 是要绑定到查询的参数。
argType是参数的 SQL 类型java.sql.Types。
rowMapper是一个回调,它将每行映射一个对象
返回:
结果对象,如果是 SQL NULL,则为 null。
抛出:
如果执行查询时遇到任何问题,则抛出DataAccessException。
使用Spring Boot的示例
PersonDAO.java
package com.concretepage;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class PersonDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
// queryForObject(String sql, Class<T> requiredType)
public int getPersonCount() {
String sql = "select count(*) from person";
return jdbcTemplate.queryForObject(sql, Integer.class);
}
// queryForObject(String sql, Class<T> requiredType, Object... args)
public String getNameById(int id) {
String sql = "select name from person where id = ?";
return jdbcTemplate.queryForObject(sql, String.class, 1);
}
// queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType)
public int getPersonCountByNameAndAge(String name, int age) {
String sql = "select count(*) from person where name = ? and age = ?";
Object[] args = {name, age};
int[] argTypes = {java.sql.Types.CHAR, java.sql.Types.INTEGER};
return jdbcTemplate.queryForObject(sql, args, argTypes, Integer.class);
}
// queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
public Person getPersonsById(int id) {
String sql = "select * from person where id = ?";
Object[] args = {id};
int[] argTypes = {java.sql.Types.INTEGER};
return jdbcTemplate.queryForObject(sql, args, argTypes, new RowMapper<Person>() {
@Override
public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
Person p = new Person(rs.getString(2), rs.getInt(3));
p.setId(rs.getInt(1));
return p;
}
});
}
// queryForObject(String sql, RowMapper<T> rowMapper)
public Person getAdmin() {
String sql = "select * from person where id = 1";
return jdbcTemplate.queryForObject(sql, new RowMapper<Person>() {
@Override
public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
Person p = new Person(rs.getString(2), rs.getInt(3));
p.setId(rs.getInt(1));
return p;
}
});
}
// queryForObject(String sql, RowMapper<T> rowMapper, Object... args)
public int getPersonCount2(String name, int age) {
String sql = "select count(*) from person where name = ? and age = ?";
return jdbcTemplate.queryForObject(sql, new RowMapper<Integer>() {
@Override
public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getInt(1);
}
}, name, age);
}
}
Person.java
package com.concretepage;
public class Person {
private int id;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/concretepage?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.concretepage</groupId>
<artifactId>soap-ws</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-demo</name>
<description>Spring Demo Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath />
</parent>
<properties>
<java.version>16</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
MyApplication.java
package com.concretepage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(MyApplication.class, args);
PersonDAO personDAO = ctx.getBean(PersonDAO.class);
System.out.println("Total Count: " + personDAO.getPersonCount());
System.out.println(personDAO.getNameById(1));
System.out.println("Count: " + personDAO.getPersonCountByNameAndAge("Mohan", 25));
Person p = personDAO.getPersonsById(1);
System.out.println(p.getId() + " - " + p.getName() + " - " + p.getAge());
Person admin = personDAO.getAdmin();
System.out.println(admin.getId() + " - " + admin.getName() + " - " + admin.getAge());
System.out.println("Count: " + personDAO.getPersonCount2("Mohan", 25));
}
}
查找我们的演示中使用的 MySQL 表(人)屏幕截图。
输出。
Total Count: 3
Rakesh
Count: 2
1 - Rakesh - 30
1 - Rakesh - 30
Count: 2
参考
类 Jdbc 模板
下载源代码