spring+hibernate事务处理

一、DAO
package org.lunzi.dao.user;

import java.util.List;

import org.lunzi.util.page.PaginationSupport;

public interface UserDAO {
public int findUsersCount();
public List findUserList(int first,int pageSize);
public PaginationSupport getUserList(int startIndex,int pageSize);
}
二、Impl
package org.lunzi.dao.impl.user;

import java.util.List;

import org.hibernate.Query;
import org.lunzi.dao.base.BaseDAO;
import org.lunzi.dao.user.UserDAO;
import org.lunzi.util.page.PaginationSupport;

public class UserDAOImpl extends BaseDAO implements UserDAO {

public int findUsersCount() {
Integer count = (Integer) getSession().createQuery(
"select count(*) from User").uniqueResult();
return count.intValue();
}

public List findUserList(int first, int pageSize) {
return getSession()
.createQuery(
"select new org.lunzi.model.SearchUserModel(u.username) from User u ")
.setFirstResult(first).setMaxResults(pageSize).list();
}

public PaginationSupport getUserList(int startIndex, int pageSize) {
PaginationSupport pageSupport = new PaginationSupport();
pageSupport.setStartIndex(startIndex);
pageSupport.setCountOnEachPage(pageSize);

Query query = getSession().createQuery(
"from User u");

pageSupport.setTotalCount(findUsersCount());
query.setFirstResult(pageSupport.getStartIndex());
query.setMaxResults(pageSupport.getCountOnEachPage());
pageSupport.setItems(query.list());
return pageSupport;
}
}

三、ServiceImpl
package org.lunzi.service.impl.user;

import java.util.List;

import org.lunzi.dao.user.UserDAO;
import org.lunzi.service.user.UserService;
import org.lunzi.util.page.PaginationSupport;

public class UserServiceImpl implements UserService {
private UserDAO userDao;

public UserDAO getUserDao() {
return userDao;
}

public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}

public int findUsersCount() {

return userDao.findUsersCount();
}
public List findUserList(int first,int pageSize){
return userDao.findUserList(first, pageSize);
}
public PaginationSupport getUserList(int startIndex, int pageSize){
return userDao.getUserList(startIndex, pageSize);
}
}
四、spring上下文配置
<?xml version=
"1.0" encoding="UTF-8"?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


<bean id=
"dataSource"
class=
"org.apache.commons.dbcp.BasicDataSource">
<property name=
"driverClassName"
value=
"com.mysql.jdbc.Driver">
</property>
<property name=
"url"
value=
"jdbc:mysql://localhost:3306/bookstore">
</property>
<property name=
"username" value="root"></property>
<property name=
"password" value="123456"></property>
</bean>

<bean id=
"sessionFactory"
class=
"org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name=
"mappingResources">
<list>
<value>org/lunzi/pojos/User.hbm.xml</value>
</list>
</property>
<property name=
"dataSource">
<ref bean=
"dataSource" />
</property>
<property name=
"hibernateProperties">
<props>
<prop key=
"hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key=
"connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</prop>
<prop key=
"hibernate.show_sql">true</prop>
<prop key=
"hibernate.cache.use_query_cache">true</prop>
<prop key=
"hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>

</props>
</property>
</bean>

<bean id=
"transactionManager"
class=
"org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name=
"sessionFactory">
<ref local=
"sessionFactory" />
</property>
</bean>


<bean id=
"userDao" class="org.lunzi.dao.impl.user.UserDAOImpl">
<property name=
"sessionFactory">
<ref bean=
"sessionFactory" />
</property>
</bean>

<bean id=
"userDaoProxy"
class=
"org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name=
"transactionManager">
<ref bean=
"transactionManager" />
</property>
<property name=
"target">
<ref local=
"userDao" />
</property>
<property name=
"transactionAttributes">
<props>
<prop key=
"find*">PROPAGATION_REQUIRED, readOnly</prop>
<prop key=
"*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<bean id=
"userService"
class=
"org.lunzi.service.impl.user.UserServiceImpl">
<property name=
"userDao">
<ref bean=
"userDaoProxy" />
</property>
</bean>


</beans>

lunzi   2007-12-13 00:15:59 评论:0   阅读:912   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2008 powered by YuLog