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

        SSM是一种常见的Java Web开发框架,它是由Spring、SpringMVC和MyBatis三个框架整合而成,用于简化企业级应用程序的开发。

  1. Spring:Spring是一个轻量级的开源框架,它提供了许多功能,如依赖注入、事务管理、安全性等,可以帮助开发者快速构建稳健的应用程序。
  2. SpringMVC:SpringMVC是一个基于MVC模式的框架,它用于将应用程序分为不同的层次,包括模型、视图和控制层,使得应用程序的架构更加清晰,易于维护和扩展。
  3. MyBatis:MyBatis是一个优秀的持久层框架,它支持自定义SQL语句、存储过程和事务处理等功能,使得开发者可以更加方便地进行数据库操作。

      通过整合这三个框架,SSM可以充分发挥各自的优势,实现更加高效、灵活和可扩展的应用程序开发.

      本文通过项目案例的形式,通过使用MVC的形式介绍SSM整合的具体过程,项目结构如下:

 每个文件的具体内容,如下所示:

1.依赖:pom.xml

    <dependencies>
        <!--servlet-api依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!--spring基础依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.20</version>
        </dependency>
        <!--spring连接jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.20</version>
        </dependency>
        <!--spring web依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.20</version>
        </dependency>
        <!--spring webmvc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.20</version>
        </dependency>
        <!--aop面向切面编程框架-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.7</version>
        </dependency>
        <!--spring事务管理-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.3.20</version>
        </dependency>
        <!--mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>
        <!--mybatis与spring整合-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--mysql数据库依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>
        <!--dbcp连接池-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.11.0</version>
        </dependency>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
        <!--jstl标签库依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

2. 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">

    <!--Spring监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--Spring配置文件的加载-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--编码过滤-->
    <filter>
        <filter-name>encondig</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>
        <filter-name>encondig</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--
        springmvc 前端控制器
    -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

3.Spring核心配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--配置数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/student_db"/>
        <property name="username" value="root"/>
        <property name="password" value="12345678"/>
    </bean>

    <!--配置SqlSession工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据源配置-->
        <property name="dataSource" ref="dataSource"/>
        <!--mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--StudentMapper代理-->
    <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--指定Mapper接口-->
        <property name="mapperInterface" value="com.laoma.mapper.StudentMapper"/>
        <!--配置SqlSession工厂-->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <!--service配置-->
    <bean id="studentService" class="com.laoma.service.impl.StudentServiceImpl">
        <property name="studentMapper" ref="studentMapper"/>
    </bean>

    <!--事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--事务的配置-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--配置事务的属性-->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
            <!--
            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT"/>
            -->
        </tx:attributes>
    </tx:advice>
    <!--使用AOP进行事务的配置-->
    <aop:config>
        <!--指定哪个类为切面类-->
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.laoma.service.impl.StudentServiceImpl.*(..))"/>
    </aop:config>
</beans>

4. SpringMVC的配置文件: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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--处理器映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!--处理器-->
    <bean name="/query" class="com.laoma.controller.StudentQueryController">
        <property name="studentService" ref="studentService"/>
    </bean>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

5.MyBatis配置文件: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>
    <!--实体类别名设置-->
    <typeAliases>
        <package name="com.laoma.pojo"/>
    </typeAliases>
    <mappers>
        <mapper resource="mapper/StudentMapper.xml"/>
    </mappers>
</configuration>

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<Student> students = studentService.listStudents();
        mav.addObject("students",students);
        mav.setViewName("show");
        return mav;
    }
}

8. 服务层:StudentService接口 与 接口实现类

import java.util.List;

public interface StudentService {
    public List<Student> listStudents();
}
import java.util.List;

public class StudentServiceImpl implements StudentService {
    private StudentMapper studentMapper;

    public void setStudentMapper(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public List<Student> listStudents() {
        return studentMapper.listStudents();
    }
}

9. 持久层:StudentMapper

public interface StudentMapper {
    public List<Student> listStudents();
}

10. 持久层映射文件:StudentMapper.xml

<?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">
<mapper namespace="com.laoma.mapper.StudentMapper">
    <select id="listStudents" resultType="Student">
        select * from t_student;
    </select>
</mapper>

11. 首页面:index.jsp

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="/query">查看所有学生信息</a>
</body>
</html>

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" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <th>序号</th>
        <th>姓名</th>
        <th>出生日期</th>
        <th>性别</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${students}" var="student" varStatus="status">
        <tr>
            <th>${status.index+1}</th>
            <th>${student.name}</th>
            <th>${student.birth}</th>
            <th>${student.sex}</th>
            <th><a href="#">删除</a></th>
        </tr>
    </c:forEach>
</table>
</body>
</html>

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    |       |
+-------+-------------+------+-----+---------+-------+

到此,整个配置整理完毕!

  • 23
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值