Struts2+Spring+Hibernate3集成

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hxdeng/article/details/52278238 加入Str...
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hxdeng/article/details/52278238

1. 加入Struts2,Hibernate3.3,Spring3.3 和数据库驱动包

注意:当有包重复的时候选择高版本即可

2. 添加log4j文件和连接数据库的配置文件

log4j.rootLogger=DEBUG,File,Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]%m%n

log4j.appender.File=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.File.File=C:/log.log
log4j.appender.File.MaxFileSize=100MB

log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug 
log4j.logger.java.sql.Statement=debug 
log4j.logger.java.sql.PreparedStatement=debug 

#\u5236\u5B9A\u6D88\u606F\u8F93\u51FA\u7684\u6700\u4F4E\u5C42\u6B21
#指定消息输出的最低层次
log4j.appender.File.Threshold=debug

#包含日志产生的时间、线程、类别等等信息
log4j.appender.File.layout=org.apache.log4j.TTCCLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

3. 添加Struts2的核心配置文件(struts.xml)和Spring的核心配置文件(applactionContext.xml)

4. 修改web.xml文件

4.1 定Spring配置文件的位置

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

4.2 添加Spring的监听器

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

4.3 添加Struts2的拦截器

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

5. Spring的核心配置文件中配置数据

5.1 在Spring的核心配置文件中引入头文件

<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-3.0.xsd   
                            http://www.springframework.org/schema/aop   
                            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
                            http://www.springframework.org/schema/tx   
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
                            http://www.springframework.org/schema/context   
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

5.2 引入数据库配置文件的位置

<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:jdbc.properties" />

5.3 配置连接数据库的数据源

5.3.1 DruidDataSource

<!-- 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${jdbc_url}" />
    <property name="username" value="${jdbc_username}" />
    <property name="password" value="${jdbc_password}" />
</bean>

5.3.2 dbcp

<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${driverClassName}" />
    <property name="url" value="${jurl}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
</bean>

5.3.3 c3p0

<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${driverClassName}" />
    <property name="jdbcUrl" value="${jdbcUrl}" />
    <property name="user" value="${user}" />
    <property name="password" value="${password}" />
    <!-- 队列中的最小连接数 -->
    <property name="minPoolSize" value="15"></property>
    <!-- 队列中的最大连接数 -->  
    <property name="maxPoolSize" value="25"></property>
    <!-- 当连接耗尽时创建的连接数 -->
    <property name="acquireIncrement" value="15"></property>
    <!-- 等待时间 -->
    <property name="checkoutTimeout" value="10000"></property>
    <!-- 初始化连接数 -->
    <property name="initialPoolSize" value="20"></property>
    <!-- 最大空闲时间,超出时间连接将被丢弃 -->
    <property name="maxIdleTime" value="20"></property>
    <!-- 每隔60秒检测空闲连接 -->
    <property name="idleConnectionTestPeriod" value="60000"></property>
</bean>

6. 配置SessionFactory

<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQL5Dialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
        <!-- mappingLocations[可以指定任何文件路径;并且可以指定前缀,也可以用通配符] -->
        <property name="mappingLocations">
            <list>
                <value>classpath:com/hy/ssh/pojo/*.hbm.xml</value>
            </list>
        </property>

        <!-- mappingResources[指定classpath下具体映射文件名]  
        <property name="mappingResources">
            <list>
                <value>com/hy/ssh/pojo/Product.hbm.xml</value>
            </list>
        </property> 
        -->

        <!-- mappingDirectoryLocations[指定映射的文件路径]
        <property name="mappingLocations">
            <list>
                <value>classpath:com/hy/ssh/pojo/</value>
            </list>
        </property>
         -->

    <!-- mappingJarLocations[指定加载的映射文件在jar文件中]  --> 
</bean>

6. 配置事务

<!-- 配置事务 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

7. 事务的通知方式

<!-- 事务的通知方式 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />
        <tx:method name="search*" propagation="REQUIRED" read-only="true" />
        <tx:method name="query*" propagation="REQUIRED" read-only="true" />

        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="submit*" propagation="REQUIRED" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="insert*" propagation="REQUIRED" />

        <tx:method name="del*" propagation="REQUIRED" />
        <tx:method name="remove*" propagation="REQUIRED" />

        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="modify*" propagation="REQUIRED" />

        <tx:method name="*" propagation="REQUIRED" read-only="true" />
    </tx:attributes>
</tx:advice>

说明:Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播

事务传播行为类型 说明
PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY 使用当前的事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER 以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。

8. 切面

<!-- AOP切面拦截事务 -->
<aop:config>
    <aop:pointcut id="serviceMethod" expression="execution(* com.hy.ssh.services.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>

9. 引入Spring其他文件[bean…..]

<import resource="xxx.xxx.xxx.xxx.xml" />

10. 编写各类bean

注意:Action的bean需要修改作用域,默认是单利模式,需要修改为每次请求进来重新创建Bean对象  scope="prototype"

11. 完善web.xml文件

11.1 添加编码格式过滤器

<filter>
    <description>字符集过滤器</description>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <description>字符集编码</description>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

11.2 添加系统session过期时间

<!-- 配置session超时时间,单位分钟 -->
<session-config>
    <session-timeout>15</session-timeout>
</session-config>

11.3 配置内存监听事件

<!-- 防止spring内存溢出监听器 -->
<listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

11. 4 添加OpenSessionInView过滤器

<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>
相关文章
|
5天前
|
安全 Java 数据库
第16课:Spring Boot中集成 Shiro
第16课:Spring Boot中集成 Shiro
|
5天前
|
消息中间件 存储 Java
第15课: Spring Boot中集成ActiveMQ
第15课: Spring Boot中集成ActiveMQ
|
28天前
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
|
4月前
|
安全 Java Apache
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 身份和权限认证
本文介绍了 Apache Shiro 的身份认证与权限认证机制。在身份认证部分,分析了 Shiro 的认证流程,包括应用程序调用 `Subject.login(token)` 方法、SecurityManager 接管认证以及通过 Realm 进行具体的安全验证。权限认证部分阐述了权限(permission)、角色(role)和用户(user)三者的关系,其中用户可拥有多个角色,角色则对应不同的权限组合,例如普通用户仅能查看或添加信息,而管理员可执行所有操作。
160 0
|
4月前
|
安全 Java 数据安全/隐私保护
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 三大核心组件
本课程介绍如何在Spring Boot中集成Shiro框架,主要讲解Shiro的认证与授权功能。Shiro是一个简单易用的Java安全框架,用于认证、授权、加密和会话管理等。其核心组件包括Subject(认证主体)、SecurityManager(安全管理员)和Realm(域)。Subject负责身份认证,包含Principals(身份)和Credentials(凭证);SecurityManager是架构核心,协调内部组件运作;Realm则是连接Shiro与应用数据的桥梁,用于访问用户账户及权限信息。通过学习,您将掌握Shiro的基本原理及其在项目中的应用。
160 0
|
4月前
|
NoSQL Java 关系型数据库
微服务——SpringBoot使用归纳——Spring Boot 中集成Redis——Redis 介绍
本文介绍在 Spring Boot 中集成 Redis 的方法。Redis 是一种支持多种数据结构的非关系型数据库(NoSQL),具备高并发、高性能和灵活扩展的特点,适用于缓存、实时数据分析等场景。其数据以键值对形式存储,支持字符串、哈希、列表、集合等类型。通过将 Redis 与 Mysql 集群结合使用,可实现数据同步,提升系统稳定性。例如,在网站架构中优先从 Redis 获取数据,故障时回退至 Mysql,确保服务不中断。
155 0
微服务——SpringBoot使用归纳——Spring Boot 中集成Redis——Redis 介绍
|
5天前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
|
10天前
|
Java 数据库连接 API
Java 8 + 特性及 Spring Boot 与 Hibernate 等最新技术的实操内容详解
本内容涵盖Java 8+核心语法、Spring Boot与Hibernate实操,按考试考点分类整理,含技术详解与代码示例,助力掌握最新Java技术与应用。
27 2
|
4月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
128 0
|
1月前
|
监控 安全 Java
Java 开发中基于 Spring Boot 3.2 框架集成 MQTT 5.0 协议实现消息推送与订阅功能的技术方案解析
本文介绍基于Spring Boot 3.2集成MQTT 5.0的消息推送与订阅技术方案,涵盖核心技术栈选型(Spring Boot、Eclipse Paho、HiveMQ)、项目搭建与配置、消息发布与订阅服务实现,以及在智能家居控制系统中的应用实例。同时,详细探讨了安全增强(TLS/SSL)、性能优化(异步处理与背压控制)、测试监控及生产环境部署方案,为构建高可用、高性能的消息通信系统提供全面指导。附资源下载链接:[https://pan.quark.cn/s/14fcf913bae6](https://pan.quark.cn/s/14fcf913bae6)。
199 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问