Spring Boot RowMapper教程展示了如何将ResultSet的行映射到数据载体。我们使用Java记录作为数据载体。对于本教程,我们需要JDK 14并启用预览功能。
Java记录是类的受限形式。Java的记录消除大量的样板代码,如构造器,getters,toString, hashCode和equals方法。他们是不变的。其目的是让对象成为简单的数据载体。
项目结构:
pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ │ MyRunner.java │ │ ├───mapper │ │ │ CityMapper.java │ │ ├───model │ │ │ City.java │ │ └───service │ │ CityService.java │ │ ICityService.java │ └───resources │ application.properties │ data-h2.sql │ schema-h2.sql └───test └───java
Maven 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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>SpringBootRowMapper</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <java.version>14</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> </parent> <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>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>14</release> <compilerArgs> --enable-preview </compilerArgs> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
RowMapper寓于 spring-boot-starter-jdbc。在maven-compiler-plugin 配置中,我们启用预览功能。
City记录:删除了典型Java模型类的大多数模板。
public record City(Long id, String name, Integer population) { }
对应数据表结构:
CREATE TABLE cities(id BIGINT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), population BIGINT);
下面CityMapper结果集的一行映射到City 记录。
public class CityMapper implements RowMapper<City> { @Override public City mapRow(ResultSet rs, int rowNum) throws SQLException { return new City(rs.getLong("id"), rs.getString("name"), rs.getInt("population")); } }
将CityMapper结果集的一行映射到City 记录。由于Java记录是不可变的,并且不遵循Java Beans规范,因此我们不能使用BeanPropertyRowMapper;。我们必须创建自己的映射器。
调用:
@Service public class CityService implements ICityService { @Autowired private JdbcTemplate jtm; @Override public List<City> findAll() { String sql = "SELECT * FROM cities"; return jtm.query(sql, new CityMapper()); } @Override public City findById(Long id) { String sql = "SELECT * FROM cities WHERE id = ?"; return jtm.queryForObject(sql, new Object[]{id}, new CityMapper()); } }