zoukankan      html  css  js  c++  java
  • spring、Struts2、Hibernate整合

    一、Hibernate框架

    1.导入jar包

    2.hibernate核心配置文件 hibernate.cfg.xml

    4.映射配置 User.hbm.xml

    二、Struts2框架

    1.导入相关jar包 

    2.web.xml配置Struts2

     3.Struts2核心配置文件  Struts.xml

    4.log4j2.xml

    三、Spring框架

    1.导入相关jar包

    2.web.xml  配置监听器

    3.核心配置文件 AppactionContext.xml

    四、SSH三大框架的整合思想

    1. web应用的三层为:

        1.1 web层,(struts2),Struts2框架用的最多的是action

        1.2 service层(spring),spring中用的最多的是IoC和AOP,把对象的创建交给Spring进行管理

        1.3 dao层(hibernate),hibernate则是用来操作数据库,进行CRUD

    2.两两进行整合

      2.1 Struts2和spring进行整合

      将Struts中的action创建交给spring进行处理

      2.2 hibernate和spring进行整合

      两种方式(方式一)将SessionFactory的创建交给Spring进行管理

      

      (方式二)Hibernate的核心文件的数据库信息也交给spring管理

     

      

     注意:

    1.最好在没添加一个框架之后就测试一下是否配置好

    2.当三个框架整合好之后,只剩下AppactionContext.xml 和 log4j2.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:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx
            https://www.springframework.org/schema/tx/spring-tx.xsd">
            
        
        <context:component-scan base-package="com.blb"/>    
    
    	<!-- 数据源 -->
    	<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown">
    		<property name="jdbcUrl"
    			value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" />
    		<property name="username" value="root" />
    		<property name="password" value="root" />
    		<property name="readOnly" value="false" />
    		<property name="connectionTimeout" value="30000" />
    		<property name="idleTimeout" value="600000" />
    		<property name="maxLifetime" value="1800000" />
    		<property name="maximumPoolSize" value="15" />
    	</bean>
    	
    	<bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" lazy-init="true">
            <property name="dataSource" ref="dataSource"/>
            <!-- <property name="configLocations" value="classpath:hibernate.cfg.xml"/> -->
            <property name="hibernateProperties">
    			<props>
    				<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
    				<prop key="show_sql">true</prop>
    			</props>
    		</property>
    		<property name="packagesToScan" value="com.blb.entity"></property>
        </bean>
        
    	<!-- 事务管理器 -->
    	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="mySessionFactory"/>
        </bean>
    	
    	<!-- 开启注解式事务 -->
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>
    

      

     3.测试类

    package com.blb.action;
    
    import javax.transaction.Transactional;
    
    import org.apache.struts2.convention.annotation.Action;
    import org.apache.struts2.convention.annotation.Result;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Controller;
    
    import com.blb.entity.User;
    import com.blb.service.IUserService;
    import com.opensymphony.xwork2.ActionSupport;
    
    @Controller
    @Scope("prototype")
    public class AppAction extends ActionSupport {
    
    	private static final long serialVersionUID = 1L;
    	
    	private User user;
    	
    	public User getUser() {
    		return user;
    	}
    
    	@Autowired
    	private IUserService userSerivce;
    
    	@Action(value = "query", results = {@Result(name = SUCCESS, location = "/index.jsp")})
    	public String query(){
    		user = userSerivce.get("0");
    		return SUCCESS;
    	}
    }
    
  • 相关阅读:
    CLR执行模式之程序集代码的执行
    CLR执行模式之托管代码程序集浅析
    第十章 使用变量的一般事项
    第九章伪代码编程过程 The PseudoCode Programming Process
    第八章防御式编程(代码大全读后)
    第七章实战高质量的子程序(代码大全第七章读后)
    一时看不明白大神的想法
    debain9 debian8 alpine 3.7 aliyun
    elk7
    ansible
  • 原文地址:https://www.cnblogs.com/qun-zi/p/11129162.html
Copyright © 2011-2022 走看看