spring 整合hibernate

Spring整合hibernate
基本原理:就是由spring来管理hibernate的SessionFactory
方式一:零障碍整合(了解)
spring中提供的一个 LocalSessionFacotry 加载Hibernate的配置文件  (需要自己定义一个hibernate.cfg.xml)


<bean id="sessionFactoy" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

我们必须配置spring的ContextLoaderListener


<bean id="sessionFactoy" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>


方式二   spring 管理 hibernate 配置
不在需要hibernate.cfg.xml文件,所有关于hibernate.cfg.xml文件中的配置都在 spring的配置文件中来配置。

需要一个db.properties 文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///sshtest
jdbc.username=root
jdbc.password=123

spring 中的配置

<!-- 引入properties 文件 -->
<context:property-placeholder location="classpath:db.properties"/>

<!-- 不需要hibernate.cfg.xml 的hibernate 的配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     <property name="driverClass" value="${jdbc.driverClass}"></property>
     <property name="jdbcurl" value="${jdbc.url}"></property>
     <property name="user" value="${jdbc.username}"></property>
     <property name="password" value="${jdbc.password}"></property>
</bean>



然后创建 LocalSessionFacotry 加载连接池

      <!-- 加载 hibernate 配置文件 -->
   
  <bean id="sessionFactoy"
          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
          
           <property name="dataSource" ref="dataSource">
                <props>
                <prop key="hiernate.show_sql">true</prop>
                
                <prop key="hiernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hiernate.hbm2ddl.auto">update</prop>
                <prop key="hiernate.format_sql">true</prop>
                </props>
           </property>
     </bean>
public class UserDaoImpl extends HibernateDaoSupport implements IUserDao{
     @Override
     public void add(User user) {
           // TODO Auto-generated method stub
           this.getHibernateTemplate().save(user);
     }
}

xml中声明bean 引入sessionFactoy
     <!-- 声明dao -->
     <bean id="userDao" class="com.ithiema.dao.UserDaoImpl">
     <!-- 只需要注入sessionFactoyBean 就可以获取hibernate  sessionFactoyBean是对hibernate 的图个封装 -->
           <property name="sessionFactoyBean" ref="sessionFactoyBean"></property>
     
     </bean>





spring 整合hibernate 后的dao

要找Dao 上继承 HibernateDaoSupport  就可以获得jdbcTemplate



spring 中配置事物管理器
      <!-- 事物管理 -->
     
     <!-- 配置事物管理器 -->
     <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
     
     <!-- 引入dataSource  这里我们用的是dataSource 的封装 sessionFactory  -->
     <property name="sessionFactoryBean" ref="sessionFactoyBean"></property>
     </bean>
     <!-- 通知 -->
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
     <tx:attributes>
     
     <tx:method name="add"/>
     <tx:method name="update"/>
     <tx:method name="del"/>
     </tx:attributes>
     
     </tx:advice>
     
     <!-- 切面 -->
     
     <aop:config>
<!-- 这里只写了add()方法  如果是想匹配所有的  * *..*(..)-->
     <aop:pointcut expression="execution(* *.add(..))" id="mypointcut"/>
     <aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>
     </aop:config>


Spring整合struts2框架

(1)spring 管理action.
1.写一个addUser.jsp
< form action= "${pageContext.request.contextPath} /user_add" method= "post" >
     name < input type= "text" name= "username" >
     age < input type= "text" name= "age" >
< input type= "submit" value= "addUser" >
</ form >




2.在applicationContext.xml 中配置  action

<!-- spring 中整合struts -->
     <bean id="userAction" class="com.ithiema.action.UserAction">
     <property name="userService" ref="userService"></property>
     </bean>



3.action 类中要提供 目标对象的set方法

public class UserAction extends ActionSupport implements  ModelDriven<User>{
      private UserService userService;
      public void setUserService(UserService userService) {
            this. userService = userService;
     }

3.在struts 中配置
     < struts >
      < package name= "default" namespace= "/" extends= "struts-default" >
      <!--  class 就是spring 配置的 stuts bean的id -->
      < action name= "user_*" class= "userAction" method= "{1}" >
      < result name= "success" >/success.jsp </ result >
      </ action >
      </ package >
     
      </ struts >

必须在web.xml 中配置struts2 框架的filter
  <!-- spring 整合 strtus -->
  < 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 >

spring整合 struts2 框架方式二 (action 中自动注入 service)
1.Struts.xml 中的Class需要全类名
  < struts >
      < package name = "default" namespace = "/" extends = "struts-default" >
      <!--  class 就是spring 配置的 stuts bean的id -->
      < action name = "user_*" class = "com.itheima.action.UserAction" method = "{1}" >
      < result name = "success" > /success.jsp </ result >
      </ action >
      </ package >
     
      </ struts >
这时 就会将action 中需要的注入到service中

public class UserAction extends ActionSupport implements  ModelDriven<User>{
      private UserService userService;
      public void setUserService(UserService userService) {
            this. userService = userService;
     }




在配置文件中配置  自动注入

struts.objectFactory.spring.autoWire= name  //表示根据name 自动注入


也可以在struts.xml 中修改注入的方式

< constant name = "struts.objectFactory.spring.autoWire" value = "type" ></ constant >  <!-- 表示根据type 自动注入-->


总结:
如果struts中的class写的是全类名  则是根据自动注入 自动注入的话就要在 db.properties配置文件中 配置注入的方式
如果不是全类名而是  applicationContext.xml 中配置的action 的bean的 id那么就要在applicationContext.xml中配置action 的bean 

记住区别 非自动注入就是在 applicationContext.xml 中配置了bean 而且class非全类名
            自动注入就是在    db.properties配置文件  中配置了autoWire class 为全类名
*action 中的目标对象要有set方法





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值