您的位置:首页 > 编程语言 > Java开发

Spring整合JDBC模板方法设计模式之基于继承的实现

2014-09-23 00:13 851 查看
Spring整合JDBC模板方法设计模式之基于继承的实现:

模板设计模式简单描述:

把相同的部分提取出来,当我们运行的时候自动往里面设置值,在JdbcTemplate 的源代码中得execute().

他把公共的部分拎出来写到一个特别的函数中,当我们使用的时候把会发生变化的内容在特定的部分调用,在不同的类里面处理相同的操作,这种方式就做模板设计模式。

例如,JdbcTemplate类中的方法:// -------------------------------------------------------------------------
// Methods dealing with static SQL (java.sql.Statement)
// -------------------------------------------------------------------------

public <T> T execute(StatementCallback<T> action)
throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");

Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
Connection conToUse = con;
if (this.nativeJdbcExtractor != null
&& this.nativeJdbcExtractor
.isNativeConnectionNecessaryForNativeStatements()) {
conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
}
stmt = conToUse.createStatement();
applyStatementSettings(stmt);
Statement stmtToUse = stmt;
if (this.nativeJdbcExtractor != null) {
stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);
}
T result = action.doInStatement(stmtToUse);
handleWarnings(stmt);
return result;
} catch (SQLException ex) {
// Release Connection early, to avoid potential connection pool
// deadlock
// in the case when the exception translator hasn't been initialized
// yet.
JdbcUtils.closeStatement(stmt);
stmt = null;
DataSourceUtils.releaseConnection(con, getDataSource());
con = null;
throw getExceptionTranslator().translate("StatementCallback",
getSql(action), ex);
} finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
模板设计模式指的是将相应的模板方法提取出来在一个专门的一个位置定义,然后把相同调用过程的操作通过模板实现。

对于模板设计模式,一般有2中方式

1.基于继承的方式实现:

首先新建MyJdbcTemplateByIn.java类:package org.oms.spring.template;

/**
* 基于继承的实现模板设计模式
*
* @author sunlight
*
*/
public abstract class MyJdbcTemplateByIn {
private void beginConnection() {
System.out.println("begin connection!");
}

private void closeConnection() {
System.out.println("close connection!");
}

public abstract void run(); /** * 在模板方法中有一种函数叫做钩子函数,钩子函数的作用是让实现类通过一些方法来控制模板中的流程 * * @return */ public abstract boolean isLog();

public void execute() {
beginConnection();
if (isLog()) {
System.out.println("加入了日志!");
}
run();
closeConnection();
}
}

然后创建Role类并继承上面创建的MyJdbcTemplateByIn类:
package org.oms.spring.template;

public class RoleDao extends MyJdbcTemplateByIn {

@Override
public void run() {
System.out.println("role add!");
}

@Override
public boolean isLog() {
return false;  //没有加入日志
}

}

再次创建类Message同上继承相同的类:package org.oms.spring.template;

public class MessageDao extends MyJdbcTemplateByIn {

@Override
public void run() {
System.out.println("message add!");
}

@Override
public boolean isLog() {
return true; //加入日志
}

}


说明:我们在类MyJdbcTemplateByIn中添加了抽象方法:
public abstract void run();

/**
* 在模板方法中有一种函数叫做钩子函数,钩子函数的作用是让实现类通过一些方法来控制模板中的流程
*
* @return
*/
public abstract boolean isLog();

在其子类中实现方法,每个方法运行前执行beginXXX方法,结束后执行close方法。

Role中不添加日志,所以isLog的返回值为false,在message类中添加日志,isLog方法返回值为true。

疑问!但我们实际过程中又许多方法,每个类都需要实现,这样做会很麻烦!

解决此问题的方法关注 “Spring整合JDBC模板方法设计模式之基于组合的方式实现”

测试类及结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息