Struts2+Spring4+Hibernate5框架整合及XML配置文件

2 篇文章 0 订阅
2 篇文章 0 订阅

怎么说呢,前段时间学习了SSH2框架 感觉新手有一定的基础来学习框架的话是比较轻松的, 框架整合了复杂的语句,简单而方便, 有了很好的耦合性 需求改动也比较容易,, 简单总结下SSH2框架.


  1. 首先 : 我们得了解什么是框架 , 框架有什么好处 .

    什么是框架:框架是典型的J2EE三层结构, 分为分为表现层, 业务逻辑层 数据服务层 三层架构体系将, 业务逻辑 数据访问及校验等工作放在中间(业务逻辑层处理)  。客户端不直接与数据库交互,而是通过组件与中间层建立连接,再由中间层与数据库交互。MVC三层架构体系!!!
    
    框架的好处: 开发效率提升 需求变更修改代码方便
    
  2. 接下来我们就开始进入SSH2项目的搭建工作:
    (1)struts2+spring+hibernate需要的jar包如下:
    这里写图片描述

    (2)xml文件配置 : web.xml – struts.xml – application.xml
    1..web.xml配置如下:

    struts2在web.xml的配置

    <!-- Struts2 核心控制器 -->
    <filter>
        <!-- 过滤器名 -->
        <filter-name>struts2</filter-name>
        <!-- StrutsPrepareAndExecuteFilter核心控制器的实现类 -->
        <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>

Spring在web.xml的 配置:

    <!-- spring实例化工厂 -->
    <filter>
        <filter-name>SpringOpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>sessionFactoryBean</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SpringOpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- 配置application.xml路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- spring监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

2.. struts2.xml配置

    <package name="default" namespace="/" extends="struts-default , json-default">
        <action name="a" class="computerAction">
            <result name="index">index.jsp</result>
        </action>
    </package>

3..application.xml配置

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 注册连接JDBC属性 -->
    <context:property-placeholder location="classpath:properties.properties"/>
    <!-- 注册数据源  -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClassName}"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="user" value="${username1}"></property>
        <property name="password" value="${password}"></property>
    </bean>
    <!-- 注册SessionFactory -->
    <bean id="sessionFactoryBean" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>

        <property name="mappingDirectoryLocations" value="classpath:com/zy/cn/beans"></property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <!-- 为保证上下文在同一线程    -->
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
            </props>
        </property>
    </bean> 

    <!-- 注册事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactoryBean"></property>
    </bean>


    <!--   在dao层并没有执行事务 所以要加这个配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/> 



    <!-- 事务的通知 -属性 - 具体执行方法-->
    <tx:advice id="tdAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 事务的具体执行方法 -->
            <tx:method name="*" read-only="false" />
        </tx:attributes>
    </tx:advice>
    <!-- 切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.zy.cn.*.*.*(..))" id="myPointcut"/>
        <aop:advisor advice-ref="tdAdvice" pointcut-ref="myPointcut"/>
    </aop:config>


    <!-- 手动装配  -->
    <bean id="computerDao" class="com.zy.cn.dao.impl.ComputerDaoImpl">
        <property name="sessionFactory" ref="sessionFactoryBean"></property>
    </bean>
    <bean id="computerService" class="com.zy.cn.service.impl.ComputerServiceImpl">
        <property name="computerDao" ref="computerDao"></property>
    </bean>
    <bean id="computerAction" class="com.zy.cn.action.ComputerAction"> 
        <property name="service" ref="computerService"></property>
    </bean>
    <bean id="computerAddParts" class="com.zy.cn.action.ComputerAddParts"> 
        <property name="service" ref="computerService"></property>
    </bean>
</beans>

差不多到这里xml配置都差不多了 是不是发现 为什么没有hibernate配置文件呢?? 其实也有的,我的hibernate的cfg,xml已经在spring的application.xml配置了 ,,然后还有一个就是实例化bean类 如图:
这里写图片描述

好了 到现在该写代码啦 也就是hibernate查询数据库操作交给spring来管理然后返回给service层 进行业务逻辑操作 service层通过action层给struts2传值给jsp等页面显示

具体代码操作可以根据需求进行操作 在这里就不一 一 显示了
其实框架这个东西也就是方便了程序员的开发效率 和 根据不同的需求进行改动 耦合性

struts2有拦截器 spring有aop切面 hibernate有hql语句 哈哈哈


======总结一下SSH2具体整合步骤======
1.创建web项目, 在lib包 导jar包
2.创建struts.xml 和application.xml文件
3.搭建web.xml.struts.xml 和application.xml核心配置文件
4.创建bean类和hbm.xml
5.dao层进行数据库操作
6.service进行业务逻辑操作
7.action层进行传值给jsp
8.jsp显示
9.完成.

<注意>:
命名规范和意义
核心配置文件一个不能少
总体逻辑和创建数据库思路要清晰
可以逆向写 但是必须遵守java代码操作

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值