The framework of day03-spring and database operations is the JDBC of dataSource, using the existing transactions in spring for JDBC operations

Source: Internet
Author: User

Zookeeper

Database Operations:
Fixed code (template, datasource acquisition) + dynamic parameters (changed SQL statements, parameters, etc)
Template Mode Programming
Code structure parsing:
JdbcTemplate extends JdbcAccessor
JdbcTemplate (){

}
JdbcTemplate (DataSource dataSource ){
This. dataSource = dataSource;
}
SetDataSource (DataSource dataSource ){
This. dataSource = dataSource;
}



Public abstract class JdbcDaoSupport extends DaoSupport {
Private JdbcTemplate jdbcTemplate;
Public final void setDataSource (DataSource dataSource ){
If (this. jdbcTemplate = null | dataSource! = This. jdbcTemplate. getDataSource ()){
This. jdbcTemplate = createJdbcTemplate (dataSource );
InitTemplateConfig ();
}
}
Protected JdbcTemplate createJdbcTemplate (DataSource dataSource ){
Return new JdbcTemplate (dataSource );
}
Public final DataSource getDataSource (){
Return (this. jdbcTemplate! = Null? This. jdbcTemplate. getDataSource (): null );
}
Public final void setJdbcTemplate (JdbcTemplate jdbcTemplate ){
This. jdbcTemplate = jdbcTemplate;
InitTemplateConfig ();
}
Public final JdbcTemplate getJdbcTemplate (){
Return this. jdbcTemplate;
}
}
Note:
1. dataSource can be injected into JdbcDaoSupport.
2. The dataSource injected in JdbcDaoSupport uses the createJdbcTemplate method.
Passed To JdbcTemplate
3. You can use JdbcDaoSupport to obtain the reference of JdbcTemplate.
HibernateTemplate
SqlMapClientTemplate
JdoTemplate

Seven methods of spring combined with jdbc:
1,
Class Syntax:
PersonDao extends JdbcDaoSupport
Configuration file:
1. Inject DataSource into PersonDao
2. Inject JdbcTemplate into PersonDao
Inject dataSource in JdbcTemplate
1. constructor form can be used
2. You can use the setter method.
2,
Class Writing
PersonDao {
Private JdbcTemplate jdbcTemplate;
}
Configuration file:
Inject JdbcTemplate into PersonDao
Inject dataSource in JdbcTemplate
1. constructor form can be used
2. You can use the setter method.
3,
Class Writing
PersonDao extends JdbcTemplate {

}
Configuration file:
Inject dataSource using the constructor or setter Method

Summary:
Whatever the form, dataSource is finally passed into the jdbcTemplate, and a series of crud operations can be performed as long as the data source is available.


package com.itheima.dao;public interface PersonDao {void savePerson();}


package com.itheima.dao.impl;import org.springframework.jdbc.core.support.JdbcDaoSupport;import com.itheima.dao.PersonDao;public class PersonDaoImpl extends JdbcDaoSupport implements PersonDao {@Overridepublic void savePerson() {// TODO Auto-generated method stubthis.getJdbcTemplate().execute("insert into person(name) values('ccc')");}}

package com.itheima.service;public interface PersonService {void savePerson();}


Package com. itheima. service. impl; import com. itheima. dao. personDao; import com. itheima. service. personService; public class PersonServiceImpl implements PersonService {private PersonDao personDao; public void setPersonDao (PersonDao personDao) {this. personDao = personDao;} @ Overridepublic void savePerson () {/*** if a transaction exists, the following operations will not be executed and will not succeed. Because of exceptions. * Either. */This. personDao. savePerson (); // int I = 1/0; this. personDao. savePerson ();}}


Spring configuration file applicationContext. xml:


 
 
  
   
    
     
Classpath: jdbc. properties
    
         
   
    
    
    
    
   
   
   
    
     
    
   
   
    
     
    
   
   
   
    
     
    
   
   
   
    
     
     
    
   
  
 


Spring transaction architecture:

1. Top-level interface public interface PlatformTransactionManager {// get the transaction TransactionStatus getTransaction (TransactionDefinition definition) throws TransactionException; // submit the transaction void commit (TransactionStatus status) throws TransactionException; // transaction rollback void rollback (TransactionStatus status) throws TransactionException;} TransactionStatusboolean isNewTransaction (); 2. abstract class public abstract class AbstractPlatformTransactionManager implements PlatformTransactionManager {// get the transaction public final TransactionStatus getTransaction (TransactionDefinition definition) throws TransactionException {// abstract method Object transaction = doGetTransaction ();} // who will inherit this abstract class and who will implement this method protected abstract Object doGetTransaction () throws TransactionException; public final void commit (TransactionStatus status) throws TransactionException {} public final void rollback (TransactionStatus status) throws TransactionException {}} 3. Implementation class: cetcetransactionmanagerhibernatetransactionmanagerjdotransactionmanager... case study: public class PersonDao {private PlatformTransactionManager platformTransactionManager; public void setPlatformTransactionManager (PlatformTransactionManager platformTransactionManager) {this. platformTransactionManager = PlatformTransactionManager ;}}


Spring declarative Transaction Processing

In this case, the spring container is a plane.

Note: To use spring transaction management to manage transactions in a program, you must comply with spring specifications (one interface, one abstract class, and three implementation classes (JDBC, hibernate, and ,)).
When using spring transaction management, you do not need to write partition classes. spring has been implemented and you do not need to worry about it. Programmers only need to write crud operations and declare transactions.


Transaction Manager: Which technology are you using, JDBC and hibernate? No matter which technology you are using, spring helps you implement full interface-oriented programming.



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.