程序员开发实例大全宝库

网站首页 > 编程文章 正文

spring框架快速入门集成hibernate、struts、JPA

zazugpt 2024-09-03 02:58:15 编程文章 26 ℃ 0 评论

spring2.5+hibernate3.3+struts1.3整合开发:

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-loggiong.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,解压包下所有的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

hibernate二级缓存的配置:

<bean 
    id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    
    <property name="dataSource" ref="dataSource"/> 
    <property name="mappingResources"> 
      <list> 
         <value>com/example/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> 

// 还要在类路径下放一个encache.xml文件; 
<?xml version="1.0" encoding="UTF-8"?> 
    <encache> 
        <diskStore path="d:\cache"/> 
        <defaultCache maxElementsInMemory="1000" 
                      enternal="false" 
                      overflowToDisk="true" 
                      timeToIdleSeconds="120" 
                      timeToLiveSeconds="180" 
                      diskPersistent="false" 
                      diskExpiryThreadIntervalSeconds="60"/> 
  </encache>

spring针对hibernate的事务管理器:

<bean id="txManager" 
		  class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
   <property name="sessionFactory" ref="sessionFactory"/> 
</bean>

<tx:annotation-driven transaction-manager="txManager"/>

spring可以自动管理session对象,所以不需要sessionFactory.openSession();只需要sessionFactory.getCurrentSession(),这是取得spring管理的那个session.


spring与struts集成

方案一: 在 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-parrern> 
</servlet-mapping>

如果action没有交给spring管理时,我们可以通过下面的语句获取spring容器实例:

 WebApplicationcontext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getService().getServiceContext());

把action交给spring管理后,我们可以使用依赖注入在action中注入业务层的bean。

注意两点:

一是确保action的path属性值与bean的名称相同。

<action path="/person/list" ...></action> 

//spring配置:
<bean name="/person/list" class="cn.itcast.web.action.PersonAction"/> 

二是在struts-config.xml中配置一个控制器

<controller> 
  <set-property property="processorClass" 
								value="org.springframework.web.struts.DelegatingRequestProcessor"/> 
</controller>

spring解决struts1.3乱码问题:

在web容器里:

<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> 
</filter> 

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

spring2.5+jpa+struts1.3整合开发:

hibernate-distribution-3.3.1.ga: hibernate3.jar,lib.bytecode/cglib/hibernate-cglib-repack-2.1_3.jar,lib/required/*.jar hiberante-annotations-3.4.0.ga: hibernate-annotations.jar,lib/ejb3-persistence.jar,hibernate-commons-annotations.jar hibernate-entitymanager-3.4.0.ga: hibernate-entitymanager.jar,lib/test/log4j.jar,slf4j-log4j12.jar

<bean id="entityManagerFactory" 
			class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
  		<property name="persistenceUnitName" value="itcast"/> 
</bean> 

<bean id="txManager" 
			class="org.springframework.orm.jpa.JpaTransactionManager"> 
  		<property name="entityManagerFactory" ref="entityManagerFactory"/> 
</bean> 

<tx:annotation-driven transaction-manager="txManager"/>

在依赖注入时:@PersistenceContext private EntityManager em;

spring解决jpa因EntityManager关闭导致的延迟加载异常问题:

<filter> 
  <filter-name>SpringOpenEntityManagerInViewFilter</filter-name> 
  <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> 
</filter> 
  
<filter-mapping> 
  <filter-name>SpringOpenEntityManagerInViewFilter</filter-name> 
	<url-pattern>/*</url-pattern> 
</filter-mapping>

spring2.5+Hibernate3.3+struts2整合开发: 使用到struts2的lib目录下所有不带-plugin结尾的jar文件,但除了struts2-spring-plugin-2.0.11.1.jar

实例化spring同struts1,实例化struts2:

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

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

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表