概述:SSM 是 S(Spring)S(Spring MVC)M(Mybatis)的简称。主要作用是简便我们的Web开发
SSM整合必须要版本兼容,建议低于5版本高于4版本,因为SSM好多的方法在5之后就已经废弃,并且到6就已经被删除了。
SSM整合所需要的 jar 包依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--导入springMVC依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- spring必要依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- spring aop织入依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!-- mybatis相关依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mybatis与spring对接依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
Spring整合Mybatis准备步骤:
1、配置好数据源需要的数据
jdbc.properties文件
jdbc.url=jdbc:mysql:localhost:3306/数据库
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
2、创建 spring-mybatis.xml 和 mybatis-config.xml 文件
mybatis-config.xml(记录日志信息===》方便我们排查错误)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
spring-mybatis.xml(配置会话工厂 和 数据源、事务管理器 以及我们的Mapper执行文件)
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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">
<!--
开启空间包扫描:由于分层开发的原因 mapper service pojo controller 写好了
所以直接扫描我们的service包即可====》细化可以再写一个impl包
-->
<context:component-scan base-package="com.service.impl"/>
<!-- 配置jdbc数据文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--
配置数据源:mybatis没有数据源就无法执行Sql语句
-->
<bean id="myDataScource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--
配置SqlSessionFactory
真正的管理配置开始
第一个参数:配置数据源
第二个参数:配置核心配置文件的位置
-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="myDataScource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<bean>
<!--
配置Mapper扫描器
参数:basePackage 值:mapper存放的位置
-->
<bean class="org.mybatis.spring.mapper.MapperScannerMapping">
<property name="basePackage" value="com.mapper"/>
</bean>
<!--
配置事务管理器
参数:需要被事务管理的数据源
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource" />
</bean>
<!--
开启事务管理器
-->
<tx:annotation-driven transaction-manager="txManager"/>
<beans>
3、创建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:mvc="http://www.springframework.org/schema/mvc"
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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 启动适配器和映射器 -->
<mvc:annotation-driven/>
<!-- 启用静态资源过滤 -->
<mvc:default-servlet-handler/>
<!--
空间扫描我们的controller层的包
一旦写了 @Component @Controller...的注解被纳入Bean容器
-->
<context:component-scan base-package="com.controller"/>
<!-- 配置视图解析器:前缀是我们访问的页面的目录 后缀是我们的文件类型名称 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/目录/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
配置总合的 applicationContext.xml 文件,用于同时启动 service层 和 controller层
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- 最终启动的Bean工厂文件 -->
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>
最后一步! 配置 web.xml 文件
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--
配置servlet的核心文件位置
切记!!! classpath配置我们总和的applicationContext.xml文件
-->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:appliacationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 映射servlet文件 -->
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
总结:我们的tomcat服务器一旦启动,我们的web.xml文件就会去查找我们的ConfigLocation属性的文件,然后我们就能使用ConfigLocation的元素做操作,我们的ConfigLocation属性的值一定是配置我们的总合的 applicationContext.xml 文件的!