Spring+mybatis+struts框架整合的配置具體解釋

學了很是久的spring+mybatis+struts.一直都是單個的用他們,或者是兩兩組合用過,今天總算整合到一塊兒了。配置起來有點麻煩,但是配置完一次以後,就輕鬆多了,那麼框架整合配置具體解釋例如如下。html

一、導入對應的jar包

因爲咱們建造的是maven的web項目,所有咱們在pom.xml中需要導入這些包。java

pom.xml 具體凝視mysql

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>

    <name>cinema</name>
    <groupId>com.yc.ssm.cinema</groupId>
    <artifactId>cinema</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.7</version>
                <configuration>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8888</port>
                            <maxIdleTime>30000</maxIdleTime>
                        </connector>
                    </connectors>
                    <webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version}</webAppSourceDirectory>
                    <contextPath>/</contextPath>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <!-- 導入對應的jar包 -->
    <dependencies>
        <!-- mybatis包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!--mybatis和spring整合的包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.1</version>
        </dependency>

        <!-- junit測試的包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!-- spring 的包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <!--web項目 需要spring-web-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <!-- spring的測試類-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <!-- spring-jdbc: 支持事務處理.. -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <!-- 數據聯接池 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>20030825.184428</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>20030825.183949</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>20040616</version>
        </dependency>
        <!-- 數據庫驅動.. -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
        <!-- 因爲使用了 javax.annotation包中的註解。 -->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation</artifactId>
            <version>1.1.0.v201105051105</version>
        </dependency>

        <!-- 日誌包 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>

        <!-- struts2 核心包 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.20</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>2.3.20</version>
        </dependency>


        <!-- web項目 需要servlet包支撐 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2.1-b03</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.20</version>
        </dependency>
    </dependencies>

</project>

這裏導入和很是多包,大部分是spring+mybatis+struts的 基礎包以及他們之間鏈接的包。因爲時maven項目。所有有很是多互相依賴的包這裏不需要再引用。web

導包完畢後咱們來回想一下,
咱們學習struts的時候。需要配置一個xml文件叫struts.xml。
學習spring的時候,咱們需要配置spring的文件叫beans.xml。
因爲咱們是web項目,所有咱們也需要再次配置web.xml;
所有咱們接下來來一個個的配置整合。spring

二、struts的struts.xml配置

struts.xmlsql

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <!-- struts結合spring的配置意思是 Struts2的action由Spring來負責進行實例化 -->
    <constant name="struts.objectFactory" value="spring" />
    <package name="default" namespace="/" extends="struts-default">

    </package>

</struts>

因爲尚未具體的實現類,所有咱們這裏沒有配置action。
在這個struts的配置中,咱們和曾經不同的額地方就是多了一句數據庫

<constant name="struts.objectFactory" value="spring" />

這句代碼的意思就是說
struts結合spring的配置意思是 Struts2的action由Spring來負責進行實例化。apache

換句話說就是:
比方如下這個案例json

這裏寫圖片描寫敘述

在這個action的配置這裏的class部分必須寫spring中配置的action的id名,因爲這個時候。由spring來生成action對象。api

spring的beans.xml
這裏寫圖片描寫敘述

接下來是:

三、 Spring的beans.xml

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 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 ">

    <context:annotation-config />
    <!-- spring可以本身主動去掃描base-pack如下或者子包如下的java文件。假設掃描到有@Component @Controller@Service等這些註解的類,則把這些類註冊爲bean -->
    <context:component-scan base-package="com.yc.ssm.cinema" />
    <!-- 讀取配置文件的操做 -->
    <context:property-placeholder location="classpath:jdbc.propertits" />


    <!-- 配置dbcp數據源... 數據庫聯接池 ( jndi-> tomcat的數據庫聯接池 ) -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="${jdbc.maxActive}" />
        <property name="minIdle" value="${jdbc.minIdle}" />
        <property name="maxIdle" value="${jdbc.maxIdle}" />
    </bean>

    <!-- 配置mybatis整合的bean -->
     <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.yc.ssm.cinema.entity" />
        <!-- 配置所有的mapper 文件的位置 -->
        <property name="mapperLocations" value="classpath:mapper/*Mapper.xml" />
    </bean> 

    <!-- 配置映射接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置要掃描的映射文件對應的接口的文件夾 -->
        <property name="basePackage" value="com.yc.ssm.cinema.mapper" />
        <!-- 指定這個 scanner 所使用的sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
</beans>

具體的在上面的xml中我都寫了凝視。
值得注意的是,裏面有一個

<context:property-placeholder location="classpath:jdbc.propertits" />

在jdbc.propertits咱們寫的是數據鏈接配置

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=a
jdbc.maxActive=150
jdbc.minIdle=5
jdbc.maxIdle=20

最後呢 是

四、web.xml

<?

xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.yc.ssm.cinema.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 做用:該元素用來聲明應用範圍(整個WEB項目)內的上下文初始化參數。

param-name 設定上下文的參數名稱。必須是惟一名稱 param-value 設定的參數名稱的值 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:beans*.xml</param-value> </context-param> <!-- ContextLoaderListener的做用就是啓動Web容器時。 本身主動裝配ApplicationContext的配置信息。

因爲它實現了ServletContextListener這個接口, 在web.xml配置這個監聽器。啓動容器時,就會默認運行它實現的方法。 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>add.jsp</welcome-file> </welcome-file-list> </web-app>

配置不一樣的地方我都寫了凝視。
值得注意的是:Web.xml配置中context-param
例如如下:

初始化過程:
一、在啓動Web項目時,容器(比方Tomcat)會讀web.xml配置文件裏的兩個節點< listener >和< contex-param >。

二、接着容器會建立一個ServletContext(上下文),應用範圍內即整個WEB項目都能使用這個上下文。

三、接着容器會將讀取到< context-param>轉化爲鍵值對,並交給ServletContext。

四、容器建立< listener>中的類實例,即建立監聽(備註:listener定義的類可以是本身定義的類但必須需要繼承ServletContextListener)。

五、在監聽的類中會有一個contextInitialized(ServletContextEvent event)初始化方法。在這種方法中可以經過event.getServletContext().getInitParameter(「contextConfigLocation」) 來獲得context-param 設定的值。在這個類中還必須有一個contextDestroyed(ServletContextEvent event) 銷燬方法.用於關閉應用前釋放資源,比方說數據庫鏈接的關閉。

六、獲得這個context-param的值以後,你就可以作一些操做了.注意,這個時候你的WEB項目尚未全然啓動完畢.這個動做會比所有的Servlet都要早。

===由上面的初始化過程可知容器對於web.xml的載入過程是context-param >> listener >> fileter >> servlet

最後

五、測試

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;





import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class Test {
    @Autowired
    private DataSource dataSource;
    @org.junit.Test
    public void test(){
        Connection con=null;
        try {
            con=dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        assertNotNull("數據庫鏈接失敗",con);
    }
}

漂亮的綠色
這裏寫圖片描寫敘述

相關文章
相關標籤/搜索