SSM框架整合
一、创建 Maven 工程
在 pom.xml 中导入依赖
4.0.0 com.chan ssm 1.0-SNAPSHOT war ssm Maven Webapp http://www.example.com UTF-8 1.8 1.8 5.0.2.RELEASE 1.6.6 1.2.17 5.1.6 3.4.5 org.aspectj aspectjweaver 1.6.8 org.springframework spring-aop ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-test ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-jdbc ${spring.version} junit junit 4.12 compile mysql mysql-connector-java ${mysql.version} javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.0 provided jstl jstl 1.2 log4j log4j ${log4j.version} org.slf4j slf4j-api ${slf4j.version} org.slf4j slf4j-log4j12 ${slf4j.version} org.mybatis mybatis ${mybatis.version} org.mybatis mybatis-spring 1.3.0 c3p0 c3p0 0.9.1.2 jar compile 构建包结构
二、Spring 和Spring MVC的整合
2.1 Spring 的配置
spring 的配置文件主要管理项目中 Bean 对象的创建,可以使用 JavaConfig 配置类。加载时,需要使用 ClasssPathXmlApplicationContext
或 AnnotationConfigApplication
对象加载;
Spring 配置文件可以存在多个。
2.2 Spring MVC 配置
在 web.xml 文件中,配置 DispatcherServlet
,并在启动时加载 Spring MVC配置文件;
- 配置
DispatcherServlet
; - 加载Spring MVC配置文件;
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMvc.xml
1
dispatcherServlet
/
Spring MVC配置
- 开启 Spring MVC 注解扫描;
- 配置视图解析器;
- 忽略拦截静态资源;
- 开启MVC注解支持
2.3 Spring 和 Spring MVC 整合
服务器启动时,使用DispatcherServlet
的内置参数contextConfigLocation
加载Spring MVC配置文件;但此时,并没有加载Spring配置文件,无法使用Spring容器的Bean。使用监听器ContextLoaderListener
,当服务器启动,ServletContext创建后,加载Spring配置文件;该监听器会创建 WEB应用上下文,存储到ServletContext对象。
使用方式:
- 创建
ContextLoaderListener
监听器;但是,这个监听器只会加载WEB-INF目录下名称为 applicationContext.xml 的配置文件; - 使用web.xml中
元素的contextConfigLocation
属性指定spring配置文件;
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
2.4 解决乱码问题
浏览器和服务器之间交互会因为编码格式不同而发生乱码,Spring 提供了过滤器来解决乱码问题,我们只需配置即可。
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
Spring和Spring MVC的整合就已经完成,可以编写例子进行测试。
三 Spring和MyBatis的整合
3.1 单一MyBatis框架配置
MyBatis的搭建通过配置文件,配置数据源、映射文件扫描。
一个简单的MyBatis配置
读取配置文件,获取SqlSession对象,执行数据库操作
package com.chan.mapper;
import com.chan.pojo.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestIAccountDao {
@Test
public void test() throws IOException {
//加载MyBatis配置文件
InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
//获取SqlSession对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = factory.openSession();
//获取IAccountDao对象
IAccountDao accountDao = sqlSession.getMapper(IAccountDao.class);
//执行操作
List accounts = accountDao.findAll();
//遍历
accounts.forEach(System.out::println);
//关闭资源
sqlSession.close();
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.2 Spring整合MyBatis
在Spring中,使用MyBatis框架可以不需要使用配置文件的形式,而在Spring配置文件中装配相关Bean对象,具体需要装配的Bean如下
这是Spring整合MyBatis的简单实现,没有进行配置事务管理,这种情况下,如果进行增删改的操作,会以非事务进行处理,会自动提交数据。
使用Spring配置事务管理
至此,SSM框架简单的整合就已经结束了。另外,会在另一篇文章中补充整合中遇到的问题。