The inheritance-based implementation of Spring integrated JDBC Template Method design pattern

Source: Internet
Author: User

Spring Integrated JDBC Template method design pattern based on the implementation of inheritance:

Template design Pattern Simple description:
The same part is extracted, when we run the time to automatically set the value inside the JdbcTemplate source code to execute ().
He takes the public part and writes it into a special function, and when we use it, we call it in a specific part, and we do the same thing in different classes, and that's the way to do the template design pattern.

For example, a method in the JdbcTemplate class:

-------------------------------------------------------------------------//Methods dealing with static SQL ( java.sql.Statement)//-------------------------------------------------------------------------public <T> T Execute (statementcallback<t> action) throws DataAccessException {Assert.notnull (Action, "Callback object must Not is null "); Connection con = datasourceutils.getconnection (Getdatasource ()); Statement stmt = null;try {Connection contouse = con;if (this.nativejdbcextractor! = null&& This.nativejdbcextrac Tor.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);D atasourceutils.releaseconnection (Con, Getdatasource ());}}

Template design pattern refers to the corresponding template method is extracted in a specific location definition, and then the same call procedure operation through the template implementation.
For template design patterns, there are typically 2 ways
1. Implemented in an inheritance-based manner:
First create a new Myjdbctemplatebyin.java class:

Package org.oms.spring.template;/** * Inheritance-based implementation template design pattern *  * @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 ();/** * There is a function called the hook function in the template method, the function of the hook function is to let the implementation class through some methods to control the process in the template *  * @return */public Abstract Boolean Islog ();p ublic Void execute () {beginconnection (); if (Islog ()) {System.out.println ("added log! ");} Run (); CloseConnection ();}}

then create the role class and inherit the Myjdbctemplatebyin class created above:
Package Org.oms.spring.template;public class Roledao extends Myjdbctemplatebyin {@Overridepublic void run () { System.out.println ("Role add!");} @Overridepublic Boolean Islog () {return false;  Did not join the log}}

Create the class message again, inheriting the same class:

Package Org.oms.spring.template;public class Messagedao extends Myjdbctemplatebyin {@Overridepublic void run () { SYSTEM.OUT.PRINTLN ("message add!");} @Overridepublic Boolean Islog () {return true;//join log}}

Description: We added an abstract method to the class Myjdbctemplatebyin:
public abstract void Run ();/** * There is a function called the hook function in the template method, the function of the hook function is to let the implementation class through some methods to control the process in the template *  * @return */public Abstract Boolean Islog ();

Implement the methods in its subclasses, each of which executes the BeginXxx method before running, and then executes the Close method.

The log is not added in role, so the return value of Islog is false and the log is added to the message class, and the Islog method returns a value of true.


Questions! But there are many methods in the process, each of which needs to be implemented, which can be cumbersome!

The approach to solving this problem focuses on the "Spring integrated JDBC Template approach design pattern implemented in a combination-based way "

Test class and results:




The inheritance-based implementation of Spring integrated JDBC Template Method design pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.