SSM整合之XML配置方式(含事务管理)详细教程

SSM是一种常见的Java Web开发框架,它是由Spring、SpringMVC和MyBatis三个框架整合而成,用于简化企业级应用程序的开发。
Spring:Spring是一个轻量级的开源框架,它提供了许多功能,如依赖注入、事务管理、安全性等,可以帮助开发者快速构建稳健的应用程序。SpringMVC:SpringMVC是一个基于MVC模式的框架,它用于将应用程序分为不同的层次,包括模型、视图和控制层,使得应用程序的架构更加清晰,易于维护和扩展。MyBatis:MyBatis是一个优秀的持久层框架,它支持自定义SQL语句、存储过程和事务处理等功能,使得开发者可以更加方便地进行数据库操作。 通过整合这三个框架,SSM可以充分发挥各自的优势,实现更加高效、灵活和可扩展的应用程序开发.
本文通过项目案例的形式,通过使用MVC的形式介绍SSM整合的具体过程,项目结构如下:
每个文件的具体内容,如下所示:
1.依赖:pom.xml
2. web.xml
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">
3.Spring核心配置文件:applicationContext.xml
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/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">
4. SpringMVC的配置文件:spring-mvc.xml
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">
5.MyBatis配置文件:mybatis-config.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
6. 实体类:Student
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private String birth;
private String sex;
}
7. 控制层:StudentQueryController
public class StudentQueryController implements Controller {
private StudentService studentService;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView();
List
mav.addObject("students",students);
mav.setViewName("show");
return mav;
}
}
8. 服务层:StudentService接口 与 接口实现类
import java.util.List;
public interface StudentService {
public List
}
import java.util.List;
public class StudentServiceImpl implements StudentService {
private StudentMapper studentMapper;
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
@Override
public List
return studentMapper.listStudents();
}
}
9. 持久层:StudentMapper
public interface StudentMapper {
public List
}
10. 持久层映射文件:StudentMapper.xml
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from t_student;
11. 首页面:index.jsp
12. 展示页面:show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
序号 | 姓名 | 出生日期 | 性别 | 操作 |
---|---|---|---|---|
${status.index+1} | ${student.name} | ${student.birth} | ${student.sex} | 删除 |
13. 数据库表结构
mysql> desc t_student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(10) | YES | | NULL | |
| birth | varchar(20) | YES | | NULL | |
| sex | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
到此,整个配置整理完毕!