发表时间:2010-03-08
最后修改:2010-03-08
[b]接一[/b]
for (int i = 0; i < values.length; i++)
query.setParameter(i, values[i]);
query.setFirstResult(Integer.parseInt(curPage
.getThisPage())
* pageSize);
query.setMaxResults(pageSize);
return query.list();
}
}));
return listpage;
}
// 数据库通用分页 不带查询条件
public ListPage queryPage(final String hql, final ListPage curPage) {
ListPage listpage = new ListPage(curPage.getThisPage(),
getTotalSize(hql));
listpage.setElements(getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(Integer.parseInt(curPage
.getThisPage())
* pageSize);
query.setMaxResults(pageSize);
return query.list();
}
}));
return listpage;
}
public int getTotalSize(String hql, Object[] values) {
return getHibernateTemplate().find(hql, values).size();
}
public int getTotalSize(String hql) {
return getHibernateTemplate().find(hql).size();
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--main applicationContext file-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
lazy-init="false">
<property name="locations">
<list>
<value>classpath:conf/db.properties</value>
</list>
</property>
</bean>
</beans>
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--BaseDAO-->
<bean id="baseDAO" class="com.sysetm.dao.impl.BaseDAOImpl"
abstract="true">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--事务拦截器-->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoProxyCreator-->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
</bean>
<!-- TransactionAttributeSourceAdvisor -->
<bean
class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor"
ref="transactionInterceptor" />
</bean>
</beans>
applicationContext-factory.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- hibernate工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
destroy-method="destroy">
<property name="dataSource" ref="DataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.jdbc.fetch_size">10</prop>
<prop key="hibernate.jdbc.batch_size">8</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
<prop key="hibernate.default_batch_fetch_size">8</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath*:/com/system/hbm</value>
</list>
</property>
</bean>