回调

JdbcTemplate类支持的回调类:

1.预编译语句及存储过程创建回调:用于根据JdbcTemplate提供的连接创建相应的语句;

1.1 PreparedStatementCreator

<T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T> action)

PreparedStatementCreator:通过回调获取JdbcTemplate提供的Connection,由用户使用该Conncetion创建相关的PreparedStatement;

Integer count = jdbcTemplate.execute(
                new PreparedStatementCreator() {

                    @Override
                    public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                        return con.prepareStatement("SELECT count(1) FROM student");//**拓展点1,改写sql
                    }
                },
                new PreparedStatementCallback<Integer>() {
                    @Override
                    public Integer doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                        ResultSet resultSet = ps.executeQuery();
                        resultSet.next();
                        return resultSet.getInt(1);//**拓展点2,改写返回值
                    }
                });

1.2 PreparedStatementCreator 处理存储过程

2.预编译语句设值回调:用于给预编译语句相应参数设值;

2.1 PreparedStatementSetter:

public int update(String sql, PreparedStatementSetter pss) throws DataAccessException

通过回调获取JdbcTemplate提供的PreparedStatement,由用户来对相应的预编译语句相应参数设值;

jdbcTemplate.update("INSERT INTO student(id,name) VALUES (?,?)", new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setInt(1, 11);
                ps.setString(2, "小张");
            }
});

此拓展点可以设置SQL的参数值

2.2BatchPreparedStatementSetter:;

类似于PreparedStatementSetter,但用于批处理,需要指定批处理大小;

自定义功能回调:提供给用户一个扩展点,用户可以在指定类型的扩展点执行任何数量需要的操作;

     ConnectionCallback:通过回调获取JdbcTemplate提供的Connection,用户可在该Connection执行任何数量的操作;
     StatementCallback:通过回调获取JdbcTemplate提供的Statement,用户可以在该Statement执行任何数量的操作;
     PreparedStatementCallback:通过回调获取JdbcTemplate提供的PreparedStatement,用户可以在该PreparedStatement执行任何数量的操作;
     CallableStatementCallback:通过回调获取JdbcTemplate提供的CallableStatement,用户可以在该CallableStatement执行任何数量的操作;

结果集处理回调:通过回调处理ResultSet或将ResultSet转换为需要的形式;

     RowMapper:用于将结果集每行数据转换为需要的类型,用户需实现方法mapRow(ResultSet rs, int rowNum)来完成将每行数据转换为相应的类型。
     RowCallbackHandler:用于处理ResultSet的每一行结果,用户需实现方法processRow(ResultSet rs)来完成处理,在该回调方法中无需执行rs.next(),该操作由JdbcTemplate来执行,用户只需按行获取数据然后处理即可。
     ResultSetExtractor:用于结果集数据提取,用户需实现方法extractData(ResultSet rs)来处理结果集,用户必须处理整个结果集;

Spring JdbcTemplate中的回调的更多相关文章

  1. Spring JdbcTemplate中关于RowMapper的使用实例

    在spring boot 集成使用jdbctemplate,首先在pom文件中引入相应的依赖 <dependency> <groupId>org.springframework ...

  2. 设计模式学习笔记(十五)命令模式及在Spring JdbcTemplate 中的实现

    命令(Command)模式是指将请求封装成为一个对象,使发出请求和执行请求的责任分割开,方便将命令对象进行存储.传递.调用.增加与管理. 也就是将发送者.接收者和调用命令封装成独立的对象,来供客户端调 ...

  3. 【sping揭秘】19、关于spring中jdbctemplate中的DataSource怎么来呢

    我们这是可以正好借助之前学的factorybean类,自己吧jdbctemplate加载到spring容器中,我们可以封装多个这种对象,那么可以实现针对不同的数据库的jdbctemplate 首先我们 ...

  4. (转)Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

  5. Spring JdbcTemplate源码阅读报告

    写在前面 spring一直以删繁就简为主旨,所以设计出非常流行的bean管理模式,简化了开发中的Bean的管理,少写了很多重复代码.而JdbcTemplate的设计更令人赞叹,轻量级,可做ORM也可如 ...

  6. Spring JdbcTemplate 与 事务管理 学习

    Spring的JDBC框架能够承担资源管理和异常处理的工作,从而简化我们的JDBC代码, 让我们只需编写从数据库读写数据所必需的代码.Spring把数据访问的样板代码隐藏到模板类之下, 结合Sprin ...

  7. Spring JdbcTemplate操作小结

    Spring 提供了JdbcTemplate 来封装数据库jdbc操作细节: 包括: 数据库连接[打开/关闭] ,异常转义 ,SQL执行 ,查询结果的转换 使用模板方式封装 jdbc数据库操作-固定流 ...

  8. Spring JdbcTemplate

    参考链接: https://my.oschina.net/u/437232/blog/279530 http://jinnianshilongnian.iteye.com/blog/1423897 J ...

  9. Spring JdbcTemplate 方法详解

    JdbcTemplate主要提供以下五类方法: execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句: update方法及batchUpdate方法:update方法用于执行新增.修 ...

随机推荐

  1. 为什么我的电脑win系统没有便笺功能?为什么我在开始菜单里找不到便笺功能?

    有些网友表示,为什么我的电脑没有便笺功能?为什么我在开始菜单里找不到便笺功能? 从问题可以基本判断出来,这些网友使用的Win7版本有可能是买笔记本或者台式电脑时预装的Win7家庭普通版或者Win7精简 ...

  2. Android广播接收器里弹出对话框

    不多说,直接上车... public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(fina ...

  3. 使用docker安装myql/redis等软件

    使用docker安装myql/redis等软件 概述 基本命令 安装mysql 安装redis 概述 在开发时经常需要安装各种软件,有时甚至为了验证一个命令不得不安装配置一个缓存.数据库.MQ等,耽误 ...

  4. Linux更改主机名

    1.临时 # hostname newhostname 2.修改/etc/hostname文件,需重启 # vim /etc/hostname 3.查看 # hostname Ubuntu18 # h ...

  5. 简说Spring事务

    一.事务定义: 事务指逻辑上的一组操作,这组操作要么全部成功,要么全部失败. 二.事务的特性: 1. 原子性 - 指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. 2. 一致性 ...

  6. URAL 1635 Mnemonics and Palindromes

    URAL 1635 思路:区间dp+贪心,先n^2处理出每段区间是否是回文串,然后贪心地找每一段1到i的最少分割. 代码: #include<bits/stdc++.h> using na ...

  7. git/ssh备查文档

    配置多个ssh key: 待更新 git速查表: git remote set-url origin(远程仓库名称) https://xxxxx/ProjectName.git  从ssh切换至htt ...

  8. 修改unity变量名但不丢失序列化值

    using UnityEngine; using UnityEngine.Serialization; public class LgsTest : MonoBehaviour { [Formerly ...

  9. m_Orchestrate learning system---三十五、php数据和js数据的解耦:php数据(php代码)不要放到js代码中

    m_Orchestrate learning system---三十五.php数据和js数据的解耦:php数据(php代码)不要放到js代码中 一.总结 一句话总结:也就是以html为中介,用html ...

  10. B2B、B2C、C2C、O2O 和 P2P 的含义

    B2C(Business-to-Customer)商家对客户 我开一家公司卖东西,你来买,即B2C.生活中常用的比如我们经常在天猫旗舰店上面购物,天猫入驻的都是商家,而我们买东西的就是客户,这就是B2 ...