资讯专栏INFORMATION COLUMN

关于 Spring JdbcTemplate 的一些总结

Pikachu / 436人阅读

摘要:关于的一些总结一个小问题的思考起因当前项目中一直使用的都是,即这种用法考虑到确实有一定的局限性,在部分查询中使用到了进行复杂查询操作由于本人年也曾使用过,古语温故而知新,所以做此总结梳理。

关于 Spring JdbcTemplate 的一些总结 一个小问题的思考 起因

当前项目中一直使用的都是 SpringData JPA ,即 public interface UserRepository extends JpaRepository 这种用法;

考虑到 SpringData JPA 确实有一定的局限性,在部分查询中使用到了 JdbcTemplate 进行复杂查询操作;
由于本人16年也曾使用过 JdbcTemplate,古语温故而知新,所以做此总结梳理。

首先列出同事的做法:

public class xxx{

    xxx method(){
        ...
        List list = jdbcTemplate.query(sql, new WishDTO());
        ...
    }
}

@Data
public class WishDTO implements RowMapper, Serializable {
    String xxx;
    Long xxx;
    Date xxx;
    BigDecimal xxx;

    @Override
    public WishDTO mapRow(ResultSet rs, int rowNum) {
        WishDTO dto = new WishDTO();
        Field[] fields = dto.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                field.set(dto, rs.getObject(field.getName()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return dto;
    }

}
个人愚见

个人感觉让 WishDTO 再实现实现一遍 RowMapper 有点麻烦,毕竟 WishDTO 实体类的所有字段都是需要赋值的,并没有定制化需求。

所以想着有没有更好地写法,然后就翻了一下 jdbcTemplate 的方法,找到了一个自认为满足自己这个需求的方法:

public  List queryForList(String sql, Class elementType)
即 将代码改为:
public class xxx{

    xxx method(){
        ...
        List list = jdbcTemplate.queryForList(sql, WishDTO.class);
        ...
    }
}

@Data
public class WishDTO implements Serializable {
    String xxx;
    Long xxx;
    Date xxx;
    BigDecimal xxx;
}

一切看起来都很完美,但执行却报错了:Incorrect column count: expected 1, actual 13

思考

经过一番对源码进行debug,结合网上的一些资料,大概知道了是什么原因了,分析如下;

    public  List queryForList(String sql, Class elementType) throws DataAccessException {
        return query(sql, getSingleColumnRowMapper(elementType));
    }

其本质是还是调用public List query(String sql, RowMapper rowMapper) ,只是将 Class elementType 封装成一个 RowMapper 实现实例;

    protected  RowMapper getSingleColumnRowMapper(Class requiredType) {
        return new SingleColumnRowMapper<>(requiredType);
    }

现在我们可以看一下 SingleColumnRowMapper 类的描述:

/**
 * {@link RowMapper} implementation that converts a single column into a single
 * result value per row. Expects to operate on a {@code java.sql.ResultSet}
 * that just contains a single column.
 *
 * 

The type of the result value for each row can be specified. The value * for the single column will be extracted from the {@code ResultSet} * and converted into the specified target type. */

其实从类名也可以看出,这是一个 RowMapper 的 简单实现,且仅能接收一个字段的数据,如 String.class 和 Integer.class 等基础类型;

网上的参考资料:https://blog.csdn.net/qq_4014...

解决方案

使用 BeanPropertyRowMapper 进行封装 ;
即 将代码改为:

public class xxx{

    xxx method(){
        ...
        List list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(WishDTO.class));
        ...
    }
}

@Data
public class WishDTO implements Serializable {
    String xxx;
    Long xxx;
    Date xxx;
    BigDecimal xxx;
}

接下来看一下 BeanPropertyRowMapper 的类描述:

/**
 * {@link RowMapper} implementation that converts a row into a new instance
 * of the specified mapped target class. The mapped target class must be a
 * top-level class and it must have a default or no-arg constructor.
 *
 * 

Column values are mapped based on matching the column name as obtained from result set * meta-data to public setters for the corresponding properties. The names are matched either * directly or by transforming a name separating the parts with underscores to the same name * using "camel" case. * *

Mapping is provided for fields in the target class for many common types, e.g.: * String, boolean, Boolean, byte, Byte, short, Short, int, Integer, long, Long, * float, Float, double, Double, BigDecimal, {@code java.util.Date}, etc. * *

To facilitate mapping between columns and fields that don"t have matching names, * try using column aliases in the SQL statement like "select fname as first_name from customer". * *

For "null" values read from the database, we will attempt to call the setter, but in the case of * Java primitives, this causes a TypeMismatchException. This class can be configured (using the * primitivesDefaultedForNullValue property) to trap this exception and use the primitives default value. * Be aware that if you use the values from the generated bean to update the database the primitive value * will have been set to the primitive"s default value instead of null. * *

Please note that this class is designed to provide convenience rather than high performance. * For best performance, consider using a custom {@link RowMapper} implementation. */

其作用就是讲一个Bean class 转化成相对应的 Bean RowMapper 实现类。

JdbcTemplate

https://docs.spring.io/spring...

Query
int rowCount = this.jdbcTemplate.queryForObject("select count(*) from t_actor", Integer.class);

int countOfActorsNamedJoe = this.jdbcTemplate.queryForObject("select count(*) from t_actor where first_name = ?", Integer.class, "Joe");

String lastName = this.jdbcTemplate.queryForObject("select last_name from t_actor where id = ?", new Object[]{1212L}, String.class);

Actor actor = this.jdbcTemplate.queryForObject(
        "select first_name, last_name from t_actor where id = ?",
        new Object[]{1212L},
        new RowMapper() {
            public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
                Actor actor = new Actor();
                actor.setFirstName(rs.getString("first_name"));
                actor.setLastName(rs.getString("last_name"));
                return actor;
            }
        });

List actors = this.jdbcTemplate.query(
        "select first_name, last_name from t_actor",
        new RowMapper() {
            public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
                Actor actor = new Actor();
                actor.setFirstName(rs.getString("first_name"));
                actor.setLastName(rs.getString("last_name"));
                return actor;
            }
        });

---

public List findAllActors() {
    return this.jdbcTemplate.query( "select first_name, last_name from t_actor", new ActorMapper());
}
private static final class ActorMapper implements RowMapper {
    public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
        Actor actor = new Actor();
        actor.setFirstName(rs.getString("first_name"));
        actor.setLastName(rs.getString("last_name"));
        return actor;
    }
}
Updating (INSERT, UPDATE, and DELETE)
this.jdbcTemplate.update(
        "insert into t_actor (first_name, last_name) values (?, ?)",
        "Leonor", "Watling");

this.jdbcTemplate.update(
        "update t_actor set last_name = ? where id = ?",
        "Banjo", 5276L);

this.jdbcTemplate.update(
        "delete from actor where id = ?",
        Long.valueOf(actorId));
Other
this.jdbcTemplate.execute("create table mytable (id integer, name varchar(100))");
NamedParameterJdbcTemplate

https://docs.spring.io/spring...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75400.html

相关文章

  • Spring Boot - 整合JdbcTemplate、MyBatis

    摘要:更简答的说就是要么全部执行成功,要么撤销不执行。因此,数据库操作的事务习惯上就称为事务。实现原理单机事务事务是用对象控制的。接口提供了两种事务模式自动提交和手工提交。事务多机事务,通过实现,需要驱动支持。局限于应用使用。 Spring Boot - 数据库配置 回顾 Spring Boot - 初识 Hello World Spring Boot - Servlet、过滤器、监听器、...

    Keagan 评论0 收藏0
  • 一起来学SpringBoot | 第五篇:使用JdbcTemplate访问数据库

    摘要:值得注意的是,默认会自动配置,它将优先采用连接池,如果没有该依赖的情况则选取,如果前两者都不可用最后选取。 SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程 Spring Framework对数据...

    ssshooter 评论0 收藏0
  • 慕课网_《轻松愉快之玩转SpringData》学习总结

    摘要:时间年月日星期一说明本文部分内容均来自慕课网。慕课网教学示例源码个人学习源码第一章课程介绍课程介绍什么是主旨提供一个熟悉的一致的,基于框架的数据访问框架。 时间:2017年04月24日星期一说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学示例源码:https://github.com/zccodere/s...个人学习源码:https://gith...

    skinner 评论0 收藏0
  • SpringBoot 与 Kotlin 完美交融

    摘要:环境依赖修改文件,添加依赖。使用为被标注的类去掉,允许被继承。数据源方案一使用默认配置使用默认配置,不需要在创建和的。相关为了展现效果,我们先定义一组简单的接口进行测试。 原文地址:梁桂钊的博客博客地址:http://blog.720ui.com 欢迎转载,转载请注明作者及出处,谢谢! 本文讲解 Spring Boot2 基础下,如何使用 Kotlin,并无缝整合与完美交融。为了让读...

    golden_hamster 评论0 收藏0
  • SpringSpring Boot和TestNG测试指南 - 测试关系型数据库

    摘要:地址提供了对的支持,能够让我们很方便对关系型数据库做集成测试。如果想要在打包的时候跳过集成测试,只需要。例子使用因为使用了来做集成测试,得益于其机制,不需要自己构建和的。 Github地址 Spring Test Framework提供了对JDBC的支持,能够让我们很方便对关系型数据库做集成测试。 同时Spring Boot提供了和Flyway的集成支持,能够方便的管理开发过程中产生...

    Meils 评论0 收藏0

发表评论

0条评论

Pikachu

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<