在SSH项目中,包括以下几个元素
hibernate的sessionFactory负责数据库的配置
hibernate的hibernateTemplate负责访问数据库
DAO将hibernateTemplate封装
Service负责各个业务处理
Action负责请求和业务处理的匹配以及页面的跳转
接受用户请求,Action负责调用service利用dao对数据库进行CRUD(create,read,update,delete)操作,返回处理结果。
Struts获取用户的请求,根据配置把请求分配到相应的Action中,在Action调用Service,返回处理结果
下面来说一下Struts2和Spring的项目例子
建立所有Action的基类PubAction
所有的业务Service都放到PubAction中并实现RequestAware,SessionAware接口(还有ResponseAware,ApplicationAware不过不常用这里没用到)代码如下:
package com.thd.action; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import com.thd.service.article.ArticleService; import com.thd.service.pub.TreeService; /** * @author thd * @serial 所有系统Action的基类,所有需要的service陪在这个action中,其他类继承使用service */ public class PubAction implements RequestAware , SessionAware{ private TreeService treeService; private ArticleService articleService; private Map<String, Object> request; private Map<String, Object> session; public void setSession(Map<String, Object> arg0) { this.request = arg0; } public void setRequest(Map<String, Object> arg0) { this.session = arg0; } public Map<String, Object> getRequest() { return request; } public Map<String, Object> getSession() { return session; } public TreeService getTreeService() { return treeService; } public void setTreeService(TreeService treeService) { this.treeService = treeService; } public ArticleService getArticleService() { return articleService; } public void setArticleService(ArticleService articleService) { this.articleService = articleService; } }
建立模块的Action
模块的Action要继承PubAction,因为要用到的所有Service都放到了PubAction中,代码如下:
package com.thd.action.article; import com.thd.action.PubAction; import com.thd.pojo.Article; /** * * @author thd * @serial 文章模块的action,用于相关文章模块的所有操作 */ public class ArticleAction extends PubAction { private Article article; /* 标识是修改文章 还是新增文章 update:修改 save:新增*/ private String deal; /* * 新建/编辑文章初始化处理 */ public String articleSaveOrUpdate(){ /* * 修改操作 */ if(deal.equals("update")){ } return "articleSaveOrUpdate"; } /* * 新增/编辑文章提交处理 */ public String articleSaveOrUpdateSubmit(){ System.out.println(article.getTit()); System.out.println(article.getDes()); System.out.println(article.getCon()); System.out.println(article.getDic()); this.getArticleService().saveOrUpdate(article); return "articleSaveOrUpdate"; } public Article getArticle() { return article; } public void setArticle(Article article) { this.article = article; } public String getDeal() { return deal; } public void setDeal(String deal) { this.deal = deal; } }
在模块的Action中,要调用service直接调用getService就可以直接得到,需要那个拿哪个,在Spring配置中这些service都是配到pubAction中,所以添加或修改service在pubAction中操作,模块的Action中就负责去拿和用就可以了,不用再配置了。
建立Service接口和实现类
接口:
package com.thd.service.article; import com.thd.pojo.Article; /** * @author thd * @serial 文章模块的所有操作 */ public interface ArticleService { /** * 保存/更新 文章 如果id属性存在则是更新,如果不存在则是保存 * @param article 保存/更新的文章对象 */ public void saveOrUpdate(Article article); /** * 保存文章 * @param article 保存的文章对象 */ public void save(Article article); /** * 更新文章 * @param article 更新的文章 */ public void update(Article article); }
实现类:
package com.thd.serviceImpl.article; import java.util.Date; import com.thd.dao.DaoImpl; import com.thd.pojo.Article; import com.thd.service.article.ArticleService; /** * @author thd * */ public class ArticleServiceImpl implements ArticleService { private DaoImpl dao; /* * (non-Javadoc) * @see com.thd.service.article.ArticleService#saveOrUpdate(com.thd.pojo.Article) */ public void saveOrUpdate(Article article) { article.setIdate(new Date()); article.setState("1"); this.dao.saveOrUpdate(article); } /* * (non-Javadoc) * @see com.thd.service.article.ArticleService#save(com.thd.pojo.Article) */ public void save(Article article) { article.setIdate(new Date()); article.setState("1"); this.dao.save(article); } /* * (non-Javadoc) * @see com.thd.service.article.ArticleService#update(com.thd.pojo.Article) */ public void update(Article article) { article.setIdate(new Date()); this.dao.update(article); } public DaoImpl getDao() { return dao; } public void setDao(DaoImpl dao) { this.dao = dao; } }
实现类中要加入dao这个属性,以便用来访问数据库。在Spring配置文件中配置注入到service实现类中
建立Dao类
建立Dao类,用于访问数据库,代码如下:
package com.thd.dao; import java.io.Serializable; import java.sql.SQLException; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class DaoImpl extends HibernateDaoSupport { public void save(Object obj) { this.getHibernateTemplate().save(obj); }; public void update(Object obj) { this.getHibernateTemplate().update(obj); } public void delete(Object obj) { this.getHibernateTemplate().delete(obj); } public void saveOrUpdate(Object obj){ this.getHibernateTemplate().saveOrUpdate(obj); } public List findByHql(String hql){ return this.getHibernateTemplate().find(hql); } public Object findById(Class c,Serializable id){ return this.getHibernateTemplate().get(c, id); } public List findBySql(final String sql) { return this.getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) { Query query = session.createSQLQuery(sql); return query.list(); } }); } public void executeSql(final String sql) { this.getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) { Query query = session.createSQLQuery(sql); query.executeUpdate(); return null; } }); } public List findPageSql( final String sql,final int currpage, final int rowsNum, final int maxResult) { return this.getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session s) throws HibernateException, SQLException { Query query = s.createSQLQuery(sql); if (rowsNum != -1) { query.setFirstResult((currpage - 1) * maxResult); query.setMaxResults(maxResult); } List list = query.list(); return list; } }); } }
Spring配置文件
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.5.xsd"> <!-- hibernate sessionFactory配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <!-- hibernate hibernateTemplate配置 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 事务处理配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 事务处理配置 --> <bean id="serviceProxy" abstract="true" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="regist*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 数据库操作类配置 --> <bean id="dao" class="com.thd.dao.DaoImpl"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate"/> </property> </bean> <!-- service层配置 --> <bean id="treeServiceImpl" class="com.thd.serviceImpl.pub.TreeServiceImpl"> <property name="dao"> <ref bean="dao"/> </property> </bean> <bean id="treeServiceProxy" parent="serviceProxy"> <property name="target"> <ref bean="treeServiceImpl"/> </property> </bean> <bean id="articleServiceImpl" class="com.thd.serviceImpl.article.ArticleServiceImpl"> <property name="dao"> <ref bean="dao"/> </property> </bean> <bean id="articleServiceProxy" parent="serviceProxy"> <property name="target"> <ref bean="articleServiceImpl"/> </property> </bean> <!-- PubAction的配置 注入了所有业务的Service --> <bean id="pubAction" abstract="true" class="com.thd.action.PubAction"> <property name="treeService"> <ref bean="treeServiceProxy"/> </property> <property name="articleService"> <ref bean="articleServiceProxy"/> </property> </bean> <!-- 所有模块的Action配置,要继承pubAction!! --> <bean name="treeAction" class="com.thd.action.pub.TreeAction" parent="pubAction"></bean> <bean name="articleAction" class="com.thd.action.article.ArticleAction" parent="pubAction"></bean> </beans>