因为刚学完SSM,有点生疏,想要通过写教程来加强理解,也方便以后复习,该教程如果有错,请指正
SSM是什么?
在整合之前我们先了解一下SSM是什么,能干什么。
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的web项目的框架。
- Spring:就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地
new
一个对象,而是让Spring框架帮你来完成这一切。 - SpringMVC:在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
- mybatis:是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
- Java Web程序分三层架构,分别是视图层、业务层、持久层,SSM中的Spring对应着业务层,SpringMVC对应视图层,Mybatis对应持久层。
以上是百度百科对SSM的介绍,小白刚学的时候看得是一头雾水,但是随着后面的深入学习之后有了自己的一点点理解。
- Spring:在学习Spring之前,service层需要对数据库操作的事务管理,原始版本需要对service层中的方法进行对事务的提交、回滚、异常操作,需要用到动态代理或其他方法,工作量大、可维护性较差,而Spring的AOP就解决了这些问题,让小白可以快速完成service层的开发,还可以把service层中可以使用单例设计模式的对象交给Spring来管理(IOC)。
- SpringMVC:springmvc相当于servlet,但是springmvc只被一个servlet管理,可以把对服务器的访问请求给拦截下来,再到controller层中相应的方法(每个方法都绑定着一个地址,请求url=绑定的地址就会调用),springmvc只管接收请求和响应请求,其他事它可不管。
- Mybatis:与Hibernate一样都是持久层的框架,但是Mybatis的优势在于可以自由编写sql,在想要通过sql优化数据库运行效率的时候,Mybatis可以让擅长数据库的大牛玩出花来。还有就是用Mybatis使用xml来管理的,所以只需要把大部分精力放在xml文件上,不需要花太多的精力在其他方面上。
SSM整合准备步骤
整合环境
spring5.2.2 + mybatis 3.5.2
准备jar包
不需要积分的SSM整合包下载链接(已包含测试需要的jar包):SSM整合包
工程目录
包名 | 作用 |
---|---|
com.ssm.controller | 对应着原始web写法中的servlet,也是springmvc发挥作用的包 |
com.ssm.service | 业务层,存放着业务逻辑的类 |
com.ssm.dao | 持久层,存放着mapper接口(用于使用mapper代理)和对应的xml文件 |
com.ssm.entity | 存放实体类的包 |
conf | 存放配置文件的文件夹 |
准备配置文件
mybatis-config.mxl
<?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>
</configuration>
applicationContext.mxl
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.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
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<!-- 配置spring -->
<!-- 扫描service -->
<context:component-scan base-package="com.ssm">
<!-- 过滤掉springmvc的注解,否则会对controller包中的类加载两次 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 创建事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 设置传播行为 -->
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution (* com.ssm.service.*.*(..))"/>
</aop:config>
<!-- spring整合mybatis -->
<!-- 引入properties文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000" />
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2" />
</bean>
<!-- 配置SqlSessionFactory工厂对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入连接池 -->
<prope