人認為使用框架并不是很難,關(guān)鍵要理解其思想,這對于我們提高編程水平很有幫助。不過,如果用都不會,談思想就變成紙上談兵了?。?!先技術(shù),再思想。實踐出真知。
1、基本概念
1.1、Spring
?Spring是一個開源框架,Spring是于2003?年興起的一個輕量級的Java?開發(fā)框架,由Rod?Johnson?在其著作Expert?One-On-One?J2EE?Development?and?Design中闡述的部分理念和原型衍生而來。它是為了解決企業(yè)應(yīng)用開發(fā)的復(fù)雜性而創(chuàng)建的。Spring使用基本的JavaBean來完成以前只可能由EJB完成的事情。然而,Spring的用途不僅限于服務(wù)器端的開發(fā)。從簡單性、可測試性和松耦合的角度而言,任何Java應(yīng)用都可以從Spring中受益。?簡單來說,Spring是一個輕量級的控制反轉(zhuǎn)(IoC)和面向切面(AOP)的容器框架。
1.2、SpringMVC
Spring?MVC屬于SpringFrameWork的后續(xù)產(chǎn)品,已經(jīng)融合在Spring?Web?Flow里面。Spring?MVC?分離了控制器、模型對象、分派器以及處理程序?qū)ο蟮慕巧?,這種分離讓它們更容易進行定制。
1.3、MyBatis
MyBatis?本是apache的一個開源項目iBatis,?2010年這個項目由apache?software?foundation?遷移到了google?code,并且改名為MyBatis?。MyBatis是一個基于Java的持久層框架。iBATIS提供的持久層框架包括SQL?Maps和Data?Access?Objects(DAO)MyBatis?消除了幾乎所有的JDBC代碼和參數(shù)的手工設(shè)置以及結(jié)果集的檢索。MyBatis?使用簡單的?XML或注解用于配置和原始映射,將接口和?Java?的POJOs(Plain?Old?Java?Objects,普通的?Java對象)映射成數(shù)據(jù)庫中的記錄。
2、開發(fā)環(huán)境搭建
如果需要,請參考這篇文章:http://blog.csdn.net/zhshulin/article/details/30779873
3、Maven Web項目創(chuàng)建
如果需要,請參考這篇文章:http://blog.csdn.net/zhshulin/article/details/37921705
4、SSM整合
下面主要介紹三大框架的整合,至于環(huán)境的搭建以及項目的創(chuàng)建,參看上面的博文。這次整合我分了2個配置文件,分別是spring-mybatis.xml,包含spring和mybatis的配置文件,還有個是spring-mvc的配置文件,此外有2個資源文件:jdbc.propertis和log4j.properties。完整目錄結(jié)構(gòu)如下(最后附上源碼下載地址,不建議直接使用源碼,因為此教程已經(jīng)有了全部代碼):
?使用框架都是較新的版本:
? ? ? ?Spring?4.0.2?RELEASE
? ? ? ?Spring?MVC?4.0.2?RELEASE
? ? ? ?MyBatis?3.2.6
4.1、Maven引入需要的JAR包
為了方便后面說的時候不需要引入JAR包,我這里直接給出所有需要的JAR包,這都是基本的JAR包,每個包的是干什么的都有注釋,就不再多說了。
pom.xml


? 1 <properties>
? 2? ? ? ? <!-- spring版本號 -->
? 3? ? ? ? <spring.version>4.0.2.RELEASE</spring.version>
? 4? ? ? ? <!-- mybatis版本號 -->
? 5? ? ? ? <mybatis.version>3.2.6</mybatis.version>
? 6? ? ? ? <!-- log4j日志文件管理包版本 -->
? 7? ? ? ? <slf4j.version>1.7.7</slf4j.version>
? 8? ? ? ? <log4j.version>1.2.17</log4j.version>
? 9? ? </properties>
10?
11? ? <dependencies>
12? ? ? ? <dependency>
13? ? ? ? ? ? <groupId>junit</groupId>
14? ? ? ? ? ? <artifactId>junit</artifactId>
15? ? ? ? ? ? <version>4.11</version>
16? ? ? ? ? ? <!-- 表示開發(fā)的時候引入,發(fā)布的時候不會加載此包 -->
17? ? ? ? ? ? <scope>test</scope>
18? ? ? ? </dependency>
19? ? ? ? <!-- spring核心包 -->
20? ? ? ? <dependency>
21? ? ? ? ? ? <groupId>org.springframework</groupId>
22? ? ? ? ? ? <artifactId>spring-core</artifactId>
23? ? ? ? ? ? <version>${spring.version}</version>
24? ? ? ? </dependency>
25?
26? ? ? ? <dependency>
27? ? ? ? ? ? <groupId>org.springframework</groupId>
28? ? ? ? ? ? <artifactId>spring-web</artifactId>
29? ? ? ? ? ? <version>${spring.version}</version>
30? ? ? ? </dependency>
31? ? ? ? <dependency>
32? ? ? ? ? ? <groupId>org.springframework</groupId>
33? ? ? ? ? ? <artifactId>spring-oxm</artifactId>
34? ? ? ? ? ? <version>${spring.version}</version>
35? ? ? ? </dependency>
36? ? ? ? <dependency>
37? ? ? ? ? ? <groupId>org.springframework</groupId>
38? ? ? ? ? ? <artifactId>spring-tx</artifactId>
39? ? ? ? ? ? <version>${spring.version}</version>
40? ? ? ? </dependency>
41?
42? ? ? ? <dependency>
43? ? ? ? ? ? <groupId>org.springframework</groupId>
44? ? ? ? ? ? <artifactId>spring-jdbc</artifactId>
45? ? ? ? ? ? <version>${spring.version}</version>
46? ? ? ? </dependency>
47?
48? ? ? ? <dependency>
49? ? ? ? ? ? <groupId>org.springframework</groupId>
50? ? ? ? ? ? <artifactId>spring-webmvc</artifactId>
51? ? ? ? ? ? <version>${spring.version}</version>
52? ? ? ? </dependency>
53? ? ? ? <dependency>
54? ? ? ? ? ? <groupId>org.springframework</groupId>
55? ? ? ? ? ? <artifactId>spring-aop</artifactId>
56? ? ? ? ? ? <version>${spring.version}</version>
57? ? ? ? </dependency>
58?
59? ? ? ? <dependency>
60? ? ? ? ? ? <groupId>org.springframework</groupId>
61? ? ? ? ? ? <artifactId>spring-context-support</artifactId>
62? ? ? ? ? ? <version>${spring.version}</version>
63? ? ? ? </dependency>
64?
65? ? ? ? <dependency>
66? ? ? ? ? ? <groupId>org.springframework</groupId>
67? ? ? ? ? ? <artifactId>spring-test</artifactId>
68? ? ? ? ? ? <version>${spring.version}</version>
69? ? ? ? </dependency>
70? ? ? ? <!-- mybatis核心包 -->
71? ? ? ? <dependency>
72? ? ? ? ? ? <groupId>org.mybatis</groupId>
73? ? ? ? ? ? <artifactId>mybatis</artifactId>
74? ? ? ? ? ? <version>${mybatis.version}</version>
75? ? ? ? </dependency>
76? ? ? ? <!-- mybatis/spring包 -->
77? ? ? ? <dependency>
78? ? ? ? ? ? <groupId>org.mybatis</groupId>
79? ? ? ? ? ? <artifactId>mybatis-spring</artifactId>
80? ? ? ? ? ? <version>1.2.2</version>
81? ? ? ? </dependency>
82? ? ? ? <!-- 導(dǎo)入java ee jar 包 -->
83? ? ? ? <dependency>
84? ? ? ? ? ? <groupId>javax</groupId>
85? ? ? ? ? ? <artifactId>javaee-api</artifactId>
86? ? ? ? ? ? <version>7.0</version>
87? ? ? ? </dependency>
88? ? ? ? <!-- 導(dǎo)入Mysql數(shù)據(jù)庫鏈接jar包 -->
89? ? ? ? <dependency>
90? ? ? ? ? ? <groupId>mysql</groupId>
91? ? ? ? ? ? <artifactId>mysql-connector-java</artifactId>
92? ? ? ? ? ? <version>5.1.30</version>
93? ? ? ? </dependency>
94? ? ? ? <!-- 導(dǎo)入dbcp的jar包,用來在applicationContext.xml中配置數(shù)據(jù)庫 -->
95? ? ? ? <dependency>
96? ? ? ? ? ? <groupId>commons-dbcp</groupId>
97? ? ? ? ? ? <artifactId>commons-dbcp</artifactId>
98? ? ? ? ? ? <version>1.2.2</version>
99? ? ? ? </dependency>
100? ? ? ? <!-- JSTL標簽類 -->
101? ? ? ? <dependency>
102? ? ? ? ? ? <groupId>jstl</groupId>
103? ? ? ? ? ? <artifactId>jstl</artifactId>
104? ? ? ? ? ? <version>1.2</version>
105? ? ? ? </dependency>
106? ? ? ? <!-- 日志文件管理包 -->
107? ? ? ? <!-- log start -->
108? ? ? ? <dependency>
109? ? ? ? ? ? <groupId>log4j</groupId>
110? ? ? ? ? ? <artifactId>log4j</artifactId>
111? ? ? ? ? ? <version>${log4j.version}</version>
112? ? ? ? </dependency>
113? ? ? ?
114? ? ? ?
115? ? ? ? <!-- 格式化對象,方便輸出日志 -->
116? ? ? ? <dependency>
117? ? ? ? ? ? <groupId>com.alibaba</groupId>
118? ? ? ? ? ? <artifactId>fastjson</artifactId>
119? ? ? ? ? ? <version>1.1.41</version>
120? ? ? ? </dependency>
121?
122?
123? ? ? ? <dependency>
124? ? ? ? ? ? <groupId>org.slf4j</groupId>
125? ? ? ? ? ? <artifactId>slf4j-api</artifactId>
126? ? ? ? ? ? <version>${slf4j.version}</version>
127? ? ? ? </dependency>
128?
129? ? ? ? <dependency>
130? ? ? ? ? ? <groupId>org.slf4j</groupId>
131? ? ? ? ? ? <artifactId>slf4j-log4j12</artifactId>
132? ? ? ? ? ? <version>${slf4j.version}</version>
133? ? ? ? </dependency>
134? ? ? ? <!-- log end -->
135? ? ? ? <!-- 映入JSON -->
136? ? ? ? <dependency>
137? ? ? ? ? ? <groupId>org.codehaus.jackson</groupId>
138? ? ? ? ? ? <artifactId>jackson-mapper-asl</artifactId>
139? ? ? ? ? ? <version>1.9.13</version>
140? ? ? ? </dependency>
141? ? ? ? <!-- 上傳組件包 -->
142? ? ? ? <dependency>
143? ? ? ? ? ? <groupId>commons-fileupload</groupId>
144? ? ? ? ? ? <artifactId>commons-fileupload</artifactId>
145? ? ? ? ? ? <version>1.3.1</version>
146? ? ? ? </dependency>
147? ? ? ? <dependency>
148? ? ? ? ? ? <groupId>commons-io</groupId>
149? ? ? ? ? ? <artifactId>commons-io</artifactId>
150? ? ? ? ? ? <version>2.4</version>
151? ? ? ? </dependency>
152? ? ? ? <dependency>
153? ? ? ? ? ? <groupId>commons-codec</groupId>
154? ? ? ? ? ? <artifactId>commons-codec</artifactId>
155? ? ? ? ? ? <version>1.9</version>
156? ? ? ? </dependency>
157? ? ? ?
158? ? ? ?
159? ? </dependencies>


4.2、Spring與MyBatis的整合
所有需要的JAR包都引入以后,首先進行Spring與MyBatis的整合,然后再進行JUnit測試,先看一個項目結(jié)構(gòu)圖:
4.2.1、建立JDBC屬性文件
jdbc.properties(文件編碼修改為utf-8)


1 driver=com.mysql.jdbc.Driver
2 url=jdbc:mysql://10.221.10.111:8080/db_zsl
3 username=demao
4 password=demao
5 #定義初始連接數(shù)
6 initialSize=0
7 #定義最大連接數(shù)
8 maxActive=20
9 #定義最大空閑
10 maxIdle=20
11 #定義最小空閑
12 minIdle=1
13 #定義最長等待時間
14 maxWait=60000


4.2.2、建立spring-mybatis.xml配置文件
這個文件就是用來完成spring和mybatis的整合的。這里面也沒多少行配置,主要的就是自動掃描,自動注入,配置數(shù)據(jù)庫。注釋也很詳細,大家看看就明白了。
spring-mybatis.xml


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" xmlns:p="http://www.springframework.org/schema/p"
4? ? xmlns:context="http://www.springframework.org/schema/context"
5? ? xmlns:mvc="http://www.springframework.org/schema/mvc"
6? ? xsi:schemaLocation="http://www.springframework.org/schema/beans?
7? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/beans/spring-beans-3.1.xsd?
8? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/context?
9? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/context/spring-context-3.1.xsd?
10? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/mvc?
11? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12? ? <!-- 自動掃描 -->
13? ? <context:component-scan base-package="com.cn.hnust" />
14? ? <!-- 引入配置文件 -->
15? ? <bean id="propertyConfigurer"
16? ? ? ? class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
17? ? ? ? <property name="location" value="classpath:jdbc.properties" />
18? ? </bean>
19?
20? ? <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
21? ? ? ? destroy-method="close">
22? ? ? ? <property name="driverClassName" value="${driver}" />
23? ? ? ? <property name="url" value="${url}" />
24? ? ? ? <property name="username" value="${username}" />
25? ? ? ? <property name="password" value="${password}" />
26? ? ? ? <!-- 初始化連接大小 -->
27? ? ? ? <property name="initialSize" value="${initialSize}"></property>
28? ? ? ? <!-- 連接池最大數(shù)量 -->
29? ? ? ? <property name="maxActive" value="${maxActive}"></property>
30? ? ? ? <!-- 連接池最大空閑 -->
31? ? ? ? <property name="maxIdle" value="${maxIdle}"></property>
32? ? ? ? <!-- 連接池最小空閑 -->
33? ? ? ? <property name="minIdle" value="${minIdle}"></property>
34? ? ? ? <!-- 獲取連接最大等待時間 -->
35? ? ? ? <property name="maxWait" value="${maxWait}"></property>
36? ? </bean>
37?
38? ? <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
39? ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
40? ? ? ? <property name="dataSource" ref="dataSource" />
41? ? ? ? <!-- 自動掃描mapping.xml文件 -->
42? ? ? ? <property name="mapperLocations" value="classpath:com/cn/hnust/mapping/*.xml"></property>
43? ? </bean>
44?
45? ? <!-- DAO接口所在包名,Spring會自動查找其下的類 -->
46? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
47? ? ? ? <property name="basePackage" value="com.cn.hnust.dao" />
48? ? ? ? <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
49? ? </bean>
50?
51? ? <!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx -->
52? ? <bean id="transactionManager"
53? ? ? ? class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
54? ? ? ? <property name="dataSource" ref="dataSource" />
55? ? </bean>
56?
57 </beans>


4.2.3、Log4j的配置
為了方便調(diào)試,一般都會使用日志來輸出信息,Log4j是Apache的一個開放源代碼項目,通過使用Log4j,我們可以控制日志信息輸送的目的地是控制臺、文件、GUI組件,甚至是套接口服務(wù)器、NT的事件記錄器、UNIX?Syslog守護進程等;我們也可以控制每一條日志的輸出格式;通過定義每一條日志信息的級別,我們能夠更加細致地控制日志的生成過程。
Log4j的配置很簡單,而且也是通用的,下面給出一個基本的配置,換到其他項目中也無需做多大的調(diào)整,如果想做調(diào)整或者想了解Log4j的各種配置,參看這篇博文,很詳細:
http://blog.csdn.net/zhshulin/article/details/37937365
下面給出配置文件目錄:
log4j.properties


1 #定義LOG輸出級別
2 log4j.rootLogger=INFO,Console,File
3 #定義日志輸出目的地為控制臺
4 log4j.appender.Console=org.apache.log4j.ConsoleAppender
5 log4j.appender.Console.Target=System.out
6 #可以靈活地指定日志輸出格式,下面一行是指定具體的格式
7 log4j.appender.Console.layout = org.apache.log4j.PatternLayout
8 log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
9?
10 #文件大小到達指定尺寸的時候產(chǎn)生一個新的文件
11 log4j.appender.File = org.apache.log4j.RollingFileAppender
12 #指定輸出目錄
13 log4j.appender.File.File = logs/ssm.log
14 #定義文件最大大小
15 log4j.appender.File.MaxFileSize = 10MB
16 # 輸出所以日志,如果換成DEBUG表示輸出DEBUG以上級別日志
17 log4j.appender.File.Threshold = ALL
18 log4j.appender.File.layout = org.apache.log4j.PatternLayout
19 log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n


4.2.4、JUnit測試
經(jīng)過以上步驟(到4.2.2,log4j不配也沒影響),我們已經(jīng)完成了Spring和mybatis的整合,這樣我們就可以編寫一段測試代碼來試試是否成功了。
4.2.4.1、創(chuàng)建測試用表
既然我們需要測試,那么我們就需要建立在數(shù)據(jù)庫中建立一個測試表,這個表建的很簡單,SQL語句為:


1 DROP TABLE IF EXISTS `user_t`;
2?
3 CREATE TABLE `user_t` (
4? `id` int(11) NOT NULL AUTO_INCREMENT,
5? `user_name` varchar(40) NOT NULL,
6? `password` varchar(255) NOT NULL,
7? `age` int(4) NOT NULL,
8? PRIMARY KEY (`id`)
9 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
10?
11 /*Data for the table `user_t` */
12?
13 insert? into `user_t`(`id`,`user_name`,`password`,`age`) values (1,'測試','sfasgfaf',24);


4.2.4.2、利用MyBatis Generator自動創(chuàng)建代碼
參考博文:http://blog.csdn.net/zhshulin/article/details/23912615
這個可根據(jù)表自動創(chuàng)建實體類、MyBatis映射文件以及DAO接口,當然,我習(xí)慣將生成的接口名改為IUserDao,而不是直接用它生成的UserMapper。如果不想麻煩就可以不改。完成后將文件復(fù)制到工程中。如圖:
4.2.4.3、建立Service接口和實現(xiàn)類
目錄結(jié)構(gòu):
下面給出具體的內(nèi)容:
IUserService.jave


1 package com.cn.hnust.service;
2?
3 import com.cn.hnust.pojo.User;
4?
5 public interface IUserService {
6? ? public User getUserById(int userId);
7 }


UserServiceImpl.java


1 package com.cn.hnust.service.impl;
2?
3 import javax.annotation.Resource;
4?
5 import org.springframework.stereotype.Service;
6?
7 import com.cn.hnust.dao.IUserDao;
8 import com.cn.hnust.pojo.User;
9 import com.cn.hnust.service.IUserService;
10?
11 @Service("userService")
12 public class UserServiceImpl implements IUserService {
13? ? @Resource
14? ? private IUserDao userDao;
15? ? @Override
16? ? public User getUserById(int userId) {
17? ? ? ? // TODO Auto-generated method stub
18? ? ? ? return this.userDao.selectByPrimaryKey(userId);
19? ? }
20?
21 }


4.2.4.4、建立測試類
測試類在src/test/java中建立,下面測試類中注釋掉的部分是不使用Spring時,一般情況下的一種測試方法;如果使用了Spring那么就可以使用注解的方式來引入配置文件和類,然后再將service接口對象注入,就可以進行測試了。
如果測試成功,表示Spring和Mybatis已經(jīng)整合成功了。輸出信息使用的是Log4j打印到控制臺。


1 package org.zsl.testmybatis;
2?
3 import javax.annotation.Resource;
4?
5 import org.apache.log4j.Logger;
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.junit.runner.RunWith;
9 import org.springframework.context.ApplicationContext;
10 import org.springframework.context.support.ClassPathXmlApplicationContext;
11 import org.springframework.test.context.ContextConfiguration;
12 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13?
14 import com.alibaba.fastjson.JSON;
15 import com.cn.hnust.pojo.User;
16 import com.cn.hnust.service.IUserService;
17?
18 @RunWith(SpringJUnit4ClassRunner.class)? ? ? ? //表示繼承了SpringJUnit4ClassRunner類
19 @ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
20?
21 public class TestMyBatis {
22? ? private static Logger logger = Logger.getLogger(TestMyBatis.class);
23 //? ? private ApplicationContext ac = null;
24? ? @Resource
25? ? private IUserService userService = null;
26?
27 //? ? @Before
28 //? ? public void before() {
29 //? ? ? ? ac = new ClassPathXmlApplicationContext("applicationContext.xml");
30 //? ? ? ? userService = (IUserService) ac.getBean("userService");
31 //? ? }
32?
33? ? @Test
34? ? public void test1() {
35? ? ? ? User user = userService.getUserById(1);
36? ? ? ? // System.out.println(user.getUserName());
37? ? ? ? // logger.info("值:"+user.getUserName());
38? ? ? ? logger.info(JSON.toJSONString(user));
39? ? }
40 }


測試結(jié)果:
至此,完成Spring和mybatis這兩大框架的整合,下面在繼續(xù)進行SpringMVC的整合。
4.3、整合SpringMVC
上面已經(jīng)完成了2大框架的整合,SpringMVC的配置文件單獨放,然后在web.xml中配置整合。
4.3.1、配置spring-mvc.xml
配置里面的注釋也很詳細,在此就不說了,主要是自動掃描控制器,視圖模式,注解的啟動這三個。


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" xmlns:p="http://www.springframework.org/schema/p"
4? ? xmlns:context="http://www.springframework.org/schema/context"
5? ? xmlns:mvc="http://www.springframework.org/schema/mvc"
6? ? xsi:schemaLocation="http://www.springframework.org/schema/beans?
7? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/beans/spring-beans-3.1.xsd?
8? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/context?
9? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/context/spring-context-3.1.xsd?
10? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/mvc?
11? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12? ? <!-- 自動掃描該包,使SpringMVC認為包下用了@controller注解的類是控制器 -->
13? ? <context:component-scan base-package="com.cn.hnust.controller" />
14? ? <!--避免IE執(zhí)行AJAX時,返回JSON出現(xiàn)下載文件 -->
15? ? <bean id="mappingJacksonHttpMessageConverter"
16? ? ? ? class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
17? ? ? ? <property name="supportedMediaTypes">
18? ? ? ? ? ? <list>
19? ? ? ? ? ? ? ? <value>text/html;charset=UTF-8</value>
20? ? ? ? ? ? </list>
21? ? ? ? </property>
22? ? </bean>
23? ? <!-- 啟動SpringMVC的注解功能,完成請求和注解POJO的映射 -->
24? ? <bean
25? ? ? ? class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
26? ? ? ? <property name="messageConverters">
27? ? ? ? ? ? <list>
28? ? ? ? ? ? ? ? <ref bean="mappingJacksonHttpMessageConverter" />? ? <!-- JSON轉(zhuǎn)換器 -->
29? ? ? ? ? ? </list>
30? ? ? ? </property>
31? ? </bean>
32? ? <!-- 定義跳轉(zhuǎn)的文件的前后綴 ,視圖模式配置-->
33? ? <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
34? ? ? ? <!-- 這里的配置我的理解是自動給后面action的方法return的字符串加上前綴和后綴,變成一個 可用的url地址 -->
35? ? ? ? <property name="prefix" value="/WEB-INF/jsp/" />
36? ? ? ? <property name="suffix" value=".jsp" />
37? ? </bean>
38? ?
39? ? <!-- 配置文件上傳,如果沒有使用文件上傳可以不用配置,當然如果不配,那么配置文件中也不必引入上傳組件包 -->
40? ? <bean id="multipartResolver"?
41? ? ? ? class="org.springframework.web.multipart.commons.CommonsMultipartResolver">?
42? ? ? ? <!-- 默認編碼 -->
43? ? ? ? <property name="defaultEncoding" value="utf-8" />?
44? ? ? ? <!-- 文件大小最大值 -->
45? ? ? ? <property name="maxUploadSize" value="10485760000" />?
46? ? ? ? <!-- 內(nèi)存中的最大值 -->
47? ? ? ? <property name="maxInMemorySize" value="40960" />?
48? ? </bean>
49?
50 </beans>


4.3.2、配置web.xml文件
這里面對spring-mybatis.xml的引入以及配置的spring-mvc的Servlet就是為了完成SSM整合,之前2框架整合不需要在此處進行任何配置。配置一樣有詳細注釋,不多解釋了。
web.xml


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? ? version="3.0">
6? ? <display-name>Archetype Created Web Application</display-name>
7? ? <!-- Spring和mybatis的配置文件 -->
8? ? <context-param>
9? ? ? ? <param-name>contextConfigLocation</param-name>
10? ? ? ? <param-value>classpath:spring-mybatis.xml</param-value>
11? ? </context-param>
12? ? <!-- 編碼過濾器 -->
13? ? <filter>
14? ? ? ? <filter-name>encodingFilter</filter-name>
15? ? ? ? <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
16? ? ? ? <async-supported>true</async-supported>
17? ? ? ? <init-param>
18? ? ? ? ? ? <param-name>encoding</param-name>
19? ? ? ? ? ? <param-value>UTF-8</param-value>
20? ? ? ? </init-param>
21? ? </filter>
22? ? <filter-mapping>
23? ? ? ? <filter-name>encodingFilter</filter-name>
24? ? ? ? <url-pattern>/*</url-pattern>
25? ? </filter-mapping>
26? ? <!-- Spring監(jiān)聽器 -->
27? ? <listener>
28? ? ? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
29? ? </listener>
30? ? <!-- 防止Spring內(nèi)存溢出監(jiān)聽器 -->
31? ? <listener>
32? ? ? ? <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
33? ? </listener>
34?
35? ? <!-- Spring MVC servlet -->
36? ? <servlet>
37? ? ? ? <servlet-name>SpringMVC</servlet-name>
38? ? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
39? ? ? ? <init-param>
40? ? ? ? ? ? <param-name>contextConfigLocation</param-name>
41? ? ? ? ? ? <param-value>classpath:spring-mvc.xml</param-value>
42? ? ? ? </init-param>
43? ? ? ? <load-on-startup>1</load-on-startup>
44? ? ? ? <async-supported>true</async-supported>
45? ? </servlet>
46? ? <servlet-mapping>
47? ? ? ? <servlet-name>SpringMVC</servlet-name>
48? ? ? ? <!-- 此處可以可以配置成*.do,對應(yīng)struts的后綴習(xí)慣 -->
49? ? ? ? <url-pattern>/</url-pattern>
50? ? </servlet-mapping>
51? ? <welcome-file-list>
52? ? ? ? <welcome-file>/index.jsp</welcome-file>
53? ? </welcome-file-list>
54?
55 </web-app>


4.3.3、測試
至此已經(jīng)完成了SSM三大框架的整合了,接下來測試一下,如果成功了,那么恭喜你,如果失敗了,繼續(xù)調(diào)試吧,作為程序員就是不停的與BUG做斗爭!
4.3.3.1、新建jsp頁面
showUser.jsp?? 此頁面僅輸出一下用戶名,完成一個完整的簡單流程。


1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3 <html>
4? <head>
5? ? <title>測試</title>
6? </head>
7?
8? <body>
9? ? ${user.userName}
10? </body>
11 </html>


4.3.3.2、建立UserController類
UserController.java??控制器


1 package com.cn.hnust.controller;
2?
3 import javax.annotation.Resource;
4 import javax.servlet.http.HttpServletRequest;
5?
6 import org.springframework.stereotype.Controller;
7 import org.springframework.ui.Model;
8 import org.springframework.web.bind.annotation.RequestMapping;
9?
10 import com.cn.hnust.pojo.User;
11 import com.cn.hnust.service.IUserService;
12?
13 @Controller
14 @RequestMapping("/user")
15 public class UserController {
16? ? @Resource
17? ? private IUserService userService;
18? ?
19? ? @RequestMapping("/showUser")
20? ? public String toIndex(HttpServletRequest request,Model model){
21? ? ? ? int userId = Integer.parseInt(request.getParameter("id"));
22? ? ? ? User user = this.userService.getUserById(userId);
23? ? ? ? model.addAttribute("user", user);
24? ? ? ? return "showUser";
25? ? }
26 }


4.3.3.3、部署項目
輸入地址:localhost:8080/項目名稱/user/showUser?id=1
至此,SSM三大框架的整合就完成了,在此基礎(chǔ)上可再添加其他功能。