Spring之ORM模块代码详解
程序员文章站
2023-12-04 11:45:58
spring框架七大模块简单介绍
spring中mvc模块代码详解
orm模块对hibernate、jdo、toplinkibatis等orm框架提供支持
orm模块...
orm模块对hibernate、jdo、toplinkibatis等orm框架提供支持
orm模块依赖于dom4j.jar、antlr.jar等包
在spring里,hibernate的资源要交给spring管理,hibernate以及其sessionfactory等知识spring一个特殊的bean,有spring负责实例化与销毁。因此dao层只需要继承hibernatedaosupport,而不需要与hibernate的api打交道,不需要开启、关闭hibernate的session、transaction,spring会自动维护这些对象
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>
如果使用xml配置的实体类,则改为
<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默认在dao层添加事务,dao层的每个方法为一个事务。spring+hibernate编程中,习惯的做法实在dao层上再添加一个service层,然后把事务配置在service层
public interface icatservice{ public void createcat(cat cat); public list<cat> listcats(); public int getcatscount(); }
分层的做法是,程序调用service层,service层调用dao层,dao层调用hibernate实现数据访问,原则上不允许跨曾访问。分层使业务层次更加清晰
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(); } }
然后再service层配置事务管理
<!-- 事务管理器--> <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>
总结
以上就是本文关于spring之orm模块代码详解的全部内容,希望大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!