- 浏览: 29482 次
- 性别:
- 来自: 南京
最新评论
-
jingjing0907:
我是java初学者,看完此文,给我很多启发!
JAVA 学习一周年记 -
craengjava:
您好,我的也是空白,Grails fckeditor plug ...
Grails 1.1 中使用 fckeditor 0.9 配置 -
rqfreedom:
1.<g:form action="list& ...
Grails 复杂查询及分页排序 -
rqfreedom:
1.<g:form action="list& ...
Grails 复杂查询及分页排序 -
rqfreedom:
system.out.println(123)
Grails 复杂查询及分页排序
2个月前,因为项目需要开始接触到SSH ,以前只用过JSP 开发过项目,所以对于SSH 一切都是新鲜玩意,虽然学习的过程是痛苦的,但是解决问题的快乐只有自己能体会到。
在这里是我第一次写Blog,一是为了能够和这里的朋友共同进步,二是为了记录自己收获的点点滴滴,三是为了能够给和我一样的初学者分享所得——严重跑题。。 :D
言归正传,下面是我的实现方法
一、开发环境
JDK1.5 Hibernate 3.1 Struts 1.2 Spring2.0 MyEclplise 5.5GA
二、Struts 和 Spring配置
Struts配置文件
xml 代码
- <!---->xml version="1.0" encoding="UTF-8"?>
- <!---->>
- <struts-config>
- <data-sources />
- <form-beans>
- <form-bean name="drugForm" type="com.trade.struts.form.DrugForm" />
- <form-bean name="drugQueryForm" type="com.trade.struts.queryform.DrugQueryForm" />
- <form-bean name="baseForm" type="com.trade.struts.queryform.BaseForm" />
- form-beans>
- <global-exceptions />
- <global-forwards />
- <action-mappings>
- <action
- attribute="drugQueryForm"
- input="/Drug/drug_list.jsp"
- name="drugQueryForm"
- parameter="method"
- path="/drugQuery"
- scope="request"
- >
- <forward name="success" path="/index.jsp" />
- <forward name="list" path="/Drug/drug_list.jsp" />
- action>
- action-mappings>
- <!---->
- <controller>
- <set-property property="processorClass"
- value="org.springframework.web.struts.DelegatingRequestProcessor" />
- controller>
- <!---->
- <message-resources parameter="com.trade.struts.ApplicationResources" />
- <!---->
- <plug-in
- className="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-property property="contextConfigLocation"
- value="/WEB-INF/applicationContext.xml" />
- plug-in>
- struts-config>
Spring配置代码
xml 代码
- <!---->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="dataSourceTrade"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName"
- value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
- property>
- <property name="url"
- value="jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=Trade">
- property>
- <property name="username" value="share">property>
- <property name="password" value="sa">property>
- bean>
- <bean id="sessionFactoryTrade"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSourceTrade" />
- property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.SQLServerDialect
- prop>
- <prop key="hibernate.show_sql">trueprop>
- <prop key="current_session_context_class">threadprop>
- <prop key="connection.pool_size">10prop>
- <prop key="hibernate.hbm2ddl.auto">updateprop>
- props>
- property>
- <property name="mappingResources">
- <list>list>
- property>
- bean>
- <!---->
- <bean id="dataSourceRush"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName"
- value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
- property>
- <property name="url"
- value="jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=Rush">
- property>
- <property name="username" value="share">property>
- <property name="password" value="sa">property>
- bean>
- <bean id="sessionFactoryRush"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSourceTrade" />
- property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.SQLServerDialect
- prop>
- props>
- property>
- <property name="mappingResources">
- <list>
- <value>com/trade/vo/rush/Drug.hbm.xmlvalue>
- list>
- property>
- bean>
- <!---->
- <bean id="DrugDAOHibernate" class="com.trade.dao.hibernate.DrugDAOHibernate">
- <property name="sessionFactory">
- <ref bean="sessionFactoryRush" />
- property>
- bean>
- <bean id="DrugService" class="com.trade.service.DrugService">
- <property name="ddh">
- <ref bean="DrugDAOHibernate" />
- property>
- bean>
- <bean name="/drugQuery" abstract="false" lazy-init="default"
- autowire="default" dependency-check="default" class="com.trade.struts.action.DrugAction">
- <property name="ds">
- <ref bean="DrugService" />
- property>
- bean>beans>
三、DAOImpl、Service 业务逻辑部分
因为主要是介绍分页,这里只放分页相关代码
DrugDAOHibernate类,这里使用的方法参考(抄袭。。)了Robbin等关于使用离线Criteria实现查询的代码
java 代码
- package com.trade.dao.hibernate;
- import java.util.List;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.hibernate.Criteria;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.criterion.DetachedCriteria;
- import org.hibernate.criterion.Projections;
- import org.springframework.orm.hibernate3.HibernateCallback;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.trade.dao.IDrugDAO;
- import com.trade.vo.DrugDAO;
- import com.trade.vo.rush.Drug;
- public class DrugDAOHibernate extends HibernateDaoSupport implements IDrugDAO {
- private static final Log log = LogFactory.getLog(DrugDAO.class);
- //获得 符合查询条件的记录数目
- public Long getDrugCount(final DetachedCriteria detachedCriteria) {
- return (Long)getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session) throws HibernateException {
- Criteria criteria = detachedCriteria.getExecutableCriteria(session);
- Long count= Long.parseLong((criteria.setProjection(Projections.rowCount()).uniqueResult()).toString());
- criteria.setProjection(null);
- return count;
- }
- }, true);
- }
- //根据条件进行查询、分页
- public List listByConditions(final DetachedCriteria detachedCriteria,final int pageNo,final int pageSize) {
- return (List) getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session) throws HibernateException {
- Criteria criteria = detachedCriteria.getExecutableCriteria(session);
- criteria.setMaxResults(pageSize);
- criteria.setFirstResult((pageNo-1)*pageSize);
- return criteria.list();
- }
- }, true);
- }
- }
Service层很简单
java 代码
- package com.trade.service;
- import java.util.List;
- import org.hibernate.criterion.DetachedCriteria;
- import com.trade.dao.hibernate.DrugDAOHibernate;
- public class DrugService {
- //由Spring注入DrugDAOHibernate实例
- private DrugDAOHibernate ddh ;
- public DrugDAOHibernate getDdh() {
- return ddh;
- }
- public void setDdh(DrugDAOHibernate ddh) {
- this.ddh = ddh;
- }
- //获得满足条件的记录数
- public List listByConditions(final DetachedCriteria detachedCriteria,final int pageNo,final int pageSize){
- return ddh.listByConditions(detachedCriteria,pageNo,pageSize);
- }
- //根据条件进行查询、分页
- public Long getDrugCount(final DetachedCriteria detachedCriteria){
- return ddh.getDrugCount(detachedCriteria);
- }
- }
评论
4 楼
amcucn
2008-10-19
不错,和我目前所用到的方法基本上是如出一辙!
3 楼
javaxy
2008-10-19
下文呢?
2 楼
erlyhds
2008-10-18
<div class='quote_title'>brimstone 写道</div>
<div class='quote_div'><span style='font-family: Arial;'>
<p>2个月前,因为项目需要开始接触到SSH ,以前只用过JSP 开发过项目,所以对于SSH 一切都是新鲜玩意,虽然学习的过程是痛苦的,但是解决问题的快乐只有自己能体会到。<br/>在这里是我第一次写Blog,一是为了能够和这里的朋友共同进步,二是为了记录自己收获的点点滴滴,三是为了能够给和我一样的初学者分享所得——严重跑题。。 </p>
<p><span style='font-family: Arial;'>言归正传,下面是我的实现方法</span></p>
<span style='font-family: Arial;'>
<p><br/>一、开发环境<br/>JDK1.5 Hibernate 3.1 Struts 1.2 Spring2.0 MyEclplise 5.5GA</p>
<p>二、Struts 和 Spring配置<br/> Struts配置文件</p>
<div class='code_title'>xml 代码</div>
<div class='dp-highlighter'><ol class='dp-xml'>
<li class='alt'><span><span class='tag'><!----><span class='tag-name'>xml</span><span> </span><span class='attribute'>version</span><span>=</span><span class='attribute-value'>"1.0"</span><span> </span><span class='attribute'>encoding</span><span>=</span><span class='attribute-value'>"UTF-8"</span><span class='tag'>?></span><span> </span></span> </span></li>
<li><span><!----><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
<li><span class='tag'><</span><span class='tag-name'>struts-config</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>data-sources</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>form-beans</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>form-bean</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"drugForm"</span><span> </span><span class='attribute'>type</span><span>=</span><span class='attribute-value'>"com.trade.struts.form.DrugForm"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>form-bean</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"drugQueryForm"</span><span> </span><span class='attribute'>type</span><span>=</span><span class='attribute-value'>"com.trade.struts.queryform.DrugQueryForm"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>form-bean</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"baseForm"</span><span> </span><span class='attribute'>type</span><span>=</span><span class='attribute-value'>"com.trade.struts.queryform.BaseForm"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>form-beans</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>global-exceptions</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>global-forwards</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>action-mappings</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>action</span><span> </span> </li>
<li><span> </span><span class='attribute'>attribute</span><span>=</span><span class='attribute-value'>"drugQueryForm"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>input</span><span>=</span><span class='attribute-value'>"/Drug/drug_list.jsp"</span><span> </span> </li>
<li><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"drugQueryForm"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>parameter</span><span>=</span><span class='attribute-value'>"method"</span><span> </span> </li>
<li><span> </span><span class='attribute'>path</span><span>=</span><span class='attribute-value'>"/drugQuery"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>scope</span><span>=</span><span class='attribute-value'>"request"</span><span> </span> </li>
<li><span> </span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>forward</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"success"</span><span> </span><span class='attribute'>path</span><span>=</span><span class='attribute-value'>"/index.jsp"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>forward</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"list"</span><span> </span><span class='attribute'>path</span><span>=</span><span class='attribute-value'>"/Drug/drug_list.jsp"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>action</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>action-mappings</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='comments'><!----></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>controller</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>set-property</span><span> </span><span class='attribute'>property</span><span>=</span><span class='attribute-value'>"processorClass"</span><span> </span> </li>
<li><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"org.springframework.web.struts.DelegatingRequestProcessor"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>controller</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='comments'><!----></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>message-resources</span><span> </span><span class='attribute'>parameter</span><span>=</span><span class='attribute-value'>"com.trade.struts.ApplicationResources"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='comments'><!----></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>plug-in</span><span> </span> </li>
<li><span> </span><span class='attribute'>className</span><span>=</span><span class='attribute-value'>"org.springframework.web.struts.ContextLoaderPlugIn"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>set-property</span><span> </span><span class='attribute'>property</span><span>=</span><span class='attribute-value'>"contextConfigLocation"</span><span> </span> </li>
<li><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"/WEB-INF/applicationContext.xml"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>plug-in</span><span class='tag'>></span><span> </span> </span></li>
<li><span class='tag'><span class='tag-name'>struts-config</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
</ol></div>
<p> Spring配置代码</p>
<div class='code_title'>xml 代码</div>
<div class='dp-highlighter'><ol class='dp-xml'>
<li class='alt'><span><span class='tag'><!----><span class='tag-name'>xml</span><span> </span><span class='attribute'>version</span><span>=</span><span class='attribute-value'>"1.0"</span><span> </span><span class='attribute'>encoding</span><span>=</span><span class='attribute-value'>"UTF-8"</span><span class='tag'>?></span><span> </span></span> </span></li>
<li><span class='tag'><</span><span class='tag-name'>beans</span><span> </span><span class='attribute'>xmlns</span><span>=</span><span class='attribute-value'>"http://www.springframework.org/schema/beans"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>xmlns:xsi</span><span>=</span><span class='attribute-value'>"http://www.w3.org/2001/XMLSchema-instance"</span><span> </span> </li>
<li><span> </span><span class='attribute'>xsi:schemaLocation</span><span>=</span><span class='attribute-value'>"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='comments'><!----></span><span> </span> </li>
<li class='alt'><span> </span><span class='comments'><!----></span><span> </span> </li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"dataSourceTrade"</span><span> </span> </li>
<li><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"org.apache.commons.dbcp.BasicDataSource"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"driverClassName"</span><span> </span> </li>
<li><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"com.microsoft.jdbc.sqlserver.SQLServerDriver"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"url"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=Trade"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"username"</span><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"share"</span><span class='tag'>></span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"password"</span><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"sa"</span><span class='tag'>></span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"sessionFactoryTrade"</span><span> </span> </li>
<li><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"org.springframework.orm.hibernate3.LocalSessionFactoryBean"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"dataSource"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>ref</span><span> </span><span class='attribute'>bean</span><span>=</span><span class='attribute-value'>"dataSourceTrade"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"hibernateProperties"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>props</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"hibernate.dialect"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> org.hibernate.dialect.SQLServerDialect </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"hibernate.show_sql"</span><span class='tag'>></span><span>true</span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"current_session_context_class"</span><span class='tag'>></span><span>thread</span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"connection.pool_size"</span><span class='tag'>></span><span>10</span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"hibernate.hbm2ddl.auto"</span><span class='tag'>></span><span>update</span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>props</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"mappingResources"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>list</span><span class='tag'>></span><span class='tag'><span class='tag-name'>list</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='comments'><!----></span><span> </span> </li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"dataSourceRush"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"org.apache.commons.dbcp.BasicDataSource"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"driverClassName"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"com.microsoft.jdbc.sqlserver.SQLServerDriver"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"url"</span><span> </span> </li>
<li><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=Rush"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"username"</span><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"share"</span><span class='tag'>></span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"password"</span><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"sa"</span><span class='tag'>></span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"sessionFactoryRush"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"org.springframework.orm.hibernate3.LocalSessionFactoryBean"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"dataSource"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>ref</span><span> </span><span class='attribute'>bean</span><span>=</span><span class='attribute-value'>"dataSourceTrade"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"hibernateProperties"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>props</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"hibernate.dialect"</span><span class='tag'>></span><span> </span> </li>
<li><span> org.hibernate.dialect.SQLServerDialect </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>props</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"mappingResources"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>list</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>value</span><span class='tag'>></span><span>com/trade/vo/rush/Drug.hbm.xml</span><span class='tag'><span class='tag-name'>value</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>list</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span><span> </span> </li>
<li class='alt'><span> </span><span class='comments'><!----></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"DrugDAOHibernate"</span><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"com.trade.dao.hibernate.DrugDAOHibernate"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"sessionFactory"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>ref</span><span> </span><span class='attribute'>bean</span><span>=</span><span class='attribute-value'>"sessionFactoryRush"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"DrugService"</span><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"com.trade.service.DrugService"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"ddh"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>ref</span><span> </span><span class='attribute'>bean</span><span>=</span><span class='attribute-value'>"DrugDAOHibernate"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"/drugQuery"</span><span> </span><span class='attribute'>abstract</span><span>=</span><span class='attribute-value'>"false"</span><span> </span><span class='attribute'>lazy-init</span><span>=</span><span class='attribute-value'>"default"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>autowire</span><span>=</span><span class='attribute-value'>"default"</span><span> </span><span class='attribute'>dependency-check</span><span>=</span><span class='attribute-value'>"default"</span><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"com.trade.struts.action.DrugAction"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"ds"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>ref</span><span> </span><span class='attribute'>bean</span><span>=</span><span class='attribute-value'>"DrugService"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span class='tag'><span class='tag-name'>beans</span><span class='tag'>></span><span> </span> </span></span></li>
</ol></div>
<p>三、DAOImpl、Service 业务逻辑部分</p>
<p>因为主要是介绍分页,这里只放分页相关代码</p>
<p>DrugDAOHibernate类,这里使用的方法参考(抄袭。。)了Robbin等关于使用离线Criteria实现查询的代码</p>
<div class='code_title'>java 代码</div>
<div class='dp-highlighter'><ol class='dp-j'>
<li class='alt'><span><span class='keyword'>package</span><span> com.trade.dao.hibernate; </span></span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> java.util.List; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.apache.commons.logging.Log; </span> </li>
<li><span class='keyword'>import</span><span> org.apache.commons.logging.LogFactory; </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.hibernate.Criteria; </span> </li>
<li><span class='keyword'>import</span><span> org.hibernate.HibernateException; </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.hibernate.Session; </span> </li>
<li><span class='keyword'>import</span><span> org.hibernate.criterion.DetachedCriteria; </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.hibernate.criterion.Projections; </span> </li>
<li><span class='keyword'>import</span><span> org.springframework.orm.hibernate3.HibernateCallback; </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.springframework.orm.hibernate3.support.HibernateDaoSupport; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> com.trade.dao.IDrugDAO; </span> </li>
<li><span class='keyword'>import</span><span> com.trade.vo.DrugDAO; </span> </li>
<li class='alt'><span class='keyword'>import</span><span> com.trade.vo.rush.Drug; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>public</span><span> </span><span class='keyword'>class</span><span> DrugDAOHibernate </span><span class='keyword'>extends</span><span> HibernateDaoSupport </span><span class='keyword'>implements</span><span> IDrugDAO { </span> </li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='keyword'>private</span><span> </span><span class='keyword'>static</span><span> </span><span class='keyword'>final</span><span> Log log = LogFactory.getLog(DrugDAO.</span><span class='keyword'>class</span><span>); </span> </li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='comment'>//获得 符合查询条件的记录数目 </span><span> </span> </li>
<li><span> </span><span class='keyword'>public</span><span> Long getDrugCount(</span><span class='keyword'>final</span><span> DetachedCriteria detachedCriteria) { </span> </li>
<li class='alt'><span> </span><span class='keyword'>return</span><span> (Long)getHibernateTemplate().execute(</span><span class='keyword'>new</span><span> HibernateCallback() { </span> </li>
<li><span> </span><span class='keyword'>public</span><span> Object doInHibernate(Session session) </span><span class='keyword'>throws</span><span> HibernateException { </span> </li>
<li class='alt'><span> Criteria criteria = detachedCriteria.getExecutableCriteria(session); </span> </li>
<li><span> Long count= Long.parseLong((criteria.setProjection(Projections.rowCount()).uniqueResult()).toString()); </span> </li>
<li class='alt'><span> criteria.setProjection(</span><span class='keyword'>null</span><span>); </span> </li>
<li><span> </span><span class='keyword'>return</span><span> count; </span> </li>
<li class='alt'><span> } </span> </li>
<li><span> }, </span><span class='keyword'>true</span><span>); </span> </li>
<li class='alt'><span> } </span> </li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='comment'>//根据条件进行查询、分页 </span><span> </span> </li>
<li><span> </span><span class='keyword'>public</span><span> List listByConditions(</span><span class='keyword'>final</span><span> DetachedCriteria detachedCriteria,</span><span class='keyword'>final</span><span> </span><span class='keyword'>int</span><span> pageNo,</span><span class='keyword'>final</span><span> </span><span class='keyword'>int</span><span> pageSize) { </span> </li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='keyword'>return</span><span> (List) getHibernateTemplate().execute(</span><span class='keyword'>new</span><span> HibernateCallback() { </span> </li>
<li class='alt'><span> </span><span class='keyword'>public</span><span> Object doInHibernate(Session session) </span><span class='keyword'>throws</span><span> HibernateException { </span> </li>
<li><span> Criteria criteria = detachedCriteria.getExecutableCriteria(session); </span> </li>
<li class='alt'><span> criteria.setMaxResults(pageSize); </span> </li>
<li><span> criteria.setFirstResult((pageNo-</span><span class='number'>1</span><span>)*pageSize); </span> </li>
<li class='alt'><span> </span><span class='keyword'>return</span><span> criteria.list(); </span> </li>
<li><span> } </span> </li>
<li class='alt'><span> }, </span><span class='keyword'>true</span><span>); </span> </li>
<li><span> } </span> </li>
<li class='alt'><span> </span> </li>
<li><span> } </span> </li>
</ol></div>
<p>Service层很简单</p>
<div class='code_title'>java 代码</div>
<div class='dp-highlighter'><ol class='dp-j'>
<li class='alt'><span><span class='keyword'>package</span><span> com.trade.service; </span></span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> java.util.List; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.hibernate.criterion.DetachedCriteria; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> com.trade.dao.hibernate.DrugDAOHibernate; </span> </li>
<li><span> </span> </li>
<li class='alt'><span> </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>public</span><span> </span><span class='keyword'>class</span><span> DrugService { </span> </li>
<li><span class='comment'>//由Spring注入DrugDAOHibernate实例 </span><span> </span> </li>
<li class='alt'><span class='keyword'>private</span><span> DrugDAOHibernate ddh ; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>public</span><span> DrugDAOHibernate getDdh() { </span> </li>
<li><span> </span><span class='keyword'>return</span><span> ddh; </span> </li>
<li class='alt'><span>} </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>public</span><span> </span><span class='keyword'>void</span><span> setDdh(DrugDAOHibernate ddh) { </span> </li>
<li><span> </span><span class='keyword'>this</span><span>.ddh = ddh; </span> </li>
<li class='alt'><span>} </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='comment'>//获得满足条件的记录数 </span><span> </span> </li>
<li><span class='keyword'>public</span><span> List listByConditions(</span><span class='keyword'>final</span><span> DetachedCriteria detachedCriteria,</span><span class='keyword'>final</span><span> </span><span class='keyword'>int</span><span> pageNo,</span><span class='keyword'>final</span><span> </span><span class='keyword'>int</span><span> pageSize){ </span> </li>
<li class='alt'><span> </span><span class='keyword'>return</span><span> ddh.listByConditions(detachedCriteria,pageNo,pageSize); </span> </li>
<li><span> </span> </li>
<li class='alt'><span>} </span> </li>
<li><span class='comment'>//根据条件进行查询、分页 </span><span> </span> </li>
<li class='alt'><span class='keyword'>public</span><span> Long getDrugCount(</span><span class='keyword'>final</span><span> DetachedCriteria detachedCriteria){ </span> </li>
<li><span> </span><span class='keyword'>return</span><span> ddh.getDrugCount(detachedCriteria); </span> </li>
<li class='alt'><span>} </span> </li>
<li><span>} </span> </li>
</ol></div>
</span></span>字数有限制。。请见下文。。</div>
<p> </p>
<div class='quote_div'><span style='font-family: Arial;'>
<p>2个月前,因为项目需要开始接触到SSH ,以前只用过JSP 开发过项目,所以对于SSH 一切都是新鲜玩意,虽然学习的过程是痛苦的,但是解决问题的快乐只有自己能体会到。<br/>在这里是我第一次写Blog,一是为了能够和这里的朋友共同进步,二是为了记录自己收获的点点滴滴,三是为了能够给和我一样的初学者分享所得——严重跑题。。 </p>
<p><span style='font-family: Arial;'>言归正传,下面是我的实现方法</span></p>
<span style='font-family: Arial;'>
<p><br/>一、开发环境<br/>JDK1.5 Hibernate 3.1 Struts 1.2 Spring2.0 MyEclplise 5.5GA</p>
<p>二、Struts 和 Spring配置<br/> Struts配置文件</p>
<div class='code_title'>xml 代码</div>
<div class='dp-highlighter'><ol class='dp-xml'>
<li class='alt'><span><span class='tag'><!----><span class='tag-name'>xml</span><span> </span><span class='attribute'>version</span><span>=</span><span class='attribute-value'>"1.0"</span><span> </span><span class='attribute'>encoding</span><span>=</span><span class='attribute-value'>"UTF-8"</span><span class='tag'>?></span><span> </span></span> </span></li>
<li><span><!----><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
<li><span class='tag'><</span><span class='tag-name'>struts-config</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>data-sources</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>form-beans</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>form-bean</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"drugForm"</span><span> </span><span class='attribute'>type</span><span>=</span><span class='attribute-value'>"com.trade.struts.form.DrugForm"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>form-bean</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"drugQueryForm"</span><span> </span><span class='attribute'>type</span><span>=</span><span class='attribute-value'>"com.trade.struts.queryform.DrugQueryForm"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>form-bean</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"baseForm"</span><span> </span><span class='attribute'>type</span><span>=</span><span class='attribute-value'>"com.trade.struts.queryform.BaseForm"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>form-beans</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>global-exceptions</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>global-forwards</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>action-mappings</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>action</span><span> </span> </li>
<li><span> </span><span class='attribute'>attribute</span><span>=</span><span class='attribute-value'>"drugQueryForm"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>input</span><span>=</span><span class='attribute-value'>"/Drug/drug_list.jsp"</span><span> </span> </li>
<li><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"drugQueryForm"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>parameter</span><span>=</span><span class='attribute-value'>"method"</span><span> </span> </li>
<li><span> </span><span class='attribute'>path</span><span>=</span><span class='attribute-value'>"/drugQuery"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>scope</span><span>=</span><span class='attribute-value'>"request"</span><span> </span> </li>
<li><span> </span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>forward</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"success"</span><span> </span><span class='attribute'>path</span><span>=</span><span class='attribute-value'>"/index.jsp"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>forward</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"list"</span><span> </span><span class='attribute'>path</span><span>=</span><span class='attribute-value'>"/Drug/drug_list.jsp"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>action</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>action-mappings</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='comments'><!----></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>controller</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>set-property</span><span> </span><span class='attribute'>property</span><span>=</span><span class='attribute-value'>"processorClass"</span><span> </span> </li>
<li><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"org.springframework.web.struts.DelegatingRequestProcessor"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>controller</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='comments'><!----></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>message-resources</span><span> </span><span class='attribute'>parameter</span><span>=</span><span class='attribute-value'>"com.trade.struts.ApplicationResources"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='comments'><!----></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>plug-in</span><span> </span> </li>
<li><span> </span><span class='attribute'>className</span><span>=</span><span class='attribute-value'>"org.springframework.web.struts.ContextLoaderPlugIn"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>set-property</span><span> </span><span class='attribute'>property</span><span>=</span><span class='attribute-value'>"contextConfigLocation"</span><span> </span> </li>
<li><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"/WEB-INF/applicationContext.xml"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>plug-in</span><span class='tag'>></span><span> </span> </span></li>
<li><span class='tag'><span class='tag-name'>struts-config</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
</ol></div>
<p> Spring配置代码</p>
<div class='code_title'>xml 代码</div>
<div class='dp-highlighter'><ol class='dp-xml'>
<li class='alt'><span><span class='tag'><!----><span class='tag-name'>xml</span><span> </span><span class='attribute'>version</span><span>=</span><span class='attribute-value'>"1.0"</span><span> </span><span class='attribute'>encoding</span><span>=</span><span class='attribute-value'>"UTF-8"</span><span class='tag'>?></span><span> </span></span> </span></li>
<li><span class='tag'><</span><span class='tag-name'>beans</span><span> </span><span class='attribute'>xmlns</span><span>=</span><span class='attribute-value'>"http://www.springframework.org/schema/beans"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>xmlns:xsi</span><span>=</span><span class='attribute-value'>"http://www.w3.org/2001/XMLSchema-instance"</span><span> </span> </li>
<li><span> </span><span class='attribute'>xsi:schemaLocation</span><span>=</span><span class='attribute-value'>"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='comments'><!----></span><span> </span> </li>
<li class='alt'><span> </span><span class='comments'><!----></span><span> </span> </li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"dataSourceTrade"</span><span> </span> </li>
<li><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"org.apache.commons.dbcp.BasicDataSource"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"driverClassName"</span><span> </span> </li>
<li><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"com.microsoft.jdbc.sqlserver.SQLServerDriver"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"url"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=Trade"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"username"</span><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"share"</span><span class='tag'>></span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"password"</span><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"sa"</span><span class='tag'>></span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"sessionFactoryTrade"</span><span> </span> </li>
<li><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"org.springframework.orm.hibernate3.LocalSessionFactoryBean"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"dataSource"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>ref</span><span> </span><span class='attribute'>bean</span><span>=</span><span class='attribute-value'>"dataSourceTrade"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"hibernateProperties"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>props</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"hibernate.dialect"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> org.hibernate.dialect.SQLServerDialect </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"hibernate.show_sql"</span><span class='tag'>></span><span>true</span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"current_session_context_class"</span><span class='tag'>></span><span>thread</span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"connection.pool_size"</span><span class='tag'>></span><span>10</span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"hibernate.hbm2ddl.auto"</span><span class='tag'>></span><span>update</span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>props</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"mappingResources"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>list</span><span class='tag'>></span><span class='tag'><span class='tag-name'>list</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='comments'><!----></span><span> </span> </li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"dataSourceRush"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"org.apache.commons.dbcp.BasicDataSource"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"driverClassName"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"com.microsoft.jdbc.sqlserver.SQLServerDriver"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"url"</span><span> </span> </li>
<li><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=Rush"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"username"</span><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"share"</span><span class='tag'>></span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"password"</span><span> </span><span class='attribute'>value</span><span>=</span><span class='attribute-value'>"sa"</span><span class='tag'>></span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"sessionFactoryRush"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"org.springframework.orm.hibernate3.LocalSessionFactoryBean"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"dataSource"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>ref</span><span> </span><span class='attribute'>bean</span><span>=</span><span class='attribute-value'>"dataSourceTrade"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"hibernateProperties"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>props</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>prop</span><span> </span><span class='attribute'>key</span><span>=</span><span class='attribute-value'>"hibernate.dialect"</span><span class='tag'>></span><span> </span> </li>
<li><span> org.hibernate.dialect.SQLServerDialect </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>prop</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>props</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"mappingResources"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>list</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>value</span><span class='tag'>></span><span>com/trade/vo/rush/Drug.hbm.xml</span><span class='tag'><span class='tag-name'>value</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>list</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span><span> </span> </li>
<li class='alt'><span> </span><span class='comments'><!----></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"DrugDAOHibernate"</span><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"com.trade.dao.hibernate.DrugDAOHibernate"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"sessionFactory"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>ref</span><span> </span><span class='attribute'>bean</span><span>=</span><span class='attribute-value'>"sessionFactoryRush"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"DrugService"</span><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"com.trade.service.DrugService"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"ddh"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>ref</span><span> </span><span class='attribute'>bean</span><span>=</span><span class='attribute-value'>"DrugDAOHibernate"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>bean</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"/drugQuery"</span><span> </span><span class='attribute'>abstract</span><span>=</span><span class='attribute-value'>"false"</span><span> </span><span class='attribute'>lazy-init</span><span>=</span><span class='attribute-value'>"default"</span><span> </span> </li>
<li class='alt'><span> </span><span class='attribute'>autowire</span><span>=</span><span class='attribute-value'>"default"</span><span> </span><span class='attribute'>dependency-check</span><span>=</span><span class='attribute-value'>"default"</span><span> </span><span class='attribute'>class</span><span>=</span><span class='attribute-value'>"com.trade.struts.action.DrugAction"</span><span class='tag'>></span><span> </span> </li>
<li><span> </span><span class='tag'><</span><span class='tag-name'>property</span><span> </span><span class='attribute'>name</span><span>=</span><span class='attribute-value'>"ds"</span><span class='tag'>></span><span> </span> </li>
<li class='alt'><span> </span><span class='tag'><</span><span class='tag-name'>ref</span><span> </span><span class='attribute'>bean</span><span>=</span><span class='attribute-value'>"DrugService"</span><span> </span><span class='tag'>/></span><span> </span> </li>
<li><span> </span><span class='tag'><span class='tag-name'>property</span><span class='tag'>></span><span> </span> </span></li>
<li class='alt'><span> </span><span class='tag'><span class='tag-name'>bean</span><span class='tag'>></span><span class='tag'><span class='tag-name'>beans</span><span class='tag'>></span><span> </span> </span></span></li>
</ol></div>
<p>三、DAOImpl、Service 业务逻辑部分</p>
<p>因为主要是介绍分页,这里只放分页相关代码</p>
<p>DrugDAOHibernate类,这里使用的方法参考(抄袭。。)了Robbin等关于使用离线Criteria实现查询的代码</p>
<div class='code_title'>java 代码</div>
<div class='dp-highlighter'><ol class='dp-j'>
<li class='alt'><span><span class='keyword'>package</span><span> com.trade.dao.hibernate; </span></span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> java.util.List; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.apache.commons.logging.Log; </span> </li>
<li><span class='keyword'>import</span><span> org.apache.commons.logging.LogFactory; </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.hibernate.Criteria; </span> </li>
<li><span class='keyword'>import</span><span> org.hibernate.HibernateException; </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.hibernate.Session; </span> </li>
<li><span class='keyword'>import</span><span> org.hibernate.criterion.DetachedCriteria; </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.hibernate.criterion.Projections; </span> </li>
<li><span class='keyword'>import</span><span> org.springframework.orm.hibernate3.HibernateCallback; </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.springframework.orm.hibernate3.support.HibernateDaoSupport; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> com.trade.dao.IDrugDAO; </span> </li>
<li><span class='keyword'>import</span><span> com.trade.vo.DrugDAO; </span> </li>
<li class='alt'><span class='keyword'>import</span><span> com.trade.vo.rush.Drug; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>public</span><span> </span><span class='keyword'>class</span><span> DrugDAOHibernate </span><span class='keyword'>extends</span><span> HibernateDaoSupport </span><span class='keyword'>implements</span><span> IDrugDAO { </span> </li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='keyword'>private</span><span> </span><span class='keyword'>static</span><span> </span><span class='keyword'>final</span><span> Log log = LogFactory.getLog(DrugDAO.</span><span class='keyword'>class</span><span>); </span> </li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='comment'>//获得 符合查询条件的记录数目 </span><span> </span> </li>
<li><span> </span><span class='keyword'>public</span><span> Long getDrugCount(</span><span class='keyword'>final</span><span> DetachedCriteria detachedCriteria) { </span> </li>
<li class='alt'><span> </span><span class='keyword'>return</span><span> (Long)getHibernateTemplate().execute(</span><span class='keyword'>new</span><span> HibernateCallback() { </span> </li>
<li><span> </span><span class='keyword'>public</span><span> Object doInHibernate(Session session) </span><span class='keyword'>throws</span><span> HibernateException { </span> </li>
<li class='alt'><span> Criteria criteria = detachedCriteria.getExecutableCriteria(session); </span> </li>
<li><span> Long count= Long.parseLong((criteria.setProjection(Projections.rowCount()).uniqueResult()).toString()); </span> </li>
<li class='alt'><span> criteria.setProjection(</span><span class='keyword'>null</span><span>); </span> </li>
<li><span> </span><span class='keyword'>return</span><span> count; </span> </li>
<li class='alt'><span> } </span> </li>
<li><span> }, </span><span class='keyword'>true</span><span>); </span> </li>
<li class='alt'><span> } </span> </li>
<li><span> </span> </li>
<li class='alt'><span> </span><span class='comment'>//根据条件进行查询、分页 </span><span> </span> </li>
<li><span> </span><span class='keyword'>public</span><span> List listByConditions(</span><span class='keyword'>final</span><span> DetachedCriteria detachedCriteria,</span><span class='keyword'>final</span><span> </span><span class='keyword'>int</span><span> pageNo,</span><span class='keyword'>final</span><span> </span><span class='keyword'>int</span><span> pageSize) { </span> </li>
<li class='alt'><span> </span> </li>
<li><span> </span><span class='keyword'>return</span><span> (List) getHibernateTemplate().execute(</span><span class='keyword'>new</span><span> HibernateCallback() { </span> </li>
<li class='alt'><span> </span><span class='keyword'>public</span><span> Object doInHibernate(Session session) </span><span class='keyword'>throws</span><span> HibernateException { </span> </li>
<li><span> Criteria criteria = detachedCriteria.getExecutableCriteria(session); </span> </li>
<li class='alt'><span> criteria.setMaxResults(pageSize); </span> </li>
<li><span> criteria.setFirstResult((pageNo-</span><span class='number'>1</span><span>)*pageSize); </span> </li>
<li class='alt'><span> </span><span class='keyword'>return</span><span> criteria.list(); </span> </li>
<li><span> } </span> </li>
<li class='alt'><span> }, </span><span class='keyword'>true</span><span>); </span> </li>
<li><span> } </span> </li>
<li class='alt'><span> </span> </li>
<li><span> } </span> </li>
</ol></div>
<p>Service层很简单</p>
<div class='code_title'>java 代码</div>
<div class='dp-highlighter'><ol class='dp-j'>
<li class='alt'><span><span class='keyword'>package</span><span> com.trade.service; </span></span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> java.util.List; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> org.hibernate.criterion.DetachedCriteria; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>import</span><span> com.trade.dao.hibernate.DrugDAOHibernate; </span> </li>
<li><span> </span> </li>
<li class='alt'><span> </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>public</span><span> </span><span class='keyword'>class</span><span> DrugService { </span> </li>
<li><span class='comment'>//由Spring注入DrugDAOHibernate实例 </span><span> </span> </li>
<li class='alt'><span class='keyword'>private</span><span> DrugDAOHibernate ddh ; </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>public</span><span> DrugDAOHibernate getDdh() { </span> </li>
<li><span> </span><span class='keyword'>return</span><span> ddh; </span> </li>
<li class='alt'><span>} </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='keyword'>public</span><span> </span><span class='keyword'>void</span><span> setDdh(DrugDAOHibernate ddh) { </span> </li>
<li><span> </span><span class='keyword'>this</span><span>.ddh = ddh; </span> </li>
<li class='alt'><span>} </span> </li>
<li><span> </span> </li>
<li class='alt'><span class='comment'>//获得满足条件的记录数 </span><span> </span> </li>
<li><span class='keyword'>public</span><span> List listByConditions(</span><span class='keyword'>final</span><span> DetachedCriteria detachedCriteria,</span><span class='keyword'>final</span><span> </span><span class='keyword'>int</span><span> pageNo,</span><span class='keyword'>final</span><span> </span><span class='keyword'>int</span><span> pageSize){ </span> </li>
<li class='alt'><span> </span><span class='keyword'>return</span><span> ddh.listByConditions(detachedCriteria,pageNo,pageSize); </span> </li>
<li><span> </span> </li>
<li class='alt'><span>} </span> </li>
<li><span class='comment'>//根据条件进行查询、分页 </span><span> </span> </li>
<li class='alt'><span class='keyword'>public</span><span> Long getDrugCount(</span><span class='keyword'>final</span><span> DetachedCriteria detachedCriteria){ </span> </li>
<li><span> </span><span class='keyword'>return</span><span> ddh.getDrugCount(detachedCriteria); </span> </li>
<li class='alt'><span>} </span> </li>
<li><span>} </span> </li>
</ol></div>
</span></span>字数有限制。。请见下文。。</div>
<p> </p>
1 楼
bobomtv17
2007-12-13
很不错的例子,我也在学习SSH,谢谢了!
相关推荐
根据提供的标题、描述、标签及部分内容,我们可以了解到这篇文章主要探讨的是如何在Struts2与Hibernate框架结合下实现分页功能。接下来将详细解析Struts2与Hibernate如何协作完成这一任务。 ### Struts2与Hibernate...
2. **在DAO层实现分页查询**:使用Hibernate的Criteria、HQL或者SQL配合分页参数(如页码和每页大小)来执行分页查询。 3. **在Service层处理分页逻辑**:Service层根据业务需求,调用DAO层的分页查询方法,并返回...
JAVA分页大全.doc Hibernate分页查询.doc hibernate实现分页技术.txt struts实现分页技术源代码txt Struts+hibernate制作...spring+struts+hibernate 分页问题.txt spring+hibernate分页技术.txt jsp实现分页技术.txt
SSH(Struts、Spring、Hibernate)集成是Java Web开发中的常见模式,用于创建复杂的业务逻辑和数据管理。在本资料中,我们将深入探讨如何利用这三大框架实现分页功能。 首先,Struts作为MVC(模型-视图-控制器)...
SSH(Struts、Hibernate、Spring)是Java Web开发中一种常见的技术栈,它结合了MVC框架Struts、对象关系映射工具Hibernate和依赖注入框架Spring。这些组件协同工作,为开发者提供了一种高效、灵活的开发环境。下面将...
在本章中,我们将深入探讨如何开发一个基于Struts、Spring、Hibernate和Ajax的网上信息发布平台。这四个技术是Java Web开发中的核心组件,它们各自承担着不同的职责,共同构建了一个强大而灵活的后端架构。 **...
struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页
在这个“Struts2 Hibernate Spring分页实例”中,开发者将展示如何在这些框架的集成环境中实现数据的分页显示。分页是Web应用中常见的一种功能,它能够帮助用户更好地浏览和管理大量的数据,避免一次性加载过多内容...
在这个项目中,“spring+struts2+hibernate整合实现分页”是一个基础教程,旨在帮助初学者理解如何在SSH框架下实现数据的分页展示。下面将详细讲解这个主题中的关键知识点。 1. **Spring框架**:Spring是Java应用的...
搜索功能通常涉及到条件查询,可以利用Hibernate3的Criteria API或者HQL动态构造查询语句,根据用户输入的关键词匹配数据库中的记录。搜索结果同样可以结合分页进行展示。 总的来说,这个"Struts2+Spring2+...
总结来说,使用Hibernate、Struts和Spring实现的通用分页查询,涉及了Java Web开发中的多个层次,包括模型、持久层接口、持久层实现、业务层和服务层的交互,以及视图层的渲染。这种分页机制灵活且可复用,可以适应...
总的来说,通过Spring、Struts和Hibernate的集成,我们可以构建出一个强大的Web应用框架,其中分页功能的实现主要依赖于Spring管理的SessionFactory、Struts的Action和Spring的IoC容器,以及自定义的PageUtil和Page...
Struts2 hibernate spring分页配套lib(2) 上传的可真慢啊 Struts2 hibernate spring分页配套lib(3)我始终上传不上去 不知道怎么弄的 例子运行http://localhost:8080/page/emp/list.action
总结来说,这个项目展示了如何在Struts、Hibernate和Spring三大框架下实现分页功能,涵盖了前端请求处理、后端分页逻辑以及数据查询的各个方面。无论是SQL还是HQL,都能有效地应用于分页场景,开发者可以根据项目...
在SSH框架下,可以通过Struts的拦截器或Spring的Pageable接口来实现分页查询,从而避免一次性加载大量数据导致的性能问题。 DWR(Direct Web Remoting)是一种JavaScript到Java的远程调用技术,它使得前端能够直接...
Struts2 hibernate spring分页配套lib 共三个分卷 这是一
Struts2 hibernate spring分页配套lib(3) 上次怎么没成功啊
在分页实现中,Spring可以管理Struts的Action类以及Hibernate的数据访问对象(DAO)。通过Spring的IOC(Inversion of Control,控制反转),我们可以将DAO的实例注入到Action中,使得Action无需关心DAO的具体创建...
这是Java常用框架阶段在为学生答疑时做的一个SSH整合分页实例(第一部分),涉及了SSH整合、日期模糊查询、查询条件绑定、查询结果排序等功能,希望能为大家提供一些思路。 分页是绝大多数MIS系统必需的功能之一,...
"ext3+struts2+hibernate+spring的CRUD+分页"是一个典型的Java Web开发组合,用于实现全面的数据管理功能。这个组合充分利用了各个框架的优势,提供了一个强大的后端数据处理和前端展示的解决方案。 首先,EXT3是一...