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>
<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>
public interface ICatService{
public void createCat(Cat cat);
public List<Cat> listCats();
public int getCatsCount();
}
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();
}
}
<!-- 事务管理器-->
<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>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有