Fork me on GitHub

Spring+SpringMVC+Mybatis整合

Spring4.1.6+SpringMVC+Mybatis3.3.0整合 MySQL5.*+文件上传

1.所需jar与解释

Spring+SpringMVC:
        spring-aop-4.1.6.RELEASE.jar
        spring-aspects-4.1.6.RELEASE.jar
        spring-beans-4.1.6.RELEASE.jar
        spring-context-4.1.6.RELEASE.jar
        spring-core-4.1.6.RELEASE.jar
        spring-expression-4.1.6.RELEASE.jar
        spring-jdbc-4.1.6.RELEASE.jar
        spring-orm-4.1.6.RELEASE.jar
        spring-tx-4.1.6.RELEASE.jar
        spring-web-4.1.6.RELEASE.jar
        spring-webmvc-4.1.6.RELEASE.jar
        
        AOP:在Spring2.*中
            aopalliance.jar
            aspectjweaver.jar

        文件上传:在Struts2中
            commons-fileupload-1.2.1.jar
            commons-io-1.3.2.jar

    Mybatis:
        mybatis-3.3.0.jar
        asm-4.2.jar
        cglib-3.1.jar
        commons-logging-1.2.jar
        log4j-1.2.17.jar
        log4j-api-2.2.jar
        log4j-core-2.2.jar
        slf4j-api-1.7.12.jar
        slf4j-log4j12-1.7.12.jar

    Mybatis与Spring整合:
        mybatis-spring-1.2.3.jar

    MySQL驱动包:
        mysql-connector-java-5.1.7-bin.jar

  如果使用maven: jar包的dependency搜索

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.zhengbin.demo</groupId>
  5     <artifactId>zzti.wd.mall</artifactId>
  6     <packaging>war</packaging>
  7     <version>0.0.1-SNAPSHOT</version>
  8     <name>zzti.wd.mall Maven Webapp</name>
  9     <url>http://maven.apache.org</url>
 10     <properties>
 11         <!-- spring版本号 -->
 12         <spring.version>4.1.6.RELEASE</spring.version>
 13         <!-- mybatis版本号 -->
 14         <mybatis.version>3.3.0</mybatis.version>
 15         <!-- log4j日志文件管理包版本 -->
 16         <slf4j.version>1.7.7</slf4j.version>
 17         <log4j.version>1.2.17</log4j.version>
 18     </properties>
 19 
 20     <dependencies>
 21         <dependency>
 22             <groupId>junit</groupId>
 23             <artifactId>junit</artifactId>
 24             <version>4.11</version>
 25             <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
 26             <scope>test</scope>
 27         </dependency>
 28         <!-- spring核心包 -->
 29         <dependency>
 30             <groupId>org.springframework</groupId>
 31             <artifactId>spring-core</artifactId>
 32             <version>${spring.version}</version>
 33         </dependency>
 34         <dependency>
 35             <groupId>org.springframework</groupId>
 36             <artifactId>spring-web</artifactId>
 37             <version>${spring.version}</version>
 38         </dependency>
 39         <dependency>
 40             <groupId>org.springframework</groupId>
 41             <artifactId>spring-oxm</artifactId>
 42             <version>${spring.version}</version>
 43         </dependency>
 44         <dependency>
 45             <groupId>org.springframework</groupId>
 46             <artifactId>spring-tx</artifactId>
 47             <version>${spring.version}</version>
 48         </dependency>
 49         <dependency>
 50             <groupId>org.springframework</groupId>
 51             <artifactId>spring-jdbc</artifactId>
 52             <version>${spring.version}</version>
 53         </dependency>
 54         <dependency>
 55             <groupId>org.springframework</groupId>
 56             <artifactId>spring-webmvc</artifactId>
 57             <version>${spring.version}</version>
 58         </dependency>
 59         <dependency>
 60             <groupId>org.springframework</groupId>
 61             <artifactId>spring-aop</artifactId>
 62             <version>${spring.version}</version>
 63         </dependency>
 64         <dependency>
 65             <groupId>org.springframework</groupId>
 66             <artifactId>spring-context-support</artifactId>
 67             <version>${spring.version}</version>
 68         </dependency>
 69         <dependency>
 70             <groupId>org.springframework</groupId>
 71             <artifactId>spring-test</artifactId>
 72             <version>${spring.version}</version>
 73         </dependency>
 74         <!-- mybatis核心包 -->
 75         <dependency>
 76             <groupId>org.mybatis</groupId>
 77             <artifactId>mybatis</artifactId>
 78             <version>${mybatis.version}</version>
 79         </dependency>
 80         <!-- mybatis/spring包 -->
 81         <dependency>
 82             <groupId>org.mybatis</groupId>
 83             <artifactId>mybatis-spring</artifactId>
 84             <version>1.2.2</version>
 85         </dependency>
 86         <!-- 导入java ee jar 包 -->
 87         <dependency>
 88             <groupId>javax</groupId>
 89             <artifactId>javaee-api</artifactId>
 90             <version>7.0</version>
 91         </dependency>
 92         <!-- 导入Mysql数据库链接jar包 -->
 93         <dependency>
 94             <groupId>mysql</groupId>
 95             <artifactId>mysql-connector-java</artifactId>
 96             <version>5.1.30</version>
 97         </dependency>
 98         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
 99         <dependency>
100             <groupId>commons-dbcp</groupId>
101             <artifactId>commons-dbcp</artifactId>
102             <version>1.2.2</version>
103         </dependency>
104         <!-- JSTL标签类 -->
105         <dependency>
106             <groupId>jstl</groupId>
107             <artifactId>jstl</artifactId>
108             <version>1.2</version>
109         </dependency>
110         <!-- 日志文件管理包 -->
111         <!-- log start -->
112         <dependency>
113             <groupId>log4j</groupId>
114             <artifactId>log4j</artifactId>
115             <version>${log4j.version}</version>
116         </dependency>
117         <!-- 格式化对象,方便输出日志 -->
118         <dependency>
119             <groupId>com.alibaba</groupId>
120             <artifactId>fastjson</artifactId>
121             <version>1.1.41</version>
122         </dependency>
123         <dependency>
124             <groupId>org.slf4j</groupId>
125             <artifactId>slf4j-api</artifactId>
126             <version>${slf4j.version}</version>
127         </dependency>
128         <dependency>
129             <groupId>org.slf4j</groupId>
130             <artifactId>slf4j-log4j12</artifactId>
131             <version>${slf4j.version}</version>
132         </dependency>
133         <!-- log end -->
134         <!-- 映入JSON -->
135         <dependency>
136             <groupId>org.codehaus.jackson</groupId>
137             <artifactId>jackson-mapper-asl</artifactId>
138             <version>1.9.13</version>
139         </dependency>
140         <!-- 上传组件包 -->
141         <dependency>
142             <groupId>commons-fileupload</groupId>
143             <artifactId>commons-fileupload</artifactId>
144             <version>1.3.1</version>
145         </dependency>
146         <dependency>
147             <groupId>commons-io</groupId>
148             <artifactId>commons-io</artifactId>
149             <version>2.4</version>
150         </dependency>
151         <dependency>
152             <groupId>commons-codec</groupId>
153             <artifactId>commons-codec</artifactId>
154             <version>1.9</version>
155         </dependency>
156         <!-- Spring配置事物管理-->
157         <dependency>
158             <groupId>org.aspectj</groupId>
159             <artifactId>aspectjweaver</artifactId>
160             <version>1.7.2</version>
161         </dependency>
162     </dependencies>
163     <build>
164         <finalName>zzti.wd.mall</finalName>
165     </build>
166 </project>
pom.xml

2.编写配置文件

  (1)web.xml ——Web容器

 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_3_0.xsd"
 5     id="WebApp_ID" version="3.0">
 6     <display-name>07spring+springmvc+mybatis</display-name>
 7 
 8     <!-- spring 配置 begin -->
 9     <context-param>
10         <param-name>contextConfigLocation</param-name>
11         <param-value>classpath:applicationContext.xml</param-value>
12     </context-param>
13     <listener>
14         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
15     </listener>
16     <!-- spring 配置 end -->
17 
18     <!-- 编码过滤器 begin -->
19     <filter>
20         <filter-name>encodingFilter</filter-name>
21         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
22         <async-supported>true</async-supported>
23         <init-param>
24             <param-name>encoding</param-name>
25             <param-value>UTF-8</param-value>
26         </init-param>
27     </filter>
28     <filter-mapping>
29         <filter-name>encodingFilter</filter-name>
30         <url-pattern>/*</url-pattern>
31     </filter-mapping>
32     <!-- 编码过滤器 end -->
33 
34     <!-- spring mvc 配置 begin -->
35     <servlet>
36         <servlet-name>springmvc</servlet-name>
37         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
38         <init-param>
39             <param-name>contextConfigLocation</param-name>
40             <param-value>classpath:mvc.xml</param-value>
41         </init-param>
42         <load-on-startup>1</load-on-startup>
43     </servlet>
44     <servlet-mapping>
45         <servlet-name>springmvc</servlet-name>
46         <url-pattern>*.do</url-pattern>
47     </servlet-mapping>
48     <!-- spring mvc 配置 end -->
49 
50 </web-app>
web.xml

  (2)mvc.xml ——SpringMVC

 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:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:mvc="http://www.springframework.org/schema/mvc"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans
 9         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd 
12         http://www.springframework.org/schema/mvc
13         http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
14 
15     <!-- 配置文件上传 begin -->
16     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
17         <property name="defaultEncoding" value="utf-8"/>
18         <property name="maxUploadSize" value="10485760000"/>
19         <property name="maxInMemorySize" value="40960"/>
20     </bean>
21     <!-- 配置文件上传 end -->
22     
23     <!-- 扫描该包中的所有注解 -->
24     <context:component-scan base-package="com.wd.controller"/>
25 </beans>
mvc.xml

  (3)applicationContext.xml ——Spring

 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:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     
 9     xsi:schemaLocation="
10         http://www.springframework.org/schema/beans
11         http://www.springframework.org/schema/beans/spring-beans.xsd
12         http://www.springframework.org/schema/context
13         http://www.springframework.org/schema/context/spring-context.xsd 
14         http://www.springframework.org/schema/aop
15         http://www.springframework.org/schema/aop/spring-aop.xsd
16         http://www.springframework.org/schema/tx
17         http://www.springframework.org/schema/tx/spring-tx.xsd ">
18     <!-- 配置datasource begin -->
19     <!-- 读取数据库配置文件 -->
20     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
21         <property name="location" value="classpath:db.properties"/>
22     </bean>
23     <!-- 用Spring自带的jdbc -->
24     <!--
25     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
26         <property name="driverClassName" value="${driver}"/>
27         <property name="url" value="${url}"/>
28         <property name="username" value="${username}"/>
29         <property name="password" value="${password}"/>
30     </bean> 
31     -->
32     <!-- 使用dbcp连接池 -->
33     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
34         destroy-method="close">
35         <property name="driverClassName" value="${driver}" />
36         <property name="url" value="${url}" />
37         <property name="username" value="${username}" />
38         <property name="password" value="${password}" />
39         <!-- 初始化连接大小 -->
40         <property name="initialSize" value="${initialSize}"></property>
41         <!-- 连接池最大数量 -->
42         <property name="maxActive" value="${maxActive}"></property>
43         <!-- 连接池最大空闲 -->
44         <property name="maxIdle" value="${maxIdle}"></property>
45         <!-- 连接池最小空闲 -->
46         <property name="minIdle" value="${minIdle}"></property>
47         <!-- 获取连接最大等待时间 -->
48         <property name="maxWait" value="${maxWait}"></property>
49     </bean>
50     <!-- 配置datasource end -->
51 
52     <!-- 配置SqlSessionFactory begin -->
53     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
54         <property name="dataSource" ref="dataSource"/>
55         <property name="configLocation" value="classpath:mybatis.cfg.xml"/>
56     </bean>
57     <!-- 配置SqlSessionFactory end -->
58     
59     <!-- 配置事物 声明式事物 begin -->
60     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
61         <property name="dataSource" ref="dataSource"/>
62     </bean>
63     <tx:advice id="txAdvice" transaction-manager="txManager">
64         <!-- 配置哪些方法使用什么样的事物,配置事物的传播特性 -->
65         <!-- REQUIRED表示如果不存在事物则必须产生一个事物 -->
66         <tx:attributes>
67             <tx:method name="add*" propagation="REQUIRED"/>
68             <tx:method name="delete*" propagation="REQUIRED"/>
69             <tx:method name="update*" propagation="REQUIRED"/>
70             <tx:method name="*" read-only="true"/>
71         </tx:attributes>
72     </tx:advice>
73     <aop:config>
74         <aop:pointcut id="pointcut" expression="execution(* com.wd.service.*.*(..))" />
75         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
76     </aop:config>
77     <!-- 配置事物 声明式事物 end -->
78     
79     <!-- 配置注解 begin -->
80     <context:component-scan base-package="com.wd"/>    
81     <!-- 配置注解 end -->
82 </beans>
applicationContext.xml

  (4)mybatis.cfg.xml ——Mybatis

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
 3     "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5     <typeAliases>
 6         <package name="com.wd.entity"/>
 7     </typeAliases>
 8     <mappers>
 9         <!-- 所有mapper文件填写位置 -->
10         <mapper resource="com/wd/entity/book.mapper.xml"/>
11         <mapper resource="com/wd/entity/computer.mapper.xml"/>
12     </mappers>
13 </configuration>
mybatis.cfg.xml

  (5)db.properties ——数据源

 1 driver=com.mysql.jdbc.Driver
 2 url=jdbc:mysql://localhost:3307/wdmall
 3 username=root
 4 password=950906
 5 #定义初始连接数
 6 initialSize=3
 7 #定义最大连接数  
 8 maxActive=20
 9 #定义最大空闲  
10 maxIdle=20
11 #定义最小空闲  
12 minIdle=3
13 #定义最长等待时间  
14 maxWait=6000
db.properties

 

posted @ 2016-03-17 08:19  郑斌blog  阅读(529)  评论(0编辑  收藏  举报