zoukankan      html  css  js  c++  java
  • Struts2+Spring4+Hibernate4整合超详细教程

    Struts2、Spring4、Hibernate4整合 超详细教程

    Struts2、Spring4、Hibernate4整合实例-下载



    项目目的:
    整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4)搭建项目架构原型。
    项目架构原型:Struts2 + Spring4.0+ Hibernate4.2.4。
    项目特色:同时使用了Struts2、Spring4、Hibernate4、log4j等库或框架,搭建一个最基本的项目原型。
    

    加入 Spring

    • 加入Spring 所需 jar 包

      这里写图片描述

    • 配置 web.xml 文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns="http://java.sun.com/xml/ns/javaee"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     5     id="WebApp_ID" version="2.5">
     6 
     7     <context-param>
     8         <param-name>contextConfigLocation</param-name>
     9         <param-value>classpath:applicationContext*.xml</param-value>
    10     </context-param>
    11 
    12     <listener>
    13         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    14     </listener>
    15 
    16 
    17 </web-app>
    • 加入 Spring 的配置文件[ applicationContext.xml ]
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:aop="http://www.springframework.org/schema/aop"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    11 
    12 </beans>
    applcationContext.xml

    加入Hibernate

    • 加入Hibernate所需jar包

    hibernate所需jar包

    • 加入 hibernate.cfg.xml 文件, 在其中配置 hibernate 的基本属性
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     5 <hibernate-configuration>
     6     <session-factory>
     7         <!-- 配置 hibernate 的基本属性 -->
     8 
     9         <!-- 方言 -->
    10         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    11 
    12         <!-- 是否显示及格式化 SQL -->
    13         <property name="hibernate.show_sql">true</property>
    14         <property name="hibernate.format_sql">true</property>
    15 
    16         <!-- 生成数据表的策略 -->
    17         <property name="hibernate.hbm2ddl.auto">update</property>
    18 
    19         <!-- 二级缓存相关 -->
    20         <!--  ....... -->
    21     </session-factory>
    22 
    23 </hibernate-configuration>
    hibernate.cfg.xml
    • 和 Spring 进行整合

    加入 c3p0 和 MySQL 的驱动

     c3p0的驱动

    MySQL 的驱动

    新建db.properties

    1 jdbc.user=root
    2 jdbc.password=1230
    3 jdbc.driverClass=com.mysql.jdbc.Driver
    4 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
    5 
    6 jdbc.initPoolSize=5
    7 jdbc.maxPoolSize=10
    db.properties

    在 Spring 的配置文件中配置: 数据源, SessionFactory, 声明式事务

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:aop="http://www.springframework.org/schema/aop"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    11 
    12 
    13     <context:annotation-config />
    14     <context:component-scan base-package="com" />
    15 
    16     <!-- 导入资源文件 -->
    17     <context:property-placeholder location="classpath:db.properties"/>
    18 
    19     <!-- 配置 C3P0 数据源 -->
    20     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    21         <property name="user" value="${jdbc.user}"></property>
    22         <property name="password" value="${jdbc.password}"></property>
    23         <property name="driverClass" value="${jdbc.driverClass}"></property>
    24         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    25 
    26         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
    27         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    28     </bean>
    29 
    30     <!-- 配置 SessionFactory -->
    31     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    32         <property name="dataSource" ref="dataSource"></property>
    33         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    34         <property name="mappingLocations" value="classpath:com/entities/*.hbm.xml"></property>
    35     </bean>
    36 
    37     <!-- 配置 Spring 的声明式事务 -->
    38     <!-- 1. 配置 hibernate 的事务管理器 -->
    39     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    40         <property name="sessionFactory" ref="sessionFactory"></property>
    41     </bean>
    42 
    43     <!-- 2. 配置事务属性 -->
    44     <tx:advice id="txAdvice" transaction-manager="transactionManager">
    45         <tx:attributes>
    46             <tx:method name="get*" read-only="true"/>
    47             <tx:method name="lastNameIsValid" read-only="true"/>
    48             <tx:method name="*"/>
    49         </tx:attributes>
    50     </tx:advice>
    51 
    52     <!-- 3. 配置事务切入点, 再把事务属性和事务切入点关联起来 -->
    53     <aop:config>
    54         <aop:pointcut expression="execution(* com.service.*.*(..))" id="txPointcut"/>
    55         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    56     </aop:config>
    57 
    58 </beans>
    applicationContext.xml
    • 小测试

    新建实体类Test.java

     1 package com.entities;
     2 
     3 import javax.persistence.Entity;
     4 import javax.persistence.GeneratedValue;
     5 import javax.persistence.GenerationType;
     6 import javax.persistence.Id;
     7 import javax.persistence.Table;
     8 
     9 @Entity
    10 @Table(name = "test")
    11 public class Test {
    12 
    13     @Id
    14     @GeneratedValue(strategy = GenerationType.IDENTITY)
    15     private long id;//主键
    16     private String name;
    17 
    18 
    19     public long getId() {
    20         return id;
    21     }
    22     public void setId(long id) {
    23         this.id = id;
    24     }
    25     public String getName() {
    26         return name;
    27     }
    28     public void setName(String name) {
    29         this.name = name;
    30     }
    31 
    32 }
    Test.java

    hibernate.cfg.xml 添加

    1    <session-factory>
    2         <!-- 以上 ...-->
    3         <mapping class="com.entities.Test"></mapping>
    4     </session-factory>
    View Code

    如果成功,数据库内自动生成Test表


    加入 Struts2

    • 加入 jar 包: 若有重复的 jar 包, 则需要删除版本较低的.

      这里写图片描述

    • 在 web.xml 文件中配置 Struts2 的 Filter
     1  <!-- 配置 Struts2 的 Filter -->
     2     <filter>
     3         <filter-name>struts2</filter-name>
     4         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
     5     </filter>
     6 
     7     <filter-mapping>
     8         <filter-name>struts2</filter-name>
     9         <url-pattern>/*</url-pattern>
    10     </filter-mapping>
    web.xml
    • 加入 Struts2 的配置文件,新建struts.xml
     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 
     6 <struts>
     7 
     8     <package name="default" namespace="/" extends="struts-default">
     9 
    10             <action name="test">
    11                 <result>index.jsp</result>
    12             </action>   
    13 
    14     </package>
    15 
    16 </struts>
    struts.xml
  • 相关阅读:
    团队项目-个人博客-4.25
    团队项目-个人博客-4.24 学习进度08
    评价使用的输入法
    个人工作总结08
    个人工作总结07
    第八周学习进度条
    个人工作总结06
    构建之法阅读笔记04
    个人工作总结05
    个人工作总结04
  • 原文地址:https://www.cnblogs.com/cymx/p/4692786.html
Copyright © 2011-2022 走看看