Struts+Spring+Hibernate整合入门详解

https://www.cnblogs.com/cailijia52o/p/8724710.html

一、基本概念

      Struts:作为基于 MVC 模式的 Web 应用最经典框架,两个项目Struts 和webwork已经集成,成为现在的Struts2。目前的最新版本是2.0.9(2007-7)。

      Spring:是一个轻型的容器,利用它可以使用一个外部 XML 配置文件方便地将对象连接在一起。每个对象都可以通过显示一个 JavaBean 属性收到一个到依赖对象的引用,留给您的简单任务就只是在一个XML 配置文件中把它们连接好。

      Hibernate 是一个纯 Java 的对象关系映射和持久性框架,它允许您用 XML 配置文件把普通 Java 对象映射到关系数据库表。使用 Hibernate 能够节约大量项目开发时间,因为整个 JDBC 层都由这个框架管理。这意味着您的应用程序的数据访问层位于 Hibernate 之上,完全是从底层数据模型中抽象出来的。

      三种技术到目前已经比较成熟,而且他们都是免费的!让我们对三者集成进行一个初览(简单而不专业):

      我们用Struts实现从Web(网页,MVC中的View)到后台系统的映射(WebàAction),然后由Spring管理这些Action,把它们作为Bean和其他对象一起处理。这些Bean之间处理业务逻辑、数据、系统状态等,且它们被Spring统一管理,为了区分,就算大概包括MVC的MC部分吧。然后需要持久化的数据由Spring和Hibernate之间的接口交由Hibernate处理(这个属于持久层)。

      必须基础:只要Java基础,一点HTML知识、XML基础就可以了。本文的目的就是从零开始建立第一个Struts+Spring+Hibernate应用。即使它是最简单的,我们也希望初学者能够从中理解一些思想,其中也包括系统架构的设计思想。

配置 pom.xml 依赖

<properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <spring.version>4.1.4.RELEASEspring.version>
        <hibernate.version>4.3.8.Finalhibernate.version>
        <struts2.version>2.3.20struts2.version>
        <jackson.version>2.5.0jackson.version>
    properties>
 
    <dependencies>
 
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
 
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>
 
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>
 
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
 
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring.version}version>
        dependency>
 
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>
 
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring.version}version>
            <scope>providedscope>
        dependency>
 
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>${spring.version}version>
        dependency>
 
        
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-coreartifactId>
            <version>${hibernate.version}version>
        dependency>
 
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-ehcacheartifactId>
            <version>${hibernate.version}version>
        dependency>
 
        
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-coreartifactId>
            <version>${struts2.version}version>
 
            <exclusions>
                <exclusion>
                    
                    <artifactId>javassistartifactId>
                    <groupId>javassistgroupId>
                exclusion>
            exclusions>
        dependency>
 
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-spring-pluginartifactId>
            <version>${struts2.version}version>
        dependency>
 
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-convention-pluginartifactId>
            <version>${struts2.version}version>
        dependency>
 
        
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
 
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.34version>
        dependency>
 
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.12version>
        dependency>
 
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.3version>
        dependency>
 
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-annotationsartifactId>
            <version>${jackson.version}version>
        dependency>
 
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>${jackson.version}version>
        dependency>
 
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>${jackson.version}version>
        dependency>
 
        
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.8.4version>
        dependency>
 
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>3.0-alpha-1version>
            <scope>providedscope>
        dependency>
 
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
 
    dependencies>
 
    <build>
        <finalName>spring_struts2_Hibernate_demofinalName>
 
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-surefire-pluginartifactId>
                <version>2.4.2version>
                <configuration>
                    <skipTests>trueskipTests>
                configuration>
 
            plugin>
 
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-war-pluginartifactId>
                <version>2.3version>
                <configuration>
                    <webXml>src/main/webapp/WEB-INF/web.xmlwebXml>
                configuration>
            plugin>
 
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-javadoc-pluginartifactId>
                <version>2.9.1version>
                <configuration>
                    <javadocDirectory>target/javadocjavadocDirectory>
                    <reportOutputDirectory>target/javadocreportOutputDirectory>
                    <charset>UTF-8charset>
                    <encoding>UTF-8encoding>
                    <docencoding>UTF-8docencoding>
                    <show>privateshow>
                configuration>
            plugin>
 
            
            <plugin>
                <groupId>org.codehaus.cargogroupId>
                <artifactId>cargo-maven2-pluginartifactId>
                <version>1.0version>
                <configuration>
                    <container>
                        <containerId>tomcat6xcontainerId>
                        <home>F:\apache-tomcat-7.0.27home>
                    container>
                    <configuration>
                        <type>existingtype>
                        <home>F:\apache-tomcat-7.0.27home>
                    configuration>
                configuration>
            plugin>
        plugins>
 
    build>
project>

在web.xml中配置spring与struts的相关代码


    <filter>
        <filter-name>struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>struts2filter-name>
        <url-pattern>/*url-pattern>
filter-mapping>

2)配置spring的监听器

当服务启动时,就会先加载spring的配置文件


     <listener>
           <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

3)配置默认加载路径

 
    <context-param>
         <param-name>contextConfigLocationparam-name>
         <param-value>classpath:applicationContext.xmlparam-value>
context-param>

src下编写相关配置文件

1)spring:applicationContext.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">
beans>

2)hibernate:hibernate.cfg.xml

  导入相关约束,并配置数据库

 

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.cj.jdbc.Driverproperty>
    <property name="connection.url">jdbc:mysql://localhost:3306/customerproperty>
    <property name="connection.username">jiangproperty>
    <property name="connection.password">1property>

    
    <property name="dialect">org.hibernate.dialect.MySQL5Dialectproperty>

    
    <property name="hibernate.show_sql">trueproperty>
    <property name="hibernate.format_sql">trueproperty>
    <property name="hibernate.hbm2ddl.auto">updateproperty>
 
        <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProviderproperty>
    
    <mapping resource="domain/Product.hbm.xml"/>

  session-factory>
hibernate-configuration>

3)配置log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=info, stdout

4)struts2:struts.xml

  导入相关约束

转载于:https://www.cnblogs.com/jiangfeilong/p/10854163.html

你可能感兴趣的:(java,数据库,测试)