spring-struts2-hibernate整合

1、导入spring

1、加入springjar包(版本自选)

2、配置web、xml

  <!-- needed for ContextLoaderListener -->
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <!--配置上下文加载监听器-->
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

3、加入sping的配置文件

1)、因整合配置文件较多,故创建conf的包,来放置所有配置文件,eclipse的这个包应创建在src下面

2)、配置spring基础配置,我先配置了c3p0数据源(先自己导入jar包),然后配置要扫描的包,便于注解操作

1、书写数据库连接信息,抽出在properties文件中,减少耦合

  jdbc.user=root
  jdbc.password=xuex
  jdbc.driverClass=com.mysql.jdbc.Driver
  jdbc.jdbcUrl=jdbc:mysql:///ssh-crud
  
  #池 最小连接数
  jdbc.initPoolSize=5
  #池 最大连接数
  jdbc.maxPoolSize=10
  #...
2、引入外部资源文件,数据源,以及要扫描的包
      <!--导入外部资源文件-->
      <context:property-placeholder location="classpath:db.properties"/>
      <!--开启注解-->
      <context:annotation-config></context:annotation-config>
      <!--配置要扫描的包-->
      <context:component-scan base-package="com.xue.*"></context:component-scan>
  
      <!--配置c3p0数据源,使用外部db.properties-->
      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
          <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
          <property name="driverClass" value="${jdbc.driverClass}"></property>
          <property name="user" value="${jdbc.user}"></property>
          <property name="password" value="${jdbc.password}"></property>
      </bean>

2、加入hibernate

①. 加入 jar 包

②. 在conf包加入 hibernate.cfg.xml 文件, 在其中配置 hibernate 的基本属性

  
          <!--数据库方言-->
          <property name="hibernate.dialect">org.hibernate.dialect.Dialect</property>
          <!--显示sql语句-->
          <property name="hibernate.show_sql">true</property>
          <!--格式化sql、-->
          <property name="hibernate.format_sql">true</property>
          <!--生成表的策略-->
          <property name="hibernate.hbm2ddl.auto">update</property>

③. 建立持久化类, 和其对应的 .hbm.xml 文件(注解即可)

  
  @Entity
  public class Employee {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;
      private String empName;
      private Integer age;
  
      @ManyToOne(targetEntity=Dept.class,cascade=CascadeType.ALL)
      @JoinColumn(name="emp_id")
      private Dept dept;

别忘在在hibernate.hfg.xml中引用这个持久化类

  
      <mapping class="com.xue.domain.Dept"/>
      <mapping class="com.xue.domain.Employee"/>

④. 和 Spring 进行整合

  
  <!-- 配置sessionFactory -->
      <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
          <property name="dataSource" ref="dataSource"></property>
          <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
      </bean>
  <!-- 配置 Spring 的声明式事务 -->
      <!-- 1. 配置 hibernate 的事务管理器 -->
      <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory"></property>
      </bean>
  
      <!-- 2. 配置事务属性 -->
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
          <tx:attributes>
              <tx:method name="get*" read-only="true"/>
              <tx:method name="lastNameIsValid" read-only="true"/>
              <tx:method name="*"/>
          </tx:attributes>
      </tx:advice>
  
      <!-- 3. 配置事务切入点, 再把事务属性和事务切入点关联起来 -->
      <aop:config>
          <aop:pointcut expression="execution(* com.xue.service.*.*(..))" id="txPointcut"/>
          <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
      </aop:config>

5、此时运行即可以自动创建表

3、加入Struts2

1)、jar包(我这里就不截图了,麻烦)

2)、在 web.xml 文件中配置 Struts2 的 Filter

  
  <!--配置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>

3)、 加入 Struts2 的配置文件

4)、整合 Spring

①、加入整合jar包

在Struts2的lib下面找到 struts2-spring-plugin-2.3.15.3.jar名字的jar导入项目

②. 在 Spring 的配置文件中正常配置 Action, 注意 Action 的 scope 为 prototype

为了方便,我又重新写了一个Spring的配置文件用来配置bean,更改了web.xml导入配置文件的方式,使用通配符导入

  
  <!-- needed for ContextLoaderListener --> 回到标题一,第二个小标题可见
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext*.xml</param-value>
      </context-param>

目录结构:

在applicationContext-bean.xml中配置依赖关系

  
  <bean id="employeeDao" class="com.xue.dao.EmployeeDao">
          <property name="sessionFactory" ref="sessionFactory"/>
      </bean>
  
      <bean id="employeeService" class="com.xue.service.EmployeeService">
          <property name="employeeDao" ref="employeeDao"></property>
      </bean>
  
  
      <bean id="employeeAction" class="com.xue.action.EmployeeAction"
            scope="prototype">
          <property name="employeeService" ref="employeeService"></property>
  </bean>
③. 在 Struts2 的配置文件中配置 Action 时, class 属性指向该 Action 在 IOC 中的 id

完结

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SpringCloud1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值