文章目录
1、环境的搭建
整合说明:
- SSM整合可以使用多种方式,咱们会选择XML + 注解的方式
- 使用Spring去整合Mybatis和SpringMVC
整合的思路
- 先搭建整合的环境
- 先把Spring的配置搭建完成
- 再使用Spring整合SpringMVC框架
- 最后使用Spring整合MyBatis框架
创建数据库和表结构
-
create database ssm; use ssm; create table account( id int primary key auto_increment, name varchar(20), money double );
创建maven的工程(今天会使用到工程的聚合和拆分的概念,这个技术maven高级会讲)
- 创建ssm_parent父工程(打包方式选择pom,必须的)
- 创建ssm_web子模块(打包方式是war包)
- 创建ssm_service子模块(打包方式是jar包)
- 创建ssm_dao子模块(打包方式是jar包)
- 创建ssm_domain子模块(打包方式是jar包)
- web依赖于service,service依赖于dao,dao依赖于domain
- 在ssm_parent的pom.xml文件中引入坐标依赖
- (包括spring整合MyBatis的jar包等等)
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!--IOC容器-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--springmvc-->
<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-test</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>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!--el表达式和JSTL-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</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 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--spring整合mybatis的包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>ssm</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
2、搭建Spring框架
-
在扫描包的时候,要扫描的是service和dao层的注解,要忽略web层注解,因为web层让SpringMVC框架去管理
-
怎么配置哪些注解不扫描??
-
<!-- 在<context:component-scan base-package="要扫描的包">中配置 --> <!-- 要扫描的注解 --> <context:include-filter type="注解" expression="注解的全路径"/> <!-- 不扫描的注解 --> <context:exclude-filter type="注解" expression="注解的全路径"/>
-
-
<?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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启注解扫描,要扫描的是service和dao层的注解,要忽略web层注解,因为web层让SpringMVC框架去管理 --> <context:component-scan base-package="com.zy"> <!-- 配置要忽略的注解,应为SpringMVC是用的@Conterller注解 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
-
注意,每配置完一个,要进行测试
3、搭建SpringMVC框架
-
在web.xml文件中配置 前端控制器 和 中文乱码过滤器
-
配置spring.xml文件:
-
开启注解扫描,只扫描@Controller注解
-
配置视图解析器
-
配置要用到的小组件
-
开启SpringMVC注解的扫描
-
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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 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"> <!-- 扫描controller的注解,别的不扫描 --> <context:component-scan base-package="com.zy"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- JSP文件所在的目录 --> <property name="prefix" value="/WEB-INF/pages/" /> <!-- 文件的后缀名 --> <property name="suffix" value=".jsp" /> </bean> <!-- 设置静态资源不过滤 --> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <!-- 开启对SpringMVC注解的支持 --> <mvc:annotation-driven /> </beans>
4、Spring整合SpringMVC框架
-
-
要实现在Coltroller控制器中,可以去调用Service中的方法
-
在控制器中通过依赖注入,把Service中对象传过来
-
在启动项目的时候,会加载web.xml然后,会加载springmvc.xml文件,但是没有加载spring.xml(applicationContext.xml)文件
-
怎么在启动服务的时候,加载springmvc.xml的同时,加载spring.xml??
-
只要加载了spring.xml,就有了service的Bean对象,就能完成对控制器的依赖注入
-
ServletContext对象在服务器启动的时候,被创建出来,服务器关闭的时候被销毁
-
我们可以通过
监听ServletContext对象的创建,去加载spring.xml文件
-
这个监听器Spring已经帮我们创建好了(在spring-web的jar包中)
-
在web.xml配置spring监听器,
-
<!--配置Spring的监听器,默认只加载WEB-INF下的applicationContext.xml配置文件,别的文件加载不到--> <!-- 2种解决办法: 法一: 把sprig.xml命名为applicationContext.xml放到WEB-INF下 法二: 设置配置文件的路径 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--设置配置文件的路径--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param>
-
-
通过springmvc.xml文件的加载,控制器的对象加入了容器
-
在加载spring.xml把service放入容器,可以对控制器进行注入service层的对象
-
5、搭建MyBatis框架
-
创建好注解,或者配置好Mybatis的配置文件就好
-
<?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> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis_damo1"/> <property name="username" value="root"/> <property name="password" value="015718"/> </dataSource> </environment> </environments> <!-- 使用的是注解 --> <mappers> <!-- <mapper class="cn.itcast.dao.AccountDao"/> --> <!-- 该包下所有的dao接口都可以使用 --> <package name="cn.itcast.dao"/> </mappers> </configuration>
6、Spring整合MyBatis框架
- 把Mybatis的一些类放入到Spring的容器中,Service层可以进行依赖注入Dao层的对象:
- 把需要用到的对象生成出来,放到容器中就好
- 常见的Mybatis放到容器中的对象:
- 连接池的连接源对象
- SqlSessiomFacatory工厂对象
- SqlSessionFactoryBuilder对象
- SqlSession对象
- 接口的实现类对象