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);
- }
- }