已经有一年没整合过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>
用户名:
u
s
e
r
.
u
s
e
r
N
a
m
e
用
户
年
龄
:
{user.userName} 用户年龄:
user.userName用户年龄:{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