简述
SSM框架的集成主要是Spring和MyBatis的集成,SpringMVC和Spring天然集成。
1.在IDEA新建一个Maven项目
图1. 创建web-app
1.2 创建确实的文件夹,结构如下
图2.文件夹结构
2.添加相关依赖
2.1 配置pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!--自定义一个版本变量,方便更新版本-->
<spring.version>5.1.5.RELEASE</spring.version>
<mybatis.version>3.4.6</mybatis.version>
<c3p0.version>0.9.5.4</c3p0.version>
</properties>
<dependencies>
<!-- 默认的idea创建的依赖版本是4.11,后续集成测试时,提示Junit的版本过低,至少需要4.12 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--Spring核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</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-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-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring的依赖添加结束,如有其他需求请自行添加-->
<!--MyBatis核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--MyBatis依赖包到此结束-->
<!--Spring-Mybatis集成的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--Spring集成Mybatis的依赖结束-->
<!--用于连接数据库的依赖,这里来连接MySQL,其他数据库自行更换包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!--数据连接池选择c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<!--数据库相关到此结束-->
<!--JavaWeb需要的-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--简化代码的依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
<!--Spring的测试包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
图3.pom.xml
2.2 创建数据库
CREATE DATABASE `test`;
USE `test`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
图4.test数据库的创建sql
3.Spring与Mybatis集成
3.1 在resources/spring下创建一个applicationContext.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: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">
<!--配置自动扫描-->
<!--
context:component-scan 会自动扫描当前包及其子包,
并且每一个context:componment-scan都会生成一个bean,
当在后续的SpringMVC中需要配置控制器,所以不排除Controller和RestController注解,
就会扫描到两个同类型的bean,导致Spring不知道要装配那个bean。
-->
<context:component-scan base-package="cn.edu.nit">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
</context:component-scan>
<!--用于管理整个Spring上下文-->
<import resource="classpath:spring/spring-*.xml"/>
</beans>
图5. applicationContext.xml
3.2 在resources下创建一个jdbc.properties并配置如下(user和password根据自己数据库而定)
jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=GMT%2B8
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.user=developer
jdbc.password=123456
图6.jdbc.properties
3.3 在resources/spring下创建一个spring-mybatis.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: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">
<!--配置数据源-->
<!--1. 先加载数据库的配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--2.配置DataSource,采用c3p0的-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="driverClass" value="${jdbc.driver}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置Spring事务用于管理Mybatis的DataSource-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置MyBatis-->
<!--1. 自动扫描mapper.xml文件和会话工厂,配置mapper是操作那个数据源-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--注意:这里是Mapperlocations,不是configLocation-->
<property name="mapperLocations" value="classpath:cn/edu/nit/mapper/*.xml"/>
</bean>
<!--2.自动扫描Mapper层的接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="cn.edu.nit.mapper"/>
</bean>
</beans>
图7.spring-mybatis.xml
3.4 测试一下Spring和MyBatis的集成环境
package cn.edu.nit.test;
import cn.edu.nit.pojo.User;
import cn.edu.nit.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringMyBatisTest {
@Autowired
private UserService userService;
@Test
public void t1(){
User user = new User();
user.setName("developer");
user.setPassword("123456");
userService.insertUser(user);
}
}
图8.测试Spring和MyBatis的集成类
- 文档结构均出现变化,测试结果如图9。
图9.SpringMybatis集成测试结果图
4.配置SpringMVC和Tomcat
4.1配置SpringMVC
- 在resources文件夹下创建springmvc.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: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">
<!--1.扫描控制层的控制器-->
<context:component-scan base-package="cn.edu.nit">
<!--include-filter是只扫描有这个注解的-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
</context:component-scan>
<!--2.配置内部视图解析器-->
<!--转发的时候会经过,重定向不会-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
图10. springmvc.xml
- 向resources/spring/applicationContext.xml添加一下配置
<import resource="classpath:springmvc.xml"/>
图11.向resources/spring/applicationContext.xml添加的代码
- 在WEB-INF/web.xml中配置springmvc
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<!--配置上下文配置文件地址-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!--配置Spring上下文加载监听器,-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置springmvc拦截器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--以下的是一些优化的不是必要的的,视情况而定-->
<!--编码过滤器-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置Spring内存溢出监听器-->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
图12.WEB-INF/web.xml
4.2.配置运行时环境(容器)
图13.配置Tomcat
5.测试运行结果
5.1测试结果
图14./user/query viewName = "show" 的结果
图15./user/query viewName="redirect:/jsp/show.jsp"的结果
注:图14中是请求转发,所以浏览器的地址栏是 http://localhost:8080/user/query ,而图15中是重定向,地址栏是http://localhost:8080/jsp/show.jsp,并且数据在第二次发起请求时丢失了。
6.相关 Exception & Error 提示
- 6.1 . org.springframework.beans.factory.BeanCreationException:
提示 1: ,每个component-scan会扫描一遍Spring上下文,并创建一个Bean,如果有多个component-scan扫描,那么这些component-scan扫描到的交集 中就会出现多个相同类型的Bean,最后导致Spring不知道怎么注入,故抛出异常。
提示 2 : web.xml 中 applicationContext.xml没有扫描到所有的注解,最后也会抛出BeanCreationException,springmvc.xml中配置@Controller和@RestController才会生效。也可以全部配置在一个spring文件中,只要能解决问题就行,爱怎么配就怎么配。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
- 6.2 jstl相关 打不出来的问题
提示:检查一下uri <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
7.完整代码
8.参考文档及其他
9.其他
- 转载在参考文档中表明即可。
- 相关问题可以在源码地址中反应。
原文地址:https://www.cnblogs.com/liguiyangcn/p/11518255.html