Home  >  Article  >  Java  >  Detailed explanation of Spring's ORM module code

Detailed explanation of Spring's ORM module code

小云云
小云云Original
2017-12-05 11:40:502085browse

ORM object-relational mapping is a programming technology used to convert data between different types of systems in object-oriented programming languages. In effect, it actually creates a "virtual object database" that can be used in a programming language. This article mainly introduces the detailed explanation of Spring's ORM module code, which has certain reference value. Friends who need it can learn more.

A brief introduction to the seven major modules of the Spring framework

Detailed explanation of the MVC module code in Spring

ORM module is very useful for Hibernate, JDO, TopLinkiBatis and other ORM frameworks provide support

ORM modules rely on dom4j.jar, antlr.jar and other packages

In Spring, Hibernate resources must be managed by Spring, Hibernate and its SessionFactory and other knowledge Spring is a special Bean, Spring is responsible for instantiation and destruction. Therefore, the DAO layer only needs to inherit HibernateDaoSupport and does not need to deal with Hibernate's API. It does not need to open or close Hibernate's Session and Transaction. Spring will automatically maintain these objects

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" /> 
<!-- 该Package下的所有类都会被当做实体类加载--> 
<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>

If using XML configured entity class, change to

<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>

Spring adds transactions to the DAO layer by default, and each method in the DAO layer is a transaction. In Spring+Hibernate programming, the customary approach is to add a Service layer on top of the DAO layer, and then configure the transaction in the Service layer

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

The layered approach is , the program calls the Service layer, the Service layer calls the DAO layer, and the DAO layer calls Hibernate to implement data access. In principle, cross-access is not allowed. 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(); 
   } 
  
}

Then configure transaction management at the Service layer

<!-- 事务管理器--> 
<bean id="hibernateTransactionManager" class=" org.springframework.orm.hibernate3.HibernateTransactionManager 
"> 
   <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 
  
<!-- 事务管理规则--> 
<bean id="hibernateTransactionAttributeSource" class=" org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource"> 
   <property name="properties"> 
       <props><!-- 为所有方法加上事务--> 
          <propkeypropkey="*">PROPGATION_REQUIRED</prop> 
   </property> 
</bean> 
  
<!-- 事务工厂代理类,将Service实现类、事务管理器、事务管理规则组装在一起--> 
<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>

The above content is a detailed explanation of Spring's ORM module code. I hope it can help everyone.

Related recommendations:

Advantages of spring framework in Java framework

Detailed explanation of SpringAop in Java

A brief introduction to Spring MVC

The above is the detailed content of Detailed explanation of Spring's ORM module code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn