最近开始重新学习spring。在研究spring开发包自带的例子jpetstore时,遇到了匿名类的使用,感觉新鲜,于是择其一二处,以供日后反刍。
在 org.springframework.orm.ibatis 包中,有一个类叫SqlMapClientTemplate,它是用spring整合iBatis对数据库进行操作的一个工具类。浏览一下,其主要提供了对数据库的各种“增查改删”的操作。
增:
public Object insert(String statementName) throws DataAccessException {
return insert(statementName, null);
}
public Object insert(final String statementName, final Object parameterObject) throws DataAccessException {
return execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
return executor.insert(statementName, parameterObject);
}});
}
查:
public List queryForList(String statementName) throws DataAccessException {
return queryForList(statementName, null);
}
public List queryForList(final String statementName, final Object parameterObject)
throws DataAccessException {
return executeWithListResult(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
return executor.queryForList(statementName, parameterObject);
}
});
}
改:
public int update(String statementName) throws DataAccessException {
return update(statementName, null);
}
public int update(final String statementName, final Object parameterObject)
throws DataAccessException {
Integer result = (Integer) execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
return new Integer(executor.update(statementName, parameterObject));
}
});
return result.intValue();
}
删:
public int delete(String statementName) throws DataAccessException {
return delete(statementName, null);
}
public int delete(final String statementName, final Object parameterObject)
throws DataAccessException {
Integer result = (Integer) execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
return new Integer(executor.delete(statementName, parameterObject));
}
});
return result.intValue();
}
从以上诸方法可以看出,在spring中使用了匿名类,使这一经典框架代码更加简洁美观。如果仅仅是代码的简洁美观,不能用,那就这样煞费苦心的设计就成了花拳绣腿了。我们可以注意到,以上所有的代码中所有的匿名类都实现了一个接口,那就是同一包(org.springframework.orm.ibatis)下的SqlMapClientCallback。这个接口只有一个方法,
Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException;
上述匿名类实现了这一接口,重写了上述方法。 之后工具类SqlMapClientTemplate利用
public Object execute(SqlMapClientCallback action) throws DataAccessException
调用了匿名类实现的接口方法
doInSqlMapClient。代码如下:
public Object execute(SqlMapClientCallback action) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
Assert.notNull(this.sqlMapClient, "No SqlMapClient specified");
// We always needs to use a SqlMapSession, as we need to pass a Spring-managed
// Connection (potentially transactional) in. This shouldn't be necessary if
// we run against a TransactionAwareDataSourceProxy underneath, but unfortunately
// we still need it to make iBATIS batch execution work properly: If iBATIS
// doesn't recognize an existing transaction, it automatically executes the
// batch for every single statement...
SqlMapSession session = this.sqlMapClient.openSession();
if (logger.isDebugEnabled()) {
logger.debug("Opened SqlMapSession [" + session + "] for iBATIS operation");
}
Connection ibatisCon = null;
try {
Connection springCon = null;
DataSource dataSource = getDataSource();
boolean transactionAware = (dataSource instanceof TransactionAwareDataSourceProxy);
// Obtain JDBC Connection to operate on...
try {
ibatisCon = session.getCurrentConnection();
if (ibatisCon == null) {
springCon = (transactionAware ?
dataSource.getConnection() : DataSourceUtils.doGetConnection(dataSource));
session.setUserConnection(springCon);
if (logger.isDebugEnabled()) {
logger.debug("Obtained JDBC Connection [" + springCon + "] for iBATIS operation");
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Reusing JDBC Connection [" + ibatisCon + "] for iBATIS operation");
}
}
}
catch (SQLException ex) {
throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
}
// Execute given callback...
try {
return action.doInSqlMapClient(session);
}
catch (SQLException ex) {
throw getExceptionTranslator().translate("SqlMapClient operation", null, ex);
}
finally {
try {
if (springCon != null) {
if (transactionAware) {
springCon.close();
}
else {
DataSourceUtils.doReleaseConnection(springCon, dataSource);
}
}
}
catch (Throwable ex) {
logger.debug("Could not close JDBC Connection", ex);
}
}
// Processing finished - potentially session still to be closed.
}
finally {
// Only close SqlMapSession if we know we've actually opened it
// at the present level.
if (ibatisCon == null) {
session.close();
}
}
}
注意此段代码中46行return action.doInSqlMapClient(session);
分享到:
相关推荐
spring 2.5.6源码 把其中的文档部分给删了,其他都是发布版本
包内含有spring2.5.6源码,解压即可使用,还包括了doc
spring2.5.6 jar包+源码
rar包内含有spring2.5.6源码,解压即可使用 源代码分析,是一件既痛苦又快乐的事情,看别人写的代码是通过的,但当你能够看明白的时候,相信快乐也会随之而来,为了减少痛苦,更快的带来快乐,在这里希望通过这篇...
spring2.5.6官方jar包 spring2.5.6官方jar包 spring2.5.6官方jar包 spring2.5.6官方jar包 spring2.5.6官方jar包 spring2.5.6官方jar包 spring2.5.6官方jar包
spring2.5.6.zip包括spring2.5.6所有jar包为
spring2.5.6jar包sprinspring2.5.6jar包g2.5.6jar包spring2.5.6jar包
spring-framework-2.5.6 框架 源码
一、Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar、spring-webmvc.jar、commons-logging.jar、cglib-nodep-2.1_3.jar Hibernate 3.6.8:hibernate3.jar...
spring2.5.6开发必备的jar文件,希望能帮到你。
该txt文件中是spring2.5.6完整版开发依赖包的直接下载地址,迅雷中直接新建下载任务复制该地址就可快速下载。该依赖包中包含了所有Spring开发中用到的jar文件以及spring框架源码,和相关开发文档,api,示例等,欢迎...
spring 2.5.6 官方包 spring-framework-2.5.6.SEC02.zip spring-framework-2.5.6.SEC02.zip spring-framework-2.5.6.SEC02.zip
spring2.5.6 aop简单示例,导入可运行,包括必须jar包
Spring2.5.6 帮助文档 API
spring2.5.6注解以及xml两个简单ioc入门示例项目,导入项目可运行。 同时带有spring-framework-2.5.6-with-docs.zip包
spring2.5.6完整源代码及api
spring-framework-2.5.6框架和源码,如果你想使用spring,其中包含spring完成框架。而且存在源码,当你想深入学习spring的时候,这无疑是个不错的选择。 压缩密码:tri10002000