SSM整合(XML方式)

文章目录

  • SSM整合之后xml方式
  • 1 系统环境
    • 1.1 软件环境
    • 1.2 项目环境
    • 1.3 配置web.xml
    • 1.4 配置jdbc.properties文件
    • 1.5 配置SpringMVC核心文件
    • 1.6 配置Spring的核心文件
    • 1.7 配置MyBatis的核心文件
    • 1.8 配置数据库
    • 1.9 配置文件位置
  • 2 编写后端代码
    • 2.1 编写实体类
    • 2.2 编写Dao接口
    • 2.3 编写Dao映射文件(resource目录下)
    • 2.4 编写Service接口
    • 2.5 编写ServiceImpl实现类
    • 2.6 编写Controller类
    • 2.7 代码映射文件位置
  • 3 编写前端代码
    • 3.1 编写首页index.jsp
    • 3.2 配置Tomcat
    • 3.3 webapp下配置图片
    • 3.4 编写功能页面WEB-INF下
  • 4 运行访问
    • 4.1 请求路径
    • 4.2 增删改查界面
    • 4.3 项目源代码

SSM整合之后xml方式

1 系统环境

1.1 软件环境

软件版本:
IDEA 2021.3
Maven 3.6.3
MySql (Mariadb) 10.10
JDK 1.8

1.2 项目环境

1、创建Maven的web工程
SSM整合(XML方式)_第1张图片
2、引入pom依赖

 <dependencies>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>4.0.1version>
            <scope>providedscope>
        dependency>
        
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.0version>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.3.1version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>5.3.1version>
        dependency>

        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>2.14.2version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.14.2version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.13version>
        dependency>
        
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.1.1version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.28version>
        dependency>
        
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.3.17version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.2.16version>
        dependency>

        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.28version>
        dependency>
    dependencies>

3 设置插件

 <build>
        <resources>
            <resource>
                
                <directory>src/main/javadirectory>
                
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.1version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>
        plugins>
    build>

1.3 配置web.xml

配置前端控制器

    
    <servlet>
        <servlet-name>myssmservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:config/springmvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>myssmservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>

注册监听器

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:config/applicationContext.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

注册字符过滤器,处理中文乱码

    
    <filter>
        <filter-name>characterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
        <init-param>
            <param-name>forceRequestEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
        <init-param>
            <param-name>forceResponseEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>characterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

1.4 配置jdbc.properties文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db002
jdbc.username=root
jdbc.password=root

1.5 配置SpringMVC核心文件


<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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    
    <context:component-scan base-package="com.hx.controller"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    bean>
    
    <mvc:annotation-driven>mvc:annotation-driven>
beans>

1.6 配置Spring的核心文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
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/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

        

    
    <context:property-placeholder location="classpath:config/jdbc.properties" />
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <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>

    
    
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        <property name="configLocation" value="classpath:config/mybatis-config.xml" />
    bean>

    
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.hx.dao"/>
    bean>

    
    <context:component-scan base-package="com.hx.service"/>

beans>

1.7 配置MyBatis的核心文件


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    <typeAliases>
        <package name="com.hx.domain"/>
    typeAliases>

    
    <mappers>
        
        <package name="com.hx.dao"/>
    mappers>
configuration>

1.8 配置数据库

创建数据库,数据表student
表字段如下:
SSM整合(XML方式)_第2张图片

1.9 配置文件位置

SSM整合(XML方式)_第3张图片

2 编写后端代码

2.1 编写实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private Integer sid;
    private String sname;
    private Integer sage;
}

2.2 编写Dao接口

@Mapper
public interface StudentDao {
    //查询所有
    List<Student> findAll();
    //新增数据
    int addStu(@Param("student") Student student);
    //删除数据
    int delStuById(Integer sid);
    //修改数据
    int updateStu(Student student);
}

2.3 编写Dao映射文件(resource目录下)


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hx.dao.StudentDao">
    
    <select id="findAll" resultType="Student">
        select sid, sname, sage
        from student;
    select>
    
    <insert id="addStu">
        insert into student
        values (#{student.sid}, #{student.sname}, #{student.sage});
    insert>
    
    <delete id="delStuById">
        delete
        from student
        where sid = #{sid}
    delete>
    
    <update id="updateStu" parameterType="Student">
        update student
        set sname=#{sname},
            sage=#{sage}
        where sid = #{sid}
    update>
mapper>

2.4 编写Service接口

public interface StudentService {
    //查询所有
    List<Student> selectAll();

    //插入数据
    int insertStu(Student student);

    int delStuById(Integer sid);

    int updateStu(Student student);
}

2.5 编写ServiceImpl实现类

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public List<Student> selectAll() {
        return studentDao.findAll();
    }

    @Override
    public int insertStu(Student student) {
        return studentDao.addStu(student);
    }

    @Override
    public int delStuById(Integer sid) {
        return studentDao.delStuById(sid);
    }

    @Override
    public int updateStu(Student student) {
        return studentDao.updateStu(student);
    }
}

2.6 编写Controller类

1、业务Controller

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/getAll.do")
    public ModelAndView getAll() {
        List<Student> list = studentService.selectAll();
        ModelAndView mv = new ModelAndView();
        mv.addObject("list", list);
        mv.setViewName("listStu");
        return mv;
    }

    @PostMapping("/add.do")
    public ModelAndView saveStu(Student student) {
        String tips = "插入失败";
        ModelAndView mv = new ModelAndView();
        int i = studentService.insertStu(student);
        if (i > 0) {
            tips = "插入成功";
        }
        mv.addObject("data1", tips);
        mv.setViewName("success");
        return mv;
    }

    @RequestMapping("/put.do")
    public ModelAndView putStu(Student student) {
        String tips = "修改失败!";
        ModelAndView mv = new ModelAndView();
        int i = studentService.updateStu(student);
        if (i > 0) {
            tips = "修改成功";
        }
        mv.addObject("data2", tips);
        mv.setViewName("success");
        return mv;
    }

    @RequestMapping(value = "/del.do")
    public ModelAndView delStu(Integer sid) {
        String tips = "删除失败!";
        ModelAndView mv = new ModelAndView();
        int i = studentService.delStuById(sid);
        if (i > 0) {
            tips = "删除成功";
        }
        mv.addObject("data3", tips);
        mv.setViewName("success");
        return mv;
    }
}

2、页面相关Controller

@RestController
@RequestMapping("/index")
public class IndexController {

    @RequestMapping("/m1Add.do")
    public ModelAndView m1Add(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("add");
        return mv;
    }
    @RequestMapping("/m2Put.do")
    public ModelAndView m2Put(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("put");
        return mv;
    }
    @RequestMapping("/m3Del.do")
    public ModelAndView m3Del(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("del");
        return mv;
    }
}

2.7 代码映射文件位置

SSM整合(XML方式)_第4张图片

3 编写前端代码

3.1 编写首页index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
    <style>
        div {
            background-color: antiquewhite;
            width: 400px;
            height: 200px;
            margin: 100px auto;
            text-align: center;
            background-image: url("images/p5.jpg");
            background-repeat: no-repeat;
        }

        a {
            text-decoration: none;
            color: orange;
        }

        button {
            margin: 10px 20px;
        }
    style>
head>
<body>
<div>
    <h1>功能区首页h1><br>
    <button><a href="/student/getAll.do">查询数据a>button>

    <button><a href="/index/m1Add.do">插入数据a>button>
    <br>
    <button><a href="/index/m2Put.do">修改数据a>button>

    <button><a href="/index/m3Del.do">删除数据a>button>
div>
body>
html>

3.2 配置Tomcat

SSM整合(XML方式)_第5张图片SSM整合(XML方式)_第6张图片

3.3 webapp下配置图片

SSM整合(XML方式)_第7张图片

3.4 编写功能页面WEB-INF下

SSM整合(XML方式)_第8张图片

1 编写 listStu.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
   String basePath =
           request.getScheme() + "://" +
                   request.getServerName() + ":" +
                   request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
   <title>展示所有学生信息title>
   <style>
       .box1 {
           width: 300px;
           height: 400px;
           text-align: center;
           margin: 10px auto;
           background-image: url("/images/p3.jpg");
           background-repeat: no-repeat;
       }
       .box2 {
           width: 80px;
           height: 25px;
           text-align: center;
           margin: 10px auto;
       }
   style>
head>
<body>
<div class="box1">
   <table border="1px" width="300px" height="30px" align="center" cellspacing="0" cellpadding="0">
       <caption style="font-size: 20px">学生信息表caption>
       <tr bgcolor="#a9a9a9" text-align="center">
           <td>学号td>
           <td>姓名td>
           <td>年龄td>
       tr>
       <%--数据行--%>
       <c:forEach items="${list}" var="stu" varStatus="s">
           <tr>
               <td>${stu.sid}td>
               <td>${stu.sname}td>
               <td>${stu.sage}td>
           tr>
       c:forEach>
   table>
   <%--返回到首页--%>
   <div class="box2">
       <form action="<%=basePath%>index.jsp">
           <input type="submit" name="返回" value="返回功能区">
       form>
   div>
div>
body>
html>

2 编写 add.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String basePath =
            request.getScheme() + "://" +
                    request.getServerName() + ":" +
                    request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
    <title>新增学生信息title>
    <style>
        div {
            background-color: antiquewhite;
            width: 300px;
            height: 180px;
            margin: 100px auto;
            text-align: center;
            line-height: normal;
            background-image: url("/images/p1.jpg");
            background-repeat: no-repeat;
        }
        .box2 {
            width: 80px;
            height: 25px;
            text-align: center;
            margin: auto;
        }
    style>
head>
<body>
<div>
    <h3>新增数据h3>
    <form action="/student/add.do" method="post">
        学号:<input type="text" name="sid" value=""><br>
        姓名:<input type="text" name="sname" value=""><br>
        年龄:<input type="text" name="sage" value=""><br>
        <input type="submit" value="添加">
        <input type="reset" value="重置">
    form>
    <%--返回到首页--%>
    <div class="box2">
        <form action="<%=basePath%>index.jsp">
            <input type="submit" name="返回" value="返回功能区">
        form>
    div>
div>
body>
html>

3 编写 put.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String basePath =
            request.getScheme() + "://" +
                    request.getServerName() + ":" +
                    request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
    <title>修改学生信息title>
    <style>
        div {
            background-color: antiquewhite;
            width: 300px;
            height: 180px;
            margin: 100px auto;
            text-align: center;
            line-height: normal;
            background-image: url("/images/p4.jpg");
            background-repeat: no-repeat;
        }
        .box2 {
            width: 80px;
            height: 25px;
            text-align: center;
            margin: auto;
        }
    style>
head>
<body>
<div>
    <h3>根据学号修改数据h3>
    <form action="/student/put.do" method="post">
        学号:<input type="text" name="sid" value=""><br>
        姓名:<input type="text" name="sname" value=""><br>
        年龄:<input type="text" name="sage" value=""><br>
        <input type="submit" value="修改">
        <input type="reset" value="重置">
    form>
    <%--返回到首页--%>
    <div class="box2">
        <form action="<%=basePath%>index.jsp">
            <input type="submit" name="返回" value="返回功能区">
        form>
    div>
div>
body>
html>

4 编写 del.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String basePath =
            request.getScheme() + "://" +
                    request.getServerName() + ":" +
                    request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
    <title>删除学生信息title>
    <style>
        div {
            background-color: beige;
            width: 300px;
            height: 150px;
            margin: 100px auto;
            text-align: center;
            background-image: url("/images/p2.jpg");
            background-repeat: no-repeat;
        }
        .box2 {
            width: 80px;
            height: 25px;
            text-align: center;
            margin: auto;
        }
    style>
head>
<body>
<div>
    <h3>根据学号删除数据h3>
    <form action="/student/del.do" method="get">
        <input type="text" placeholder="请输入学号" name="sid" value="">
        <input type="submit" value="删除">
    form>
    <%--返回到首页--%>
    <div class="box2">
        <form action="<%=basePath%>index.jsp">
            <input type="submit" name="返回" value="返回功能区">
        form>
    div>
div>
body>
html>

5 编写success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String basePath =
            request.getScheme() + "://" +
                    request.getServerName() + ":" +
                    request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
    <title>Titletitle>
    <style>
        .box1{
            width: 200px;
            height: 150px;
            background-color: lightgoldenrodyellow;
            margin: 0 auto;
            text-align: center;
        }
    style>
head>
<body>
<div class="box1">
    <div class="box2">
        <div>插入:${data1}div>
        <div>修改:${data2}div>
        <div>删除:${data3}div>
    div>
    <%--返回到首页--%>
    <div>
        <form action="<%=basePath%>index.jsp">
            <input type="submit" name="返回" value="返回功能区">
        form>
    div>
div>
body>
html>

4 运行访问

4.1 请求路径

首页:http://localhost:8080
SSM整合(XML方式)_第9张图片

4.2 增删改查界面

  • 查询功能
    SSM整合(XML方式)_第10张图片
  • 新增功能
    SSM整合(XML方式)_第11张图片
  • 修改功能
    SSM整合(XML方式)_第12张图片
  • 删除功能
    SSM整合(XML方式)_第13张图片

4.3 项目源代码

https://gitee.com/allureyu/ssm__xml.git

以上纯属个人一手编写,欢迎指教,不喜勿喷!

你可能感兴趣的:(SSM,ssm)