Spring ORM module code details

  • 2020-12-09 00:52:31
  • OfStack

7 modules of Spring framework are briefly introduced

MVC module code detail in Spring

ORM module provides support for ORM frameworks such as Hibernate, JDO and TopLinkiBatis

The ORM module relies on packages like dom4ES17en. jar, antlr. jar, etc

In Spring, Hibernate's resources are managed by Spring, Hibernate and its knowledge of SessionFactory, Spring1 special Bean, and Spring is responsible for instantiation and destruction. Therefore, the DAO layer only inherits HibernateDaoSupport and does not need to deal with API of Hibernate. Session and Transaction of Hibernate do not need to be turned on and off. Spring maintains these objects automatically


public interface ICatDao{ 
   public void createCat(Cat cat); 
   public List<Cat> listCats(); 
   public int getCatsCount(); 
   public Cat findCatByName(String name); 
} 

import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 
public class CatDaoImpl extends HibernateDaoSupportimplements ICatDao{ 
   public void createCat(Cat cat){ 
       this.getHibernateTemplate().persist(cat); 
   } 
   public List<Cat> listCats(){ 
       return this. getHibernateTemplate().find("select cfrom Cat c"); 
   } 
   public int getCatsCount(){ 
       Number n = (Number)this.getSession(true).createQuery("selectcount(c) from Cat c").uniqueResult(); 
       return n.intValue(); 
   } 
   public Cat findCatByName(String name){ 
       List<Cat> catList =this.getHibernateTemplate().find("select c from Cat where c.name = ?",name); 
       if(catList.size()>0) 
          return catList.get(0); 
       return null; 
   } 
  
} 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" destroy-method="destroy"> 
<property name="dataSource" ref="dataSource" /> 
<!--  the Package All classes below will be loaded as entity classes --> 
<property name="annotatedPackages" value="classpath:/com/clf/orm" /> 
<property name="anotatedClasses"> 
   <list> 
       <value>com.clf.spring.orm.Cat</value> 
       <value>com.clf.spring.orm.Dog</value> 
   </list> 
<property name="hibernateProperties"> 
   <props> 
       <prop key="hiberante.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key=" hibernate.format_sql">true</prop> 
       <prop key=" hibernate.hbm2ddl.auto">create</prop> 
   </props> 
</property> 
  
<bean id="catDao" class="com.clf.spring.orm.CatDaoImpl"> 
   <property name="sessionFactory" ref=" sessionFactory"/> 
</bean> 

Change to XML configured entity class


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy"> 
<property name="mappingResources"> 
   <list> 
       <value>classpath:/com/clf/orm/Cat.hbm.xml</value> 
   </list> 
</property> 
 ...  
</bean> 

By default, Spring adds transactions to the DAO layer, with one transaction per method in the DAO layer. In Spring+Hibernate programming, it is customary to add an Service layer to the DAO layer and then configure transactions in the Service layer


public interface ICatService{ 
   public void createCat(Cat cat); 
   public List<Cat> listCats();  
   public int getCatsCount(); 
} 

The layered approach is that the program calls the Service layer, the Service layer calls the DAO layer, and the DAO layer calls Hibernate for data access, which in principle is not allowed across previous accesses. Layering makes the business level clearer


public class CatServiceImpl implements ICatService{ 
   private IDao catDao; 
   public IDao getCatDao(){ 
       return catDao; 
   } 
  
   public void setCatDao(IDao dao){ 
       this.catDao = dao; 
   } 
    
   public void createCat(Cat cat){ 
       catDao.createCat(cat); 
   } 
   public List<Cat> listCats(){ 
       return catDao.listCats(); 
   } 
   public int getCatsCount(){ 
       return catDao.getCatsCount(); 
   } 
  
} 

The transaction management is then configured at the Service layer


<!--  Transaction manager --> 
<bean id="hibernateTransactionManager" class=" org.springframework.orm.hibernate3.HibernateTransactionManager 
"> 
   <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 
  
<!--  Transaction management rules --> 
<bean id="hibernateTransactionAttributeSource" class=" org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource"> 
   <property name="properties"> 
       <props><!--  Add transactions to all methods --> 
          <propkeypropkey="*">PROPGATION_REQUIRED</prop> 
   </property> 
</bean> 
  
<!--  The transaction factory agent class, will Service The implementation class, transaction manager, and transaction management rules are assembled in 1 since --> 
<bean id="catService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
   <property name="transactionManager" ref=" hibernateTransactionManager"> 
   <property name="target"> 
       <bean class="com.clf.spring.orm.CatServiceImpl" > 
          <property name="catDao" ref="catDao"/> 
       </bean> 
   </property> 
   <property name="transactionAttributeSource" ref=" hibernateTransactionAttributeSource" /> 
</bean> 

conclusion

Above is the whole content of Spring ORM module code explanation in detail, I hope you can help. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: