Spring+mybatis+struts框架整合的配置详解

来源:互联网 发布:javascript var obj 编辑:程序博客网 时间:2024/06/05 18:10




目录(?)[-]

  1. 导入相应的jar包
  2. struts的strutsxml配置
  3. Spring的beansxml
  4. webxml
  5. 测试

转载自: http://blog.csdn.net/u012036171/article/details/47469065


学了很久的spring+mybatis+struts.一直都是单个的用他们,或者是两两组合用过,今天总算整合到一起了,配置起来有点麻烦,但是配置完一次之后,就轻松多了,那么框架整合配置详解如下。

1、导入相应的jar包

因为我们建造的是maven的web项目,所有我们在pom.xml中需要导入这些包。

pom.xml 详细注释

<?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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153

这里导入和很多包,大部分是spring+mybatis+struts的 基础包以及他们之间连接的包,因为时maven项目,所有有很多互相依赖的包这里不需要再引用。

导包完毕后我们来回顾一下, 
我们学习struts的时候,需要配置一个xml文件叫struts.xml; 
学习spring的时候,我们需要配置spring的文件叫beans.xml; 
因为我们是web项目,所有我们也需要再次配置web.xml; 
所有我们接下来来一个个的配置整合。

2、struts的struts.xml配置

struts.xml

<?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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

因为还没有具体的实现类,所有我们这里没有配置action。 
在这个struts的配置中,我们和以前不一样的额地方就是多了一句

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

这句代码的意思就是说 
struts结合spring的配置意思是 Struts2的action由Spring来负责进行实例化。换句话说就是: 
比如下面这个案例

这里写图片描述

在这个action的配置这里的class部分必须写spring中配置的action的id名,因为这个时候,由spring来生成action对象。

spring的beans.xml 
这里写图片描述

接下来是:

3、 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

具体的在上面的xml中我都写了注释。 
值得注意的是,里面有一个

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

在jdbc.propertits我们写的是数据连接配置

jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8jdbc.username=rootjdbc.password=ajdbc.maxActive=150jdbc.minIdle=5jdbc.maxIdle=20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

最后呢 是

4、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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

配置不同的地方我都写了注释。 
值得注意的是:Web.xml配置中context-param 
如下:

初始化过程: 
1、在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点< listener >和< contex-param >。

2、接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。

3、接着容器会将读取到< context-param>转化为键值对,并交给ServletContext。

4、容器创建< listener>中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。

5、在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter(“contextConfigLocation”) 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。

6、得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。

===由上面的初始化过程可知容器对于web.xml的加载过程是context-param >> listener >> fileter >> servlet

最后

5、测试

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);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

美丽的绿色 
这里写图片描述


0 0
原创粉丝点击