A brief introduction to the seven major modules of Spring framework
Detailed explanation of the MVC module code in Spring
ORM module provides support for ORM frameworks such as Hibernate, JDO, TopLinkiBatis, etc.
ORM modules rely on dom4j.jar, antlr.jar and other packages
In Spring, Hibernate resources must be handed over to Spring management, Hibernate and its SessionFactory and other knowledge Spring is a special bean, and 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, and does not need to turn on or off 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" destroy-method="destroy"> <property name="dataSource" ref="dataSource" /> <!-- All classes under this Package 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"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
If you use an entity class configured with XML, change it to
<bean id="sessionFactory" destroy-method="destroy"> <property name="mappingResources"> <list> <value>classpath:/com/clf/orm/Cat.hbm.xml</value> </list> </property> … </bean>
Spring adds transactions in the DAO layer by default, and each method of the DAO layer is one transaction. In Spring+Hibernate programming, the usual practice is to add a Service layer to the DAO layer, and then configure the transactions to the Service layer.
public interface ICatService{ public void createCat(Cat cat); public List<Cat> listCats(); public int getCatsCount(); }The hierarchical approach is that the program calls the Service layer, the Service layer calls the DAO layer, and the DAO layer calls Hibernate to achieve data access. In principle, cross-border access is not allowed. Layering makes business hierarchy 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 level
<!-- Transaction Manager --> <bean id="hibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- Transaction Management Rules--> <bean id="hibernateTransactionAttributeSource"> <property name="properties"> <props><!-- Add transactions to all methods--> <propkeypropkey="*">PROPGATION_REQUIRED</prop> </property> </bean> <!-- Transaction Factory Agent Class, assemble Service Implementation Class, Transaction Manager, and Transaction Management Rules--> <bean id="catService"> <property name="transactionManager" ref=" hibernateTransactionManager"> <property name="target"> <bean > <property name="catDao" ref="catDao"/> </bean> </property> <property name="transactionAttributeSource" ref=" hibernateTransactionAttributeSource" /> </bean>
Summarize
The above is all the detailed explanation of Spring's ORM module code. I hope you can help. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!