已經有一年沒整合過SSM框架了,以前都是保存着一大堆jar包再搞整合的,現在感覺jar包都老了所以想整合個跟得上時代的SSM,想了想找jar包有點煩就整了個maven項目(方便以後更改,管理jar包)
開始搭建SSM項目
###1、Maven Web項目創建
如果需要,請參考博文(我剛開始也是也是看這個創建的):http://blog.csdn.net/zhshulin/article/details/37921705
###2、SSM整合
SSM整合框架的思路:首先,是數據庫跟spring整合(dao層);
其次,是我們的service層的整合;
最後是表現層的整合。
在整合之前我們先把項目結構創建好以及把pom.xml包的依賴填寫好,
項目結構如圖:
附上pom.xml文件內容如下:
pom.xml
<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> <groupId>com.elan</groupId> <artifactId>ssm</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ssm Maven Webapp</name> <url>http://maven.apache.org</url>
<properties> <!-- spring版本號 --> <spring.version>4.3.10.RELEASE</spring.version> <!-- mybatis版本號 --> <mybatis.version>3.4.5</mybatis.version> <mybatis-connector-java.version>8.0.7-dmr</mybatis-connector-java.version> <mybatis-spring.version>1.3.1</mybatis-spring.version> <javaee-api.version>7.0</javaee-api.version> <druid.version>1.1.2</druid.version> <jstl.version>1.2</jstl.version> <aspectjweaver.version>1.5.4</aspectjweaver.version> <fastjson.version>1.2.37</fastjson.version> <jackson-mapper-asl.version>1.9.13</jackson-mapper-asl.version> <commons-fileupload.version>1.3.3</commons-fileupload.version> <commons-io.version>2.5</commons-io.version> <commons-codec.version>1.10</commons-codec.version> <!-- log4j日誌文件管理包版本 --> <slf4j.version>1.8.0-alpha2</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <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-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</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-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- spring核心包 --> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mybatis-connector-java.version}</version> </dependency> <!-- mybatis核心包 --> <!-- mybatis與spring整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <!-- mybatis與spring整合包 --> <!-- 導入java ee jar 包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>${javaee-api.version}</version> </dependency> <!-- 數據庫連接池用阿里的druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- 數據庫連接池用阿里的druid --> <!-- JSTL標籤類 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- spring-aop實現動態代理所依賴的包 --> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectjweaver.version}</version> </dependency> <!-- json轉換包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <!-- 日誌文件管理包 --> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- 格式化對象,方便輸出日誌 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <!-- 映入JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>${jackson-mapper-asl.version}</version> </dependency> <!-- 上傳組件包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>${commons-codec.version}</version> </dependency> </dependencies> <build> <finalName>ssm</finalName> </build>
</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
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
####1.Dao層:使用mybatis框架。
在src/main/resources下面的spring文件夾下創建一個applicationContext-dao.xml文件,文件裏面需要配置的主要有3個即
a. 配置數據源
b. 需要讓spring容器管理sqlsessionFactory,單例存在
c. 把mapper的代理對象放到spring容器中。使用掃描包的方式來加載mapper的代理對象。
以及在src/main/resources下的resource文件夾下創建一個db.properties文件。
附上db.properties內容如下:
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?serverTimezone=UTC
jdbc.username=root
jdbc.password=admin
- 1
- 2
- 3
- 4
附上applicationContext-dao.xml內容如下:
applicationContext-dao.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 數據庫連接池 --> <!-- 加載配置文件 --> <context:property-placeholder location="classpath:resource/*.properties"/> <!-- 數據庫連接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /> </bean> <!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 數據庫連接池 --> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/elan/mapping/*.xml"></property> <!-- 加載mybatis的全局配置文件 --> <!-- <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> --> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.elan.dao" /> <!--sqlSessionFactory這裏配不配都行,不配的話他會自動找 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </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
####2.Service層
在src/main/resources下面的spring文件夾下創建一個applicationContext-service.xml文件和一個applicationContext-tx.xml文件。
a. applicationContext-tx.xml用來配置事務管理
b. applicationContext-service.xml用來配置service實現類對象放到spring容器中管理。
附上applicationContext-service.xml內容:
applicationContext-service.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 掃描包加載Service實現類 --> <context:component-scan base-package="com.elan.service"></context:component-scan>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
附上applicationContext-tx.xml的內容:
applicationContext-tx.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 數據源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 傳播行爲 ,REQUIRED:如果當前有事務就在當前事務執行,如果當前沒有事務就開啓一個新的事務來執行 SUPPORES:如果當前有事務就在當前事務執行,如果當前沒有事務就不開啓事務 --> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- 切面 --> <aop:config> <!--1.*:任何返回值2.com.elan.service:在service包下3.*包含所有子包,4.*:任何方法名5.(..):任意參數 --> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.elan.service.*.*(..))"></aop:advisor> </aop:config>
</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
到了這裏已經完成了Spring和mybatis的整合***
####3.測試spring+mybatis
現在就讓我們來測試下吧
首先,在src/main/resources*目錄下建立一個log4j.properties文件(用來打印數據,驗證我們整合成功),其內容如下
log4.properties
#定義LOG輸出級別
log4j.rootLogger=INFO,Console,File
#定義日誌輸出目的地爲控制檯
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以靈活地指定日誌輸出格式,下面一行是指定具體的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#mybatis\u663E\u793ASQL\u8BED\u53E5\u65E5\u5FD7\u914D\u7F6E
#log4j.logger.org.mybatis=DEBUG
log4j.logger.net.cxp.blog.dao=DEBUG
#文件大小到達指定尺寸的時候產生一個新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定輸出目錄
log4j.appender.File.File = logs/ssm.log
#定義文件最大大小
log4j.appender.File.MaxFileSize = 10MB
輸出所以日誌,如果換成DEBUG表示輸出DEBUG以上級別日誌
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c]%m%n
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
然後,在數據庫建立一個user_t表建表語句如下:
SET FOREIGN_KEY_CHECKS=0;
– Table structure for user_t
DROP TABLE IF EXISTS user_t
;
CREATE TABLE user_t
(
id
bigint(11) unsigned NOT NULL AUTO_INCREMENT,
user_name
varchar(40) NOT NULL,
password
varchar(255) NOT NULL,
age
int(4) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=150407717146215 DEFAULT CHARSET=utf8;
– Records of user_t
INSERT INTO user_t
VALUES (‘1’, ‘elan1’, ‘7854645’, ‘12’);
INSERT INTO user_t
VALUES (‘150407458366574’, ‘212’, ‘’, ‘321’);
INSERT INTO user_t
VALUES (‘150407484434940’, ‘32’, ‘’, ‘321’);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
最後,利用MyBatis Generator自動創建代碼
參考博文:http://blog.csdn.net/zhshulin/article/details/23912615
這個可根據表自動創建實體類、MyBatis映射文件以及DAO接口,當然,我習慣將生成的接口名改爲IUserDao,而不是直接用它生成的UserMapper。如果不想麻煩就可以不改。完成後將文件複製到工程中。如圖:
- 1
建立Service接口和實現類
目錄結構:
這裏附上IUserService.java的代碼:
IUserService.java
Ipackage com.elan.service;
import com.elan.pojo.User;
public interface IUserService {
public User getById(long userId);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
UserServiceImpl.java的代碼
UserServiceImpl.java
package com.elan.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elan.dao.IUserDao;
import com.elan.pojo.User;
import com.elan.service.IUserService;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
/*獲取單個用戶*/
public User getById(long userId) {
return this.userDao.selectByPrimaryKey(userId);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
建立測試類
測試類在src/test/java中的com.elan.test包下建立,這裏命名爲MybatisAndSpringTest附上代碼如下:
MybatisAndSpringTest.java
package com.elan.test;
import org.apache.log4j.Logger;
import org.junit.Test;
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;
import com.alibaba.fastjson.JSON;
import com.elan.pojo.User;
import com.elan.service.IUserService;
@RunWith(SpringJUnit4ClassRunner.class) //表示繼承了SpringJUnit4ClassRunner類
@ContextConfiguration(locations = {“classpath:spring/applicationContext-*.xml”})//讀取所有spring配置文件
public class MybatisAndSpringTest {
private static Logger logger = Logger.getLogger(MybatisAndSpringTest.class);
@Autowired
private IUserService userService = null;
@Test
public void test1() {
User user = userService.getById(1);
logger.info(JSON.toJSONString(user));
}
}
- 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
如果測試成功,表示Spring和Mybatis已經整合成功了。輸出信息使用的是Log4j打印到控制檯。如圖:
至此,完成Spring和mybatis這兩大框架的整合,下面在繼續進行表現層的整合(spring+springMvc)。
####4.表現層(springMVC)的整合
在src/main/resources下面的spring文件夾下創建一個spring-mvc.xml文件,文件裏面需要配置的主要有3個即
a. 配置註解驅動
b. 配置視圖解析器
c. 掃描controller
以及更改src/main/webapp/WEB-INF目錄下的web.xm文件,上面也需要配置3個東西即
a.spring容器的配置
b.Springmvc前端控制器的配置
c.Post亂碼的配置
附上spring-mvc.xml內容如下:
spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置掃描包 -->
<context:component-scan base-package="com.elan.controller" />
<!--配置註解驅動 -->
<mvc:annotation-driven />
<!--配置視圖解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!--資源映射(由於web.xml配置了WEB-INF下的所有請求都被攔截,這裏必須對靜態資源進行配置這樣就可以直接訪問靜態資源) -->
<mvc:resources location="/WEB-INF/css" mapping="/css/**"/>
<mvc:resources location="/WEB-INF/js" mapping="/js/**"/>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
附上web.xml內容如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!-- 加載spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!-- 解決post亂碼 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- springmvc的前端控制器 --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation不是必須的, 如果不配置contextConfigLocation, springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
</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
到了這裏已經***完成了Spring和mybatis+SpringMVC的整合***
####5.測試spring+mybatis+SpringMVC
a.在WEB-INF下新建一個jsp文件夾在裏面創建getById.jsp頁面
附上getById.jsp的代碼
getById.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ssm測試</title>
</head>
<body>
用戶名: {user.age}
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
b.在com.elan.controller包裏建立UserController類
附上UserController.java 代碼
UserController.java
package com.elan.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.elan.pojo.User;
import com.elan.service.IUserService;
@Controller
@RequestMapping("/user")
public class UserController {
private static final Log logger = LogFactory.getLog(UserController.class);
@Resource
private IUserService userService;
/*根據id獲取單個用戶信息*/
@RequestMapping("/getById")
public String getById(HttpServletRequest request, ModelMap modelMap) {
long userId = Long.parseLong(request.getParameter("id"));
User user = this.userService.getById(userId);
modelMap.addAttribute("user", user);
return "getById";
}
}
- 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
c.部署項目
瀏覽器輸入地址:http://localhost/項目名稱/user/getById?id=1
我這裏項目名字是springMybatis所以我輸入的是:http://localhost/springMybatis/user/getById?id=1
出現結果:
至此,恭喜SSM三大框架的整合完成了,你可以繼續添加其他功能。
注:如果此處xml報錯對工程不影響:
使用Eclipse開發工具,XML文件報錯的,(如果是複製上面貼的代碼)基本都是本地Eclipse顯示有問題,原因是Eclipse中對項目工程的校驗,點工程右鍵>Properties>Validation ,然後將右側列表中的勾選框全都不勾選,然後clean項目,就可以了.
當然可以也從我的源碼裏面複製出來,這樣就不會報錯了。
源碼下載地址:http://download.csdn.net/download/czc9309/9957727