Spring ORM module code details, springorm details

Source: Internet
Author: User

Spring ORM module code details, springorm details

Seven modules of Spring framework

MVC module code in Spring

The ORM module supports ORM frameworks such as Hibernate, JDO, and TopLinkiBatis.

The ORM module depends on packages such as dom4j. jar and anlr. jar.

In Spring, Hibernate resources should be handed over to Spring for management. Hibernate and Its SessionFactory and other knowledge Spring have a special Bean. Spring is responsible for instantiation and destruction. Therefore, the DAO layer only needs to inherit the HibernateDaoSupport, instead of dealing with the Hibernate API, and does not need to enable or disable the Hibernate Session and Transaction. Spring automatically maintains 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 "/> <! -- All classes under the 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 "class =" com. clf. spring. orm. catDaoImpl "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean>

If the object class configured using XML is changed

<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. Each method in the DAO layer is a transaction. In Spring + Hibernate programming, we are used to adding a Service layer on the DAO layer, and then configuring transactions on 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 for data access. In principle, cross-historical access is not allowed. Layered to make 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.

<! -- 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 a transaction to all methods --> <propkeypropkey = "*"> PROPGATION_REQUIRED </prop> </property> </bean> <! -- The transaction factory proxy class that assembles Service implementation classes, transaction managers, and transaction management rules --> <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>

Summary

The above is all the details about the ORM module code of Spring. I hope you can help me. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.