SSH - 基于注解的SSH框架整合包含Hibernate二级缓存

本文提供了一个详细的Spring+Struts2+Hibernate整合教程,包括所需JAR包的准备、web.xml配置、struts.xml配置、bean.xml配置等步骤,并介绍了二级缓存的配置方法。
由于本人习惯使用“注解”方式的配置,所以这里的事例都是基于“注解”的,还有就是我都是按步骤直接告诉大家如何搭建使用,没有冗长的理论在里面,理论部分,度娘和谷歌一搜一大把,我再粘过来一点意义都没有。

[color=red][b]1、找齐所有的JAR包[/b][/color]
[img]http://dl.iteye.com/upload/attachment/459609/444e7640-678e-36e3-a0fd-cfc1654e71f8.png[/img]

[color=red][b]2、配置web.xml[/b][/color]

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>

<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>/index</welcome-file>
</welcome-file-list>
</web-app>


[color=red][b]3、配置struts.xml(在src目录下建立struts.xml即可,这里是struts2)[/b][/color]

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!-- 去掉默认样式 -->
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.ui.templateDir" value="template" />
<constant name="struts.ui.templateSuffix" value="ftl" />

<!-- 国际化 -->
<!-- <constant name="struts.custom.i18n.resources" value="文件名称"></constant> -->
<!-- 开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- action交给spring创建 -->
<constant name="struts.objectFactory" value="spring" />

<!-- 默认配置 -->
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index"></default-action-ref>
<action name="index">
<result>/index.jsp</result>
</action>
</package>

<!-- ajax请求配置 -->
<package name="ajax" namespace="/ajax" extends="json-default">
<action name="*_*" class="{1}Action" method="{2}">
<result type="json" name="success"></result>
</action>
</package>
</struts>


[color=red][b]4、配置bean.xml(在src目录下建立bean.xml即可,这里是Spring2.5)[/b][/color]

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 注解方式 -->
<context:annotation-config />
<!-- 自动检测,查找com.main包下包含注解的类,需要替换 -->
<context:component-scan base-package="com.main" />
<!-- datasource赋值,这里用的是JNDI的方式 -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/main_movie"/>
</bean>
<!-- 配置sessionFactory -->
<!-- 使用注解的方式 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<!-- 自动扫描com.main.movie.entity下带注解的实体类,需要替换 --> <value>com.main.movie.entity</value>
</list>
</property>
<!-- 配置hibernate属性 -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
<!-- 设置批量处理值 -->
hibernate.jdbc.batch_size=20
<!-- 以下3条是用于配置hibernate二级缓存的,这里用的是EhCatch,所以还需要ehcatch的jar包 -->

hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
</value>
</property>
</bean>

<!-- 配置事务 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 设置事务包含的方法,需要替换 -->
<aop:pointcut id="defaultServiceMethods" expression="execution(* com.main.*.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="defaultServiceMethods" />
</aop:config>

<!-- 配置hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>


[color=red][b]5、配置ehcache.xml(配置二级缓存的设置)[/b][/color]

<ehcache>
<diskStore path="java.io.tmpdir" />
<!--
maxElementsInMemory 缓存对象的数量
eternal 是否可以在内存中清除
timeToIdleSeconds 多少秒没人访问就清除
timeToLiveSeconds 生成了多长时间就可以清除 需要长时间存在就放在这
overflowToDisk 溢出时放到硬盘上
-->
<defaultCache maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="1200"
overflowToDisk="true" />
</ehcache>


[color=red][b]6、创建类HibernateTempUtil[/b][/color]

package com.main.movie.util;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;

public abstract class HibernateTempUtil {
private HibernateTemplate hibernateTemplate;

public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}

@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}


[color=red][b]7、在DAO层使用Hibernate[/b][/color]
这里稍微说明一下
1、@Component("adminDao"),是设置bean的名称为adminDao,当然在service层注入的时候就需要用到注解@Resource(name = "adminDao"),注意是在需要注入的属性的setter方法上写注解,其它地方同理
2、DAO的实现需要继承刚才建立的HibernateTempUtil,其实就是为了注入Template
3、用Hibernate查询的时候我用的是Spring的回调函数,回调函数要想使用外面方法传进来的参数,该参数必须是final型的

package com.main.movie.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import com.main.movie.dao.IAdminDao;
import com.main.movie.entity.MovAdmin;
import com.main.movie.exception.DaoException;
import com.main.movie.util.HibernateTempUtil;
import com.main.movie.util.HqlUtil;

/**
* 管理员维护
* @author main
*
*/
@Component("adminDao")
public class AdminDaoImpl extends HibernateTempUtil implements IAdminDao {
/**
* 管理员列表(带分页)
* @param page
* @param pageSize
* @return
* @throws DaoException
*/
@SuppressWarnings("unchecked")
public List<MovAdmin> findAdminList(final int page, final int pageSize) throws DaoException {
try {
List<MovAdmin> adminList = super.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
String hql = "from MovAdmin admin order by admin.ltime desc, admin.ctime asc";
Query q = session.createQuery(hql);
q.setFirstResult((page-1) * pageSize);
q.setMaxResults(pageSize);
return q.list();
}
});
return adminList;
} catch (DataAccessException e) {
throw new DaoException(e.getMessage(), e);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值