SSM(Spring+SpringMVC+Mybatis)框架集由Spring、SpringMVC、Mybatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。
1.三大框架概述 Spring作为一个轻量级的框架,有很多的拓展功能,最主要的我们一般项目使用的就是IOC和AOP。
SpringMVC是Spring实现的一个Web层,属于SpringFrameWork的后续产品。Spring MVC分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
Mybatis是一个持久层的框架,使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
2.SSM框架集搭建过程一.使用Maven管理项目
【new】→【project】→【maven project】→【webapp】
创建一个名为SSM的maven项目,在工程目录下增加【doc】文件夹,在【src/main】目录下增加【java】文件夹。
二.配置spring
1.添加jar包,配置pom.xml
5.0.2.RELEASE
junit
junit
4.12
test
javax.servlet
javax.servlet-api
3.1.0
commons-logging
commons-logging
1.2
com.alibaba
druid
1.1.6
mysql
mysql-connector-java
5.1.38
org.springframework
spring-context
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
4000
ctId>
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-webmvc
${spring.version}
2.配置web.xml
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:config/spring/applicationContext.xml
3.配置springIOC容器bean的扫描及数据源的基本配置
(1)applicationContext.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
(2)applicationContext-db.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
三.配置springMVC
1.在web.xml配置前端控制器
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:config/spring/applicationContext-mvc.xml
DispatcherServlet
/
2.配置springmvc配置文件applicationContext-mvc.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
四.配置mybatis
1.添加jar包,配置pom.xml
org.mybatis
mybatis
3.4.1
org.mybatis
mybatis-spring
1.3.0
2.配置SqlMapConfig.xml
/p>
PUBLIC "-//mybati
bd49
s.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
注意:由于mybaits的二级缓存是mapper范围级别,所以除了在SqlMapConfig.xml设置二级缓存的总开关外,还要在具体的mapper.xml中开启二级缓存。
3.在applicationContext-mvc.xml文件中,载入SqlMapConfig.xml
附.ioc相关配置
以向service层注入dao为例:
1.配置applicationContext-ioc.xml文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
2.在applicationContext.xml文件中载入applicationContext-ioc.xml文件
注意:如果使用ioc手动装配,需要注意将手动装配的javabean文件中相应的注解删除,否则系统会重复创建javabean进而导致BeanCreationException。
五.测试所搭建SSM框架集是否正常
可使用Junit注解完成测试,也可以按完成一个简单的前后台交互过程来完成。
这里使用完成一次简单的前后台信息交互。
测试正常,则SSM框架搭建完毕。
附:博主自己已搭建成的SSM框架集
https://github.com/yueqi1125/SSMFramesets