Spring integration JDBC template method design mode based on combination implementation, jdbc Design Mode
Combination-based implementation of Spring integrated JDBC Template Method Design Patterns
The template design pattern refers to extracting the corresponding template methods from a specific location and defining them, and then implementing the operations in the same call process through the template.
The template design mode is generally in the middle of 2
1. Implementation Based on inheritance
2. Implementation Based on Combination
Previously implemented: 1. Inheritance-based implementation. Inheritance-based implementation of Spring's Integrated JDBC Template Method Design Model
Next implementation: 2. Implementation Based on Combination
Spring integrated JDBC template method design mode-a combination-based method is especially convenient when we have a large number of classes.
This implementation simulates the JdbcTemplate class of org. springframework. jdbc. core. In Spring.
Create a template class on the basis of 1: MyJdbcTemplate. For details, see the code.
Package org. oms. spring. template; public class MyJdbcTemplate {private void beginConnection () {System. out. println ("begin connection! ");} Private void closeConnection () {System. out. println (" close connection! ");}/*** Call method, pass in the interface of a hook function */public void execute (MyCallback callback) {beginConnection (); callback. doInTemplate (); closeConnection () ;}/ *** create all methods to be implemented in the template, modify parameters, and ask the corresponding Object */public void add (final object Object) {execute (new MyCallback () {@ Overridepublic void doInTemplate () {System. out. println ("add" + object) ;}}) ;}public void delete (final int id) {execute (new MyCallback () {@ Overridepublic void doInTemplate () {System. out. println ("delete" + id) ;}}) ;}public void update (final Object object) {execute (new MyCallback () {@ Overridepublic void doInTemplate () {System. out. println ("update" + object );}});}}
Create interface MyCallback
package org.oms.spring.template;public interface MyCallback {public void doInTemplate();}
Modify the class MyJdbcTemplateByIn in 1:
Package org. oms. spring. template;/*** implementation template design mode based on combination ** @ author sunlight **/public abstract class MyJdbcTemplateByIn {private MyJdbcTemplate mt = new MyJdbcTemplate (); public void add (Object object) {mt. add (object);} public void delete (int id) {mt. delete (id);} public void update (Object object) {mt. update (object);} public abstract void run ();/*** in the template method, a function is called a hook function, the function of the hook allows the implementation class to control the process in the template through some methods ** @ return */public abstract boolean isLog ();}
The MessageDao. java class and RoleDao. java class remain unchanged. For details, refer to the implementation of 1.
Test class and result:
What design patterns are used in the java spring context to teach the solution?
It can be added downstairs, but for spring context, it is based on the factory mode of jdbc sessionFactory ------ solution -------------------------------------------------- Abstract Factory single-piece mode factory method builder mode prototype mode memorandum Mode Policy mode visitor Mode Observer Mode responsibility Chain Mode intermediary mode status mode Agent Mode iterator mode command mode template method Bridge Mode adapter mode appearance mode enjoy meta mode decoration Mode Combination Mode ------ solution ------------------------------------------------------ factory mode ------ Solution solution --------------------------------------- ----------------- Mainly refers to the factory model. BeanFactory and ApplicationContext are not used as a factory. It is an object used to generate objects.
Spring integrates jdbc for queries.
I think your usage is awkward.
String query = "select tcp. recno as id, tcp. card_no, tcp. emp_no, hs. name, hs. corp_cod, hs. depa_cod," +
"Convert (int, tcp. balance) as balance from tblcardpub as tcp" +
"Left outer join hr_staff as hs on tcp. emp_no = hs. emp_no where card_status = 1"
Public List <CardPub> getByCondition (String query) throws Exception {
List <CardPub> rlist = (List <CardPub> using jdbctemplate.exe cute (query, new PreparedStatementCallback (){
Public Object doInPreparedStatement (PreparedStatement pst) throws SQLException
{
List <CardPub> rlist = new ArrayList <CardPub> ();
ResultSet rst = pst.exe cuteQuery ();
While (rst. next ())
{
CardPub cardiopulmonary bypass = new CardPub ();
ECC. setR_id (rst. getString ("id "));
ECC. setCard_no (rst. getString ("card_no "));
ECC. setBalance (rst. getInt ("balance "));
ECC. setCorp_cod (rst. getString ("corp_cod "));
ECC. setDepa_cod (rst. getString ("depa_cod "));
ECC. setEmp_no (rst. getString ("emp_no "));
ECC. setName (rst. getString ("name "));
Rlist. add (cpb );
}
Return rlist;
}
});
Return rlist;
}
We recommend that you use the execute () method.