SSM整合
ssm整合是对Spring(整体把控) SpringMVC(表现层) Mybatis(持久层)三个框架的整体整合使用
该方案是对SpringMVC和Spring进行分离之后的整体呈现。
整合的基本步骤
1. web工程搭建初始化
创建webapp工程
编写工程信息
POM文件
该文件是整个SSM整合完毕之后的展现
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--mysql环境-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--spring整合jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--spring整合mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!--分页插件坐标-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<!--springmvc环境-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--jackson相关坐标3个-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!--<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency> //由于已经被SJON的Jar包依赖,所以注释掉
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>-->
<!--servlet环境-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<!--构建-->
<build>
<!--设置插件-->
<plugins>
<!--具体的插件配置-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
<!--用于get方式请求的编码-->
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
工程搭建完成后的效果展示
2.制作工程的基本类
基本配置
1)Student对象
package com.itheima.domain;
import org.springframework.stereotype.Component;
@Component
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Student() {
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
2) 通用返回对象
package com.itheima.domain;
import org.springframework.stereotype.Component;
@Component
public class GeneralResult<T> {
private T data;
private Boolean flag;
private String msg;
private Integer code;
public GeneralResult() {
}
public GeneralResult(T data, Boolean flag, String msg, Integer code) {
this.data = data;
this.flag = flag;
this.msg = msg;
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Boolean getFlag() {
return flag;
}
public void setFlag(Boolean flag) {
this.flag = flag;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
@Override
public String toString() {
return "GeneralResult{" +
"data=" + data +
", flag=" + flag +
", msg='" + msg + '\'' +
", code=" + code +
'}';
}
}
3)web.xml编写
- 过滤器
- 中央控制器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:ApplicationContext.xml</param-value> //读取Spring的配置文件
</context-param>
<listener>
//使用监听器分离配置文件
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
//过滤POST请求的字符编码
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
//拦截所有路径的POST请求
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> //定义MVC核心控制器
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:SpringConfig.xml</param-value> //读取SpringMVC的配置文件
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name> //MVC核心控制器,配置拦截所有的路径。不加*
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4)springConfig.xml配置文件
-
包扫描
只扫描Controller层,不然事务会报错。
因为框架会把业务层接口当作一个普通Bean进行解析,但是接口种又定义了事务注解,导致事务的代理类效果不生效而报错。
-
静态资源放行
-
注解支持
-
事务支持
注意:tx的事务配置文件时 ,导入命名空间要以tx结尾!
-
MVC支持
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--该配置文件主要用于MVC-->
<!--开启mvc注解支持-->
<mvc:annotation-driven/>
<!--扫包,将该包下所以含有注解的类或方法都加载-->
<context:component-scan base-package="com.itheima.controller"/>
<!--使用注解,生成事务管理器-->
<bean id="dtManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSources"/>
</bean>
<!--开启事务注解支持-->
<tx:annotation-driven transaction-manager="dtManager"/>
<!--放行静态资源-->
<mvc:default-servlet-handler/>
</beans>
5)ApplicationConfig.xml配置文件
- 扫描整个工程包
- 开启AOP支持
- 整合Mybatis的三个核心类
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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 https://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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--该配置文件主要用于Spring,主容器。扫描整个包-->
<context:component-scan base-package="com.itheima"/>
<!--开启AOP支持-->
<aop:aspectj-autoproxy/>
------------------------------------以下是在对Mybatis进行整合--------------------------------
<!--读入外部配置文件-->
<context:property-placeholder location="classpath*:jdbc.properties"/>
<!--配置与数据库的连接池-->
<bean id="dataSources" 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>
<!--配置生成sqlsession的类-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSources"/>
<property name="typeAliasesPackage" value="com.itheima.domain"/>
<!--配置分页插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!--配置生成实例化的入口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.dao" />
</bean>
</beans>
3.持久层
1) 编写StudentDao接口
package com.itheima.dao;
import com.itheima.domain.GeneralResult;
import com.itheima.domain.Student;
import java.util.List;
public interface StudentDao {
List<Student> findAll(); //查所有
Student findById(Integer id); //查单个
int insertStu(Student student); //添加一个
int updateStu(Integer id); //更新一个
int deleteStu(Integer id); //删除一个
}
2) StudentDao.xml映射文件
- 注意此文件必须在对应的接口权限定报名下
- 在resources下对应Dao接口位置必须同包同名。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:命名空间,指定接口的完全限定名字-->
<mapper namespace="com.itheima.dao.StudentDao">
<!--
id:表示接口中方法名
resultType:表示方法返回的数据类型,如果返回的是集合,这里指定集合中每个元素类型
select标签标签体就是SQL语句
-->
<select id="findAll" resultType="Student">
select * from student
</select>
<select id="findById" resultType="Student">
select * from student where id =#{id}
</select>
<insert id="insertStu" >
insert into Student values (#{id} ,#{name},#{age});
</insert>
<update id="updateStu" >
update student set name=#{name},age=#{age} where id = #{id} ;
</update>
<delete id="deleteStu" >
delete from student where id= #{id};
</delete>
</mapper>
3) jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db10
jdbc.username=root
jdbc.password=root
4.业务层
1)service接口
package com.itheima.service;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.GeneralResult;
import com.itheima.domain.Student;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Transactional(readOnly = false)
public interface StudentService {
List<Student> findAll(); //查所有
Student findById(Integer id); //查单个
GeneralResult insertStu(Student student); //添加一个
GeneralResult updateStu(Integer id); //更新一个
@Transactional(readOnly = true)
GeneralResult deleteStu(Integer id); //删除一个
PageInfo findPage(Integer start,Integer size); //分页查询
}
2)service实现类
package com.itheima.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.StudentDao;
import com.itheima.domain.GeneralResult;
import com.itheima.domain.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import javax.sql.DataSource;
import java.util.List;
import java.util.Random;
import java.util.UUID;
@Component //存入IOC
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public List<Student> findAll() {
List<Student> all = studentDao.findAll();
return all;
}
@Override
public Student findById(Integer id) {
Student stu = studentDao.findById(id);
return stu;
}
//新建学生
@Override
public GeneralResult insertStu(Student student) {
student.setId(new Random().nextInt());
int num = studentDao.insertStu(student);
// int a = 1/0; 异常测试
int num1 = studentDao.insertStu(student);
GeneralResult<Student> result = new GeneralResult<>();
if(num!=0){
result.setFlag(true);
}else {
result.setFlag(false);
}
return result;
}
//更新学生信息
@Override
public GeneralResult updateStu(Integer id) {
int num = studentDao.updateStu(id);
GeneralResult<Student> result = new GeneralResult<>();
if(num!=0){
result.setFlag(true);
}else {
result.setFlag(false);
}
return result;
}
//删除功能
@Override
public GeneralResult deleteStu(Integer id) {
int num = studentDao.deleteStu(id);
GeneralResult result = new GeneralResult<>();
if(num!=0){
result.setFlag(true);
}else {
result.setFlag(false);
}
return result;
}
@Override
public PageInfo findPage(Integer start,Integer size) {
// PageHelper.startPage(2,2); 硬编码测试
PageHelper.startPage(start,size);
List<Student> all = studentDao.findAll();
PageInfo<Student> studentPageInfo = new PageInfo<>(all);
return studentPageInfo;
}
}
5.表现层
package com.itheima.controller;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.GeneralResult;
import com.itheima.domain.Student;
import com.itheima.service.Impl.StudentServiceImpl;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class ControllerDemo {
@Autowired
private StudentService studentService; //要使用接口接收,因为在执行事务的时候是代理类
//查询所有学生
@RequestMapping("/findAllStudents")
@ResponseBody
public List<Student> findAllStudents(){
// int a = 1/0;
List<Student> all = studentService.findAll();
System.out.println(all);
return all;
}
//按照ID查询学生
@RequestMapping("/findStudentById")
@ResponseBody
public Student findStudentById(@RequestBody Integer id){
Student stu = studentService.findById(id);
return stu;
}
//新建学生信息
@RequestMapping("/saveStudent")
@ResponseBody
public GeneralResult saveStudent(@RequestBody Student student){
GeneralResult<Student> result = studentService.insertStu(student);
return result;
}
//删除学生信息
@RequestMapping("/deletStudent")
@ResponseBody
public GeneralResult deletStudent(@RequestBody Integer id){
GeneralResult result = studentService.deleteStu(id);
return result;
}
//更新学生信息
@RequestMapping("/updateStudent")
@ResponseBody
public GeneralResult updateStudent(@RequestBody Integer id){
GeneralResult result = studentService.updateStu(id);
return result;
}
//分页查询消息
@RequestMapping("/findPage")
@ResponseBody
public GeneralResult findPage(Integer start,Integer size){
PageInfo page = studentService.findPage(start,size);
GeneralResult<Object> result = new GeneralResult<>();
if(page!=null){
result.setFlag(true);
result.setData(page);
}else {
result.setFlag(false);
}
return result;
}
}
6.AOP额外功能
此处使用的是半注解半配置文件的方式进行AOP配置
注意:AOP在设置规则的时候对表现层的方法是不起拦截作用的,也不会报错。只对业务层生效。
package com.itheima.domain;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
@Component //放入IOC种
@Aspect //设置切面
public class AOP {
//设置切点规则
@Pointcut("execution(* com.itheima.service..*(..))") //业务中只会对业务层进行拦截且不支持对表现层进行拦截。
public void AOP(){
}
@Before("AOP()") //设置切面类型
public void show(){
System.out.println("统一AOP输出...");
}
}
7.事务额外功能
此处使用的是半注解半配置文件的方式进行事务配置
使用Spring内部提供的事务类进行管理
使用注解@Transactional定义此类为需要执行事务的类
注意:1.tx的事务配置文件时 ,导入命名空间要以tx结尾!
2.当使用事务的时候,由于使用了代理类,所有要单独进行扫描,如果统一扫描Spring会认为对应的类不是在做事务功能从而反射出一个普通类,普通类不是代理类不具备事务的功能!(具体参考MVC配置文件中的配置)
@Transactional
//@Transactional(readOnly = false) 可以对该类使用属性进行统一管理,再在下面的各个方法进行单一管理
public interface StudentService {
List<Student> findAll(); //查所有
Student findById(Integer id); //查单个
GeneralResult insertStu(Student student); //添加一个
GeneralResult updateStu(Integer id); //更新一个
@Transactional(readOnly = true)
GeneralResult deleteStu(Integer id); //删除一个
PageInfo findPage(Integer start,Integer size); //分页查询
}
8.异常额外功能
此处使用的是半注解半配置文件的方式进行事务配置
@ExceptionHandler(Exception.class)定义了Exception,故所有的异常都会进入到该类中统一执行
package com.itheima.exception;
import com.itheima.domain.GeneralResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@Component //放入容器中
@ControllerAdvice //定义为异常类 异常的本质也是一个AOP
public class ExceptionDem {
//定义异常时使用的方法
@ExceptionHandler(Exception.class) //所有的异常都进入这里
@ResponseBody //返回的类型使用JSON
public GeneralResult getExceptions(@Autowired Exception ex){
GeneralResult<Object> result = new GeneralResult<>();
result.setFlag(false);
result.setMsg("错误!"+ex.getMessage());
return result;
}
}
xception,故所有的异常都会进入到该类中统一执行
```java
package com.itheima.exception;
import com.itheima.domain.GeneralResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@Component //放入容器中
@ControllerAdvice //定义为异常类 异常的本质也是一个AOP
public class ExceptionDem {
//定义异常时使用的方法
@ExceptionHandler(Exception.class) //所有的异常都进入这里
@ResponseBody //返回的类型使用JSON
public GeneralResult getExceptions(@Autowired Exception ex){
GeneralResult<Object> result = new GeneralResult<>();
result.setFlag(false);
result.setMsg("错误!"+ex.getMessage());
return result;
}
}