https://www.cnblogs.com/cailijia52o/p/8724710.htmlhtml
1、基本概念java
Struts:做爲基於 MVC 模式的 Web 應用最經典框架,兩個項目Struts 和webwork已經集成,成爲如今的Struts2。目前的最新版本是2.0.9(2007-7)。mysql
Spring:是一個輕型的容器,利用它可使用一個外部 XML 配置文件方便地將對象鏈接在一塊兒。每一個對象均可以經過顯示一個 JavaBean 屬性收到一個到依賴對象的引用,留給您的簡單任務就只是在一個XML 配置文件中把它們鏈接好。web
Hibernate 是一個純 Java 的對象關係映射和持久性框架,它容許您用 XML 配置文件把普通 Java 對象映射到關係數據庫表。使用 Hibernate 可以節約大量項目開發時間,由於整個 JDBC 層都由這個框架管理。這意味着您的應用程序的數據訪問層位於 Hibernate 之上,徹底是從底層數據模型中抽象出來的。spring
三種技術到目前已經比較成熟,並且他們都是免費的!讓咱們對三者集成進行一個初覽(簡單而不專業):sql
咱們用Struts實現從Web(網頁,MVC中的View)到後臺系統的映射(WebàAction),而後由Spring管理這些Action,把它們做爲Bean和其餘對象一塊兒處理。這些Bean之間處理業務邏輯、數據、系統狀態等,且它們被Spring統一管理,爲了區分,就算大概包括MVC的MC部分吧。而後須要持久化的數據由Spring和Hibernate之間的接口交由Hibernate處理(這個屬於持久層)。數據庫
必須基礎:只要Java基礎,一點HTML知識、XML基礎就能夠了。本文的目的就是從零開始創建第一個Struts+Spring+Hibernate應用。即便它是最簡單的,咱們也但願初學者可以從中理解一些思想,其中也包括系統架構的設計思想。apache
配置 pom.xml 依賴json
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.1.4.RELEASE</spring.version> <hibernate.version>4.3.8.Final</hibernate.version> <struts2.version>2.3.20</struts2.version> <jackson.version>2.5.0</jackson.version> </properties> <dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>provided</scope> </dependency> <!-- 關係型數據庫整合時需配置 如hibernate jpa等 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>${hibernate.version}</version> </dependency> <!-- struts2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts2.version}</version> <exclusions> <exclusion> <!-- Hibernate已經還有該包的依賴 --> <artifactId>javassist</artifactId> <groupId>javassist</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>${struts2.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>${struts2.version}</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- mysql鏈接 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <!-- 數據源 druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.12</version> </dependency> <!-- json --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!-- aop --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>spring_struts2_Hibernate_demo</finalName> <plugins> <!-- Run the JUnit unit tests in an isolated classloader --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.2</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> </configuration> </plugin> <!-- generate java doc --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.9.1</version> <configuration> <javadocDirectory>target/javadoc</javadocDirectory> <reportOutputDirectory>target/javadoc</reportOutputDirectory> <charset>UTF-8</charset> <encoding>UTF-8</encoding> <docencoding>UTF-8</docencoding> <show>private</show> </configuration> </plugin> <!-- 部署至本機 --> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.0</version> <configuration> <container> <containerId>tomcat6x</containerId> <home>F:\apache-tomcat-7.0.27</home> </container> <configuration> <type>existing</type> <home>F:\apache-tomcat-7.0.27</home> </configuration> </configuration> </plugin> </plugins> </build> </project>
<!-- 配置核心過濾器 --> <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>
2)配置spring的監聽器api
當服務啓動時,就會先加載spring的配置文件
<!-- 配置Spring框架整合WEB的監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
3)配置默認加載路徑
<!-- 監聽器默認加載Web-INF文件下,須要配置參數來加載指定文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
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> <!--JDBC連接--> <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/customer</property> <property name="connection.username">jiang</property> <property name="connection.password">1</property> <!--數據庫方言--> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!--自動生成表結構--> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置C3P0的鏈接池 --> <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!--加載映射文件--> <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
導入相關約束