spring框架开发笔记 lesson5 三大框架整合

三大框架(SSH)整合

    Struts2     2.3      表现层(视图view  控制器 StrutsPreparedAndExceuteFilter)
   Spring      3.1       管理bean对象的(整合其他的框架) 
   Hibernate   3.3    数据持久层(数据访问层)

为什么整合hibernate
    • dao或者mapper对象通过 spring容器统一创建和管理,防止内存里有各种对象导致堆栈溢出
    • 第三方数据源连接池(Pool)    c3p0(****) dbcp , 通过连接池获取连接  更快 可以反复利用
    • .采用aop思想 对事务管理 
    • 使用HibernateTemplate  操作hibernate的模板类,操作数据库更简洁
为什么整合struts2:
    • action对象交给spring容器创建  多例的
    •  通过一个监听器创建ApplicationContext对象
    • 监听到tomcat启动了 就会创建ApplicationContext对象


整合三大框架
action

   private IDeptService service;

   @Override

public String execute() throws Exception {

List<Dept> list = service.findAllDepts();

request.put("list", list);

return "success";

}

dao

private HibernateTemplate template;//封装了操作的基本代码
 
public void setTemplate(HibernateTemplate template) {
this.template = template;
}

public List<Dept> findAll() {
List<Dept> list = template.find("from Dept");
return list;
}

service

  public List<Dept> findAllDepts() {

List<Dept> list = dao.findAll();

return list;

}

config

hibernate.cfg.xml

<hibernate-configuration>

   <!-- 因为spring整合hibernate  
       通过c3p0连接池提供连接
       所在在这里不需要配置数据库的url driver username password等获取连接了
     -->

       通过c3p0连接池提供连接

       所在在这里不需要配置数据库的url driver username password等获取连接了

     -->

<session-factory><!-- 设置数据库的方言 -->

<property name="hibernate.dialect">

org.hibernate.dialect.Oracle10gDialect

</property>

<!-- 显示sql -->

<property name="hibernate.show_sql">true</property>

<property name="hibernate.format_sql">true</property>

<!-- 引入映射文件 -->

<mapping resource="com/ssh/mapping/Dept.hbm.xml" />

</session-factory>

</hibernate-configuration>

struts

struts_dept.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="ssh" extends="struts-default">

   <!-- 
      注意:
      class:不能写 全类名 
               要写spring配置文件中 bean的id值

    -->

<action name="list" class="deptListAction">

  <result name="success">/jsp/list.jsp</result>

</action>

</package>

</struts>  

struts.xml

<struts>

    <include file="struts/struts_dept.xml"></include>

</struts> 

spring

application_dept.xml

<bean id="deptDao" class="com.ssh.dao.DeptDaoHibernateImpl">

<property name="template"  ref="hibernateTemplate"></property>

</bean>

<bean id="deptService" class="com.ssh.service.DeptServiceImpl">

<property name="dao" ref="deptDao"></property>

</bean>

<!-- 配置事务  -->
  <!-- 1.配置事务管理切面对象 
  -->

 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

  <property name="sessionFactory" ref="sessionFactory"></property>

 </bean>

 <!-- 配置通知 
  事务管理默认底层是环绕通知
  id:通知的id
  transaction-manager: 事务管理切面对象  
 
  -->

 <tx:advice id="txAdvice"  transaction-manager="txManager" >

  <!-- 什么方法 采用 什么事务传播行为(事务机制)
  name: 方法名
  propagation:  事务机制
                     REQUIRED *: 必须要有事务   如果当前有事务就用当前事务 如果当前没有事务 新创建一个事务
    NOT_SUPPORTED*:   不需要事务
    NEVER:   不要事务  如果当前有事务  报错
    SUPPORTS:如果当前有事务就参与事务   没有也就算了
    REQUIRES_NEW:每次都新建事务

  -->

 

  <tx:attributes>

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

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

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

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

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

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

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

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

   <tx:method name="select*" propagation="NOT_SUPPORTED"/>

   <tx:method name="find*" propagation="NOT_SUPPORTED"/>

   <tx:method name="get*" propagation="NOT_SUPPORTED"/>

   <tx:method name="set*" propagation="NOT_SUPPORTED"/>

   <!-- 除了上面的方法 以外的其他方法  默认就是: REQUIRED   -->

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

  </tx:attributes>

 </tx:advice>

 <aop:config>

   <!-- 配置切入点 -->

  <aop:pointcut expression="execution(public *  com.zrkc.ssh.service.*.*(..))" id="myPointCut"/>

   <!-- 配置切入点 与 通知的结合 -->

   <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut"/>

 </aop:config>

<!-- action对象 给容器创建
 方便给action类中的属性 注入值
-->

<bean id="deptListAction" scope="prototype" class="com.ssh.action.ListAction">

 <property name="service"  ref="deptService"></property>

</bean>

application_datasource.xml

<!-- 将ssh.properties加载到内存   -->

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="location">

<value>classpath:ssh.properties</value>

</property>

</bean>

<!-- 配置c3p0数据源连接池  通过Datasource 只能获取Connection

 从c3p0.jar查找

-->

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="jdbcUrl">

<value>${url}</value>

</property>

<property name="driverClass">

<value>${driver}</value>

</property>

<property name="user">

<value>${user}</value>

</property>

<property name="password">

<value>${pwd}</value>

</property>

<!--连接池的最大连接数 -->

<property name="maxPoolSize" value="${maxSize}"/>

<!--连接池的最小连接数 -->

<property name="minPoolSize" value="1"/>

<!--初始化连接数 -->

<property name="initialPoolSize" value="1"/>

<!--连接的最大空闲时间,超时的连接将被丢弃,单位:秒 -->

<property name="maxIdleTime" value="60"/>

<!--没有连接可用时,等待连接的时间,单位:毫秒 -->

<property name="checkoutTimeout" value="2000"/>

</bean>

<!-- 创建sessionFactory对象

    从spring-orm.jar查找

-->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!-- 获取hibernate.cfg.xml位置 -->

<property name="configLocation">

<value>classpath:hibernate/hibernate.cfg.xml</value>

</property>

 <!-- 给dataSource属性注入值  -->

<property name="dataSource" ref="myDataSource"></property>

</bean>

<!-- 创建hibernateTemplate模板对象 -->

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>


applicationContext.xml

 <!-- 引入 子配置文件 -->

<import resource="application_*.xml"/>


properties 将数据源中url等 配置在一个*.properties文件中

url=jdbc:oracle:thin:@localhost:1521:XE

driver=oracle.jdbc.driver.OracleDriver

user=system

pwd=tiger

maxSize=40


web.xml

 <!-- 为spring容器对象  指定 applicationContext.xml 配置文件的位置-->

    <context-param>

    <param-name>contextConfigLocation</param-name>

<param-value>classpath:spring/applicationContext.xml</param-value>    

    </context-param>

  <!-- 配置监听器  

    监听到tomcat启动了  通过这个监听器就会将spring容器对象创建起来

   -->

   <listener>

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

   </listener>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值