hibernate
核心安装包下的
:
hibernate3.jar
lib/required/*.jar
lib/optional/ehcache-1.2.3.jar
hibernate
注解安装包下的
lib/test/slf4j-log4j12.jar
Spring
安装包下的
dist/spring.jar
dist/modules/spring-webmvc-struts.jar
lib/jakarta-commons/commons-logging.jar
、
commons-dbcp.jar
、
commons-pool.jar
lib/aspectj/aspectjweaver.jar
、
aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
lib/j2ee/common-annotations.jar
lib/log4j/log4j-1.2.15.jar
Struts
下载
struts-1.3.8-lib.zip
,需要使用到解压目录下的所有
jar
,建议把
jstl-1.0.2.jar
和
standard-1.0.2.jar
更换为
1.1
版本。
Spring
中已经存在一个
antlr-2.7.6.jar
,所以把
struts
中的
antlr-2.7.2.jar
删除,避免
jar
冲突。
数据库驱动
jar
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
.....
略
</bean>
<bean id="
sessionFactory
" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>
<bean id="txManager" class="
org.springframework.orm.hibernate3.HibernateTransactionManager
">
<property name="sessionFactory" ref="
sessionFactory
"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
实体
bean
配置模版
.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.bean">
<class name="Person" table="person">
<id name="id" type="integer" >
<generator class="native"/>
</id>
<property name="name" length="10" not-null="true"/>
</class>
</hibernate-mapping>
Hibernate
二级缓存的配置
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>
在需要缓存的实体
bean
配置文件中加入缓存配置项
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.bean">
<class name="Person" table="person">
<cache usage="read-write" region="cn.itcast.bean.Person"/>
<id name="id">
<generator class="native"/>
</id>
<property name="name" length="10" not-null="true"/>
</class>
</hibernate-mapping>
usage
说明了缓存的策略,
region
指定缓存的区域名
Ehcache
默认的配置文件
ehcache.xml
(放在类路径下)
<ehcache>
<diskStore path="D:/cache"/>
<defaultCache maxElementsInMemory="1000“ eternal="false“ overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="180"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="60"/>
<cache name="
cn.itcast.bean.Person
" maxElementsInMemory="100" eternal="false"
overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false"/>
</ehcache>
defaultCache
节点为缺省的缓存策略
maxElementsInMemory
内存中最大允许存在的对象数量
eternal
设置缓存中的对象是否永远不过期
overflowToDisk
把溢出的对象存放到硬盘上
timeToIdleSeconds
指定缓存对象空闲多长时间就过期
,
过期的对象会被清除掉
timeToLiveSeconds
指定缓存对象总的存活时间
diskPersistent
当
jvm
结束是是否持久化对象
diskExpiryThreadIntervalSeconds
指定专门用于清除过期对象的监听线程的轮询时间
在
web
容器中实例化
spring
容器,
<!--
指定
spring
的配置文件,默认从
web
根目录寻找配置文件,我们可以通过
spring
提供的
classpath:
前缀指定从类路径下寻找
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!--
对
Spring
容器进行实例化
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在
web
容器中配置
struts
,
<servlet>
<servlet-name>struts</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>struts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
如果
action
没有交给
spring
管理时,我们通过下面语句获取
spring
容器实例
WebApplicationContext ctx =
WebApplicationContextUtils.
getWebApplicationContext(
this.getServlet().getServletContext());
把
action
交给
spring
管理后,我们可以使用依赖注入在
action
中注入业务层的
bean
。确保
action
的
path
属性值与
bean
的名称相同。
<action path="
/person/list"
...>
</action>
Spring
配置:
<bean name=
"/person/list"
class="cn.itcast.web.action.PersonAction"/>
在
struts
配置文件中添加进
spring
的请求控制器,该请法语控制器会先根据
action
的
path
属性值到
spring
容器中寻
找跟该属性值同名的
bean
。如果寻找到即使用该
bean
处理用户请求
<controller>
<set-property property="processorClass"
value="
org.springframework.web.struts.DelegatingRequestProcessor
"/>
</controller>
使用
spring
解决
struts1.3
乱码问题。
<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>
使用
spring
解决
hibernate
因
session
关闭导致的延迟加载例外问题。
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<!--
指定
org.springframework.orm.hibernate3.LocalSessionFactoryBean
在
spring
配置文件中的名称
,
默认值为
sessionFactory.
如果
LocalSessionFactoryBean
在
spring
中的名称不是
sessionFactory,
该参数一定要指定
,
否则会
出现找不到
sessionFactory
的例外
-->
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>