zoukankan      html  css  js  c++  java
  • ssh三大框架整合

    Hibernate框架

      1 hibernate核心配置文件

        (0)orm思想

        - 对象关系映射

        (1)数据库信息

        (2)hibernate信息

        (3)映射配置

        (4)hibernate核心配置文件

        - 如果单纯使用hibernate框架,核心配置文件名称 hibernate.cfg.xml和位置 src下面 固定的

        - hibernate和spring整合时候,hibernate核心配置文件名称和位置没有固定要求的

      2 hibernate映射配置文件

        (1)实体类和数据库表映射关系—使用orm思想

      3 hibernate操作的步骤

        (1)在spring框架对hibernate框架进行封装,使用hibernateTemplate

    Struts2框架

      1 Action操作

        (1)action创建三种方式

        - 继承类 ActionSupport

        (2)配置action访问路径

        - 创建struts.xml配置文件,这个文件名称和位置固定 src下面的

        (3)配置访问action的多个方法

        - 使用通配符方式配置

        (4)在action获取表单提交数据

        - 获取request对象

        ** 使用ServletActionContext类获取

        - 属性封装

        - 模型驱动(重点)

        - 表达式封装

        (5)在action操作域对象

        - 使用ServletActionContext获取域对象

        (6)配置struts2的过滤器

      2 值栈

        (1)向值栈放数据

        - set方法

        - push方法

        - 定义变量,生成get方法

        (2)从值栈获取数据

        - 在jsp中使用struts2标签+ognl获取

        - <s:property>

        - <s:iterator>

      3 拦截器

        (1)aop和责任链模式

        (2)自定义拦截器

        - 继承MethodFilterInterceptor

        - 重写类里面的方法

        - 配置拦截器和action关联

    Spring框架

      1 spring核心配置文件

        (1)名称和位置没有固定要求

        (2)在spring核心配置文件中引入schema约束

      2 创建对象

        (1)xml配置方式:<bean  id=””  class=””  scope=””/>

        (2)注解方式:四个注解

      3 注入属性(对象类型属性)

        (1)xml配置方式:

        (2)注解方式:两个注解

        - autowired

        - resource

      3 使用ServletContext对象和监听器实现

        (1)在服务器启动时候,加载spring配置文件,创建对象

        (2)配置spring的监听器

        (3)指定spring配置文件位置

      4 jdbcTemplate

      5 spring事务配置

        (1)xml方式

        (2)注解方式

    SSH框架整合思想

      1 三大框架应用在javaee三层结构

      2 struts2框架和spring整合

        (1)struts2的action在spring配置

      3 spring框架和hibernate框架整合

        (1)hibernate的sessionFactory交给spring配置

        (2)把hibernate数据库配置交给spring配置

    整合struts2和spring框架

      1 把struts2的action交给spring管理

      2 实现过程

        第一步 导入struts2的jar包

        (1)导入用于整合的jar包

          (log4j.properties为log4j的日志输出格式,放入src下面)

        (2)Spring为了整合Struts还需要额外再导入一个jar包:(该包在struts的lib目录下)

            

        (3)导入Struts2的jar包(struts2版本为2.3.24):

            

        (4)创建Action

    public class UserAction extends ActionSupport {
    
        public String execute() throws Exception {
            System.out.println("Success....");
            return NONE;
        }
    }

        (5)创建Strut2的核心配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
        
    <struts>
        <package name="demo" extends="struts-default" namespace="/">
            <action name="userAction" class="com.wm.action.UserAction"></action>
        </package>
    
    </struts>

        (6)在web.xml中配置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>

        (7)导入Spring整合Web项目的jar包,也就是监控项目启动的监听器所在的jar包

          

        (8)创建Spring的核心配置文件并在其中引入约束

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

        (9)把action交给Spring进行配置

      1 <!-- 配置action的对象 -->
      2 <!-- 注意action是多实例的,因此我们这里把scope配置为prototype的 -->
      3 <bean id="userAction" class="com.ssh.domain.UserAction" scope="prototype"></bean>

          ①在spring配置action对象,在struts.xml中也配置action对象

          ②解决:

            只需要在spring里面配置action对象,不要在struts.xml中配置

    <struts>
        <package name="demo1" extends="struts-default" namespace="/">
            <!-- class属性里面不写action全路径了,因为写,action对象创建两次
                写spring配置的action的bean的id值
             -->
            <action name="userAction" class="userAction"></action>
        </package>
    </struts>

           可以这样写的原因是我们导入了一个Spring整合Struts2的jar包:struts2-spring-plugin-2.3.24.jar

        (10)web.xml中Spring监听器的配置

      <listener>
      	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <context-param>
      	<param-name>contextConfigLocation</param-name>
      	<param-value>classpath:bean.xml</param-value>
      </context-param>
    

        (11)测试准备

          ① 所需的为Struts2的核心配置文件:struts.xml

          ②Spring的配置文件:bean.xml

          ③项目的配置文件:web.xml

          ④Struts2的UserAction类

          ⑤在UserAction中对UserService的调用

          ⑥UserService中对UserDao的调用

          ⑦UserDao类的编写

     struts.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        <package name="demo1" extends="struts-default" namespace="/">
            <!-- class属性里面不写action全路径了,因为写,action对象创建两次
                写spring配置的action的bean的id值
             -->
            <action name="userAction" class="userAction"></action>
        </package>
    </struts>

    bean.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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <bean id="userAction" class="com.wm.action.UserAction">
            <property name="userService" ref="userService"></property>
        </bean>
        
        <bean id="userService" class="com.wm.service.UserService">
            <property name="userDao" ref="userDaoImp"></property>
        </bean>
        
        <bean id="userDaoImp" class="com.wm.dao.UserDaoImp"></bean>
    
    </beans>

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>spring-day4-ssh</display-name>
      
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
          <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:bean.xml</param-value>
          </context-param>
      
      <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>
      
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    UserAction.java

    import com.opensymphony.xwork2.ActionSupport;
    import com.wm.service.UserService;
    
    public class UserAction extends ActionSupport {
    
        private UserService userService;    
        public void setUserService(UserService userService) {
            this.userService = userService;
        }
    
        public String execute() throws Exception {
            userService.add();
            return NONE;
        }
    }

    UserService.java

    public class UserService {
    
        private UserDao userDao;
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
        
        public void add() {
            userDao.add();
        }
    }

    UserDao.java 其实现类

    public class UserDaoImp implements UserDao {
        
        public void add() {
            System.out.println("UserDaoImp....");
        }
    }

        (12) 测试结果:

              访问链接:http://localhost:8080/spring_struts2/userAction

              可以在后台观察到如下测试结果:

            image

              至此,Spring和Struts2的整合已经完毕,然后是Spring对Hibernate的整合了。

    Spring框架整合hibernate框架

        1 把hibernate核心配置文件中配置数据库信息,把数据库信息在spring进行配置

        2 把hibernate里面的sessionFactory创建交给spring管理

      具体实现

        第一步 导入hibernate的jar包

        (1)导入struts2和hibernate的jar包时候有jar冲突问题

        在struts2里面有jar包

        

        在hibernate里面有jar包

        

          删除低版本的jar包

        (2)导入spring整合持久化层框架需要导入jar包

      

        第二步 搭建hibernate环境搭建

        1 创建实体类

    public class User {
        
        private Integer uid;
        private String username;
        private String password;
        public Integer getUid() {
            return uid;
        }
        public void setUid(Integer uid) {
            this.uid = uid;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
    }

        2 配置实体类映射关系

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <!-- 1 配置类和表对应 
            class标签
            name属性:实体类全路径
            table属性:数据库表名称
        -->
        <class name="com.wm.entity.User" table="t_user">
            <id name="uid" column="uid">
                <generator class="native"></generator>
            </id>
            <property name="username" column="username"></property>
            <property name="password" column="password"></property>
        </class>
    </hibernate-mapping>

        3 创建核心配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
        <session-factory>
    <!-- 第一部分: 配置数据库信息 必须的 -->
    <!--         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql:///spring_day3</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root</property> -->
            
            <!-- 第二部分: 配置hibernate信息  可选的-->
            <!-- 输出底层sql语句 -->
            <property name="hibernate.show_sql">true</property>
            <!-- 输出底层sql语句格式 -->
            <property name="hibernate.format_sql">true</property>
            <!-- hibernate帮创建表,需要配置之后 
                update: 如果已经有表,更新,如果没有,创建
            -->
            <property name="hibernate.hbm2ddl.auto">update</property>
            <!-- 配置数据库方言
                在mysql里面实现分页 关键字 limit,只能使用mysql里面
                在oracle数据库,实现分页rownum
                让hibernate框架识别不同数据库的自己特有的语句
             -->
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            
            
            <!-- 第三部分: 把映射文件放到核心配置文件中 必须的-->
            <mapping resource="com/wm/entity/User.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>

        第三步 把hibernate核心配置文件数据库配置,在spring进行配置

        (1)把hibernate核心文件中数据库配置去掉了,在spring配置

        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///spring_day3"></property>
            <property name="user" value="root"></property>
            <property name="password" value="root"></property>
        </bean>

        第四步 把hibernate的sessionFactory交给spring配置

        (1)服务器启动时候,加载spring配置文件,把配置文件中对象创建

        (2)把sessionFactory对象创建在spring配置

        (3)因为创建sessionFactory代码不是new出来的,而是多行代码实现的

    // 首先在静态代码块中生成出sessionFactory
    static {
        try {
            configuration.configure(configFile);
            sessionFactory = configuration.buildSessionFactory();
        } catch(Exception e) {
            System.err.println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
        }
    }

        (4)spring里面针对上面情况,封装类,配置类对象可以创建sessionFactory

        <!-- sessionFactory创建交给spring管理 -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <!-- 因为在hibernate核心配置文件中,没有数据库配置,数据库配置在spring里面配置,注入dataSource -->
            <property name="dataSource" ref="dataSource"></property>
            
            <!-- 指定使用hibernate核心配置文件 -->
            <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
        </bean>

        第五步 在dao里面使用hibernateTemplate

    public class UserDaoImp implements UserDao {
        
        private HibernateTemplate hibernateTemplate;    
        public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
            this.hibernateTemplate = hibernateTemplate;
        }
    
        public void add() {
            User user = new User();
            user.setUsername("lili");
            user.setPassword("123");
            hibernateTemplate.save(user);
            System.out.println("UserDaoImp....");
        }
    } 

        bean.xml中配置

        <!-- 创建hibernateTemplate对象 -->
        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
            <!-- 注入sessionFactory -->
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>

        没有配置事务,做操作时候,出现异常

        第六步 配置事务

        <!-- 第一步 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <!--注入sessionFactory-->
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
        <!-- 第二步 开启事务注解 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>

    完整:

    <?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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        <!-- 配置c3p0连接池 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!-- 注入属性值 -->
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///spring_day3"></property>
            <property name="user" value="root"></property>
            <property name="password" value="root"></property>
        </bean>
        
        <!-- sessionFactory创建交给spring管理 -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <!-- 因为在hibernate核心配置文件中,没有数据库配置,数据库配置在spring里面配置,注入dataSource -->
            <property name="dataSource" ref="dataSource"></property>
            
            <!-- 指定使用hibernate核心配置文件 -->
            <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
        </bean>
        
        <!-- 第一步 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <!--注入sessionFactory-->
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
        <!-- 第二步 开启事务注解 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
        
        <!-- 配置action的对象 -->
        <bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype">
            <!-- 注入service -->
            <property name="userService" ref="userService"></property>
        </bean>
        
        <!-- 创建service对象 -->
        <bean id="userService" class="cn.itcast.service.UserService">
            <!-- 注入dao 
                接口 = 实现类对象
            -->
            <property name="userDao" ref="userDaoImpl"></property>
        </bean>
        
        <!-- 创建实现类对象 -->
        <bean id="userDaoImpl" class="cn.itcast.dao.UserDaoImpl">
            <property name="hibernateTemplate" ref="hibernateTemplate"></property>
        </bean>
        
        <!-- 创建hibernateTemplate对象 -->
        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
            <!-- 注入sessionFactory -->
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
    </beans>

    SSH框架整合过程

      第一步 导入jar包

      第二步 搭建struts2环境

        (1)创建action,创建struts.xml配置文件,配置action

        (2)配置struts2的过滤器

      第三步 搭建hibernate环境

        (1)创建实体类

        (2)配置实体类和数据库表映射关系

        (3)创建hibernate核心配置文件

        - 引入映射配置文件

      第四步 搭建spring环境

        (1)创建spring核心配置文件

        (2)让spring配置文件在服务器启动时候加载

        - 配置监听器

        - 指定spring配置文件位置

      第五步 struts2和spring整合

        (1)把action在spring配置(action多实例的)

        (2)在struts.xml中action标签class属性里面写 bean的id值

      第六步 spring和hibernate整合

        (1)把hibernate核心配置文件中数据库配置,在spring里面配置

        (2)把hibernate的sessionFactory在spring配置

      第七步 在dao里面使用hibernateTemplate

        (1)在dao注入hibernateTemplate对象

        (2)在hibernateTemplate对象中注入sessionFactory

      第八步 配置事务

    整合其他方式

      1 spring整合hibernate时候,可以不写hibernate核心配置文件

        (1)把hibernate核心配置文件中,基本信息配置和映射引入都放到spring配置

        <!-- sessionFactory创建交给spring管理 -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <!-- 因为在hibernate核心配置文件中,没有数据库配置,数据库配置在spring里面配置,注入dataSource -->
            <property name="dataSource" ref="dataSource"></property>
            
            <!-- 指定使用hibernate核心配置文件 -->
            <!-- <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> -->
            
            <!-- 配置hibernate基本信息 -->
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                </props>
            </property>
            
            <!-- 配置映射文件引入 -->
            <property name="mappingResources">
                <list>
                    <value>cn/itcast/entity/User.hbm.xml</value>
                    <!-- <value>....</value> -->
                </list>
            </property>

    Spring分模块开发

      1 在spring里面配置多个内容,造成配置混乱,不利用维护

      2 把spring核心配置文件中,一部分配置放到单独的配置文件中,在spring核心配置文件中引入单独配置文件

    <?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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        <!-- 配置c3p0连接池 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!-- 注入属性值 -->
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///spring_day04"></property>
            <property name="user" value="root"></property>
            <property name="password" value="root"></property>
        </bean>
        
        <!-- sessionFactory创建交给spring管理 -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <!-- 因为在hibernate核心配置文件中,没有数据库配置,数据库配置在spring里面配置,注入dataSource -->
            <property name="dataSource" ref="dataSource"></property>
            
            <!-- 指定使用hibernate核心配置文件 -->
            <!-- <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> -->
            
            <!-- 配置hibernate基本信息 -->
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                </props>
            </property>
            
            <!-- 配置映射文件引入 -->
            <property name="mappingResources">
                <list>
                    <value>cn/itcast/entity/User.hbm.xml</value>
                    <!-- <value>....</value> -->
                </list>
            </property>
            
        </bean>
        
        <!-- 第一步 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <!--注入sessionFactory-->
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
        <!-- 第二步 开启事务注解 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
        
    
        <!-- 引入其他spring配置文件 -->
        <import resource="classpath:user.xml"/>
        
    </beans>

    user.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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        <!-- 配置action的对象 -->
        <bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype">
            <!-- 注入service -->
            <property name="userService" ref="userService"></property>
        </bean>
        
        <!-- 创建service对象 -->
        <bean id="userService" class="cn.itcast.service.UserService">
            <!-- 注入dao 
                接口 = 实现类对象
            -->
            <property name="userDao" ref="userDaoImpl"></property>
        </bean>
        
        <!-- 创建实现类对象 -->
        <bean id="userDaoImpl" class="cn.itcast.dao.UserDaoImpl">
            <property name="hibernateTemplate" ref="hibernateTemplate"></property>
        </bean>
        
        <!-- 创建hibernateTemplate对象 -->
        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
            <!-- 注入sessionFactory -->
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
        
    </beans>
  • 相关阅读:
    C# WM_NCMOUSELEAVE 消息触发
    C#常用集合的使用(转载)
    关于直播,所有的技术细节都在这里了(转载)
    C# Winform 窗体美化
    正则表达式符号全解析
    C#中List<T>转DataTable
    C#中的Queue集合
    C#中Stack集合
    智能信息处理
    Mysql
  • 原文地址:https://www.cnblogs.com/kpsmile/p/10151913.html
Copyright © 2011-2022 走看看