JdbcTemplate BeanPropertyRowMapper 使用
所属分类 JdbcTemplate
浏览量 86
spring-boot-2.7.10
spring-jdbc-5.3.26
org.springframework.jdbc.core.JdbcTemplate#query(java.lang.String, org.springframework.jdbc.core.RowMapper)
public < T> List< T> query(String sql, RowMapper< T> rowMapper) throws DataAccessException {
org.springframework.jdbc.core.BeanPropertyRowMapper
BeanPropertyRowMapper implements RowMapper
字段名一致 或者 驼峰 下划线 转换后名字一致都可以正确映射 ,忽略大小写
属性名 userName 可以匹配 USERNAME 或 user_name
List< Pet> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper< >(Pet.class));
public class Pet {
private Long pet_id;
private String petName;
private Integer petAge;
// get set ...
}
@RestController
public class JdbcTempleteController {
@Resource
JdbcTemplate jdbcTemplate;
@Resource
DataSource dataSource;
@RequestMapping("/jt/query")
public Object query(String sql) {
List
http://127.0.0.1:8090/jt/update?sql=create table pet(id bigint ,name varchar(16),age int)
http://127.0.0.1:8090/jt/query?sql=show tables
http://127.0.0.1:8090/jt/update?sql=insert into pet(id,name,age) values(1,'pet1',1)
http://127.0.0.1:8090/jt/update?sql=insert into pet(id,name,age) values(2,'pet2',2)
http://127.0.0.1:8090/jt/queryPet?sql=select * from pet
查询字段和结果对象字段名不匹配 返回都是null
以下查询sql ,结果ok
select id as pet_id,name as petName,age as PET_AGE from pet
select id as pet_id,name as pet_name,age as PET_AGE from pet
select id as pet_id,name as pet_name,age as PETAGe from pet
https://gitee.com/dyyx/work2024/blob/master/demo/appdemo/src/main/java/demo/controller/JdbcTempleteController.java
上一篇
下一篇
vue3 vite 和 Vue-CLI
RuoYi-Vue3 登录 前端代码笔记
vue3 技术栈
家常炒挂面
JPA常用注解
Springboot 过滤器 拦截器 全局异常处理