jdbcTemplate查询的三种回调

xiaoxiao2021-02-28  101

首先我们先了解一下什么时回调?

从维基百科上面摘抄定义:(看完本篇文章再来理解这个定义) 在计算机程序设计中,回调函数,或简称回调,是指通过函数参数传递到其它代码的,某一块可执行代码的引用。这一设计允许了底层代码调用在高层定义的子程序。 也就是说底层封装的时候预留了回调函数接口,程序员可以可以通过底层封装的接口回调自己写的方法。 下面是例子。

首先定义一个类Caller,按照上面的定义就是程序员A写的程序a,这个类里面保存一个接口引用。 public class Caller { private MyCallInterface callInterface; public Caller() { } public void setCallFunc(MyCallInterface callInterface) { this.callInterface = callInterface; } public void call() { callInterface.printName(); } }

2.当然需要接口的定义,为了方便程序员B根据我的定义编写程序实现接口。

public interface MyCallInterface { public void printName(); } 3. 第三是定义程序员B写的程序b 第三是定义程序员B写的程序b public class Client implements MyCallInterface { @Override public void printName() { System.out.println("This is the client printName method"); } } 4. 测试如下 public class Test { public static void main(String[] args) { Caller caller = new Caller(); caller.setCallFunc(new Client()); caller.call(); } } 看到这里应该明白什么是回调了,有些文章介绍的很好,但是刚开始没看明白,是因为把第3步的类省略,直接写成匿名类了。 5. 在测试方法中直接使用匿名类,省去第3步。 public class Test { public static void main(String[] args) { Caller caller = new Caller(); // caller.setCallFunc(new Client()); caller.setCallFunc(new MyCallInterface() { public void printName() { System.out.println("This is the client printName method"); } }); caller.call(); } }

那么接下来让我们聊一下jdbctemplate的三个回调函数

“` org.springframework.jdbc.core.ResultSetExtractor.  基本上属于JdbcTemplate内部使用的Callback接口,相对于下面两个Callback接口来说,ResultSetExtractor拥有更多的控制权,因为使用它,你需要自行处理ResultSet:

public interface ResultSetExtractor { Object extractData(ResultSet rs) throws SQLException, DataAccessException; }

在直接处理完ResultSet之后,你可以将处理后的结果以任何你想要的形式包装后返回。 org.springframework.jdbc.core.RowCallbackHandler.  RowCallbackHandler相对于ResultSetExtractor来说,仅仅关注单行结果的处理,处理后的结果可以根据需要存放到当前RowCallbackHandler对象内或者使用JdbcTemplate的程序上下文中,当然,这个完全是看个人爱好了。 RowCallbackHandler的定义如下:

public interface RowCallbackHandler { void processRow(ResultSet rs) throws SQLException; }

org.springframework.jdbc.core.RowMapper.  ResultSetExtractor的精简版,功能类似于RowCallbackHandler,也只关注处理单行的结果,不过,处理后的结果会由ResultSetExtractor实现类进行组合。 RowMapper的接口定义如下:

public interface RowMapper { Object mapRow(ResultSet rs, int rowNum) throws SQLE““` ception; }

“`为了说明这三种Callback接口的使用和相互之间的区别,我们暂且设定如下场景: 数据库表customer中存在多行信息,对该表查询后,我们需要将每一行的顾客信息都映射到域对象Customer中,并以Java.util.List的形式返回所有的查询结果。 现在,我们分别使用这三种Callback接口对customer表进行查询:

List customerList = (List)jdbcTemplate.query(“select * from customer”, new ResultSetExtractor(){

public Object extractData(ResultSet rs) throws SQLException,DataAccessException { List customers = new ArrayList(); while(rs.next()) { Customer customer = new Customer(); customer.setFirstName(rs.getString(1)); customer.setLastName(rs.getString(2)); ... customers.add(customer); } return customers; }});

List customerList = jdbcTemplate.query(“select * from customer”, new RowMapper(){

public Object mapRow(ResultSet rs, int rowNumber) throws SQLException { Customer customer = new Customer(); customer.setFirstName(rs.getString(1)); customer.setLastName(rs.getString(2)); ... return customer; }});

final List customerList = new ArrayList();

jdbcTemplate.query(“select * from customer”, new RowCallbackHandler(){

public void processRow(ResultSet rs) throws SQLException { Customer customer = new Customer(); customer.setFirstName(rs.getString(1)); customer.setLastName(rs.getString(2)); ... customerList.add(uery方法返回Object型结果,要使用查询结果,我们需要对其进行强制转型;

以RowMapper接口作为方法参数的query方法直接返回List型的结果; 以RowCallbackHandler作为方法参数的query方法,返回值为void; 使用ResultSetExtractor作为Callback接口处理查询结果,我们需要自己声明集合类,自己遍历ResultSe 使用ResultSetExtractor作为Callback接口处理查询结果,我们需要自己声明集合类,自己遍历

使用ResultSetExtractor作为Callback接口处理查询结果,我们需要自己声明集合类,自己遍历ResultSet,自己根据每行数据组装Customer对象,自己将组装后的Customer对象添加到集合类中,方法最终只负责将组装完成的集合返回;

转载请注明原文地址: https://www.6miu.com/read-26951.html

最新回复(0)