SSM 整合案例

Ssm整合

  • 注意事项
  1. Spring mvc+Spring+Mybatis+MySQL
  2. 项目结构,以web项目结构整合(还有maven、gradle)
  3. 整合日志、事务一个项目中
  4. Idea 2019、JDK12、Tomcat9、MySQL 5.5
  • 项目结构  D:\java\Idea\Idea_spacework\SSMhzy:不会就去找项目案例重新学习
  1. web项目为主结构,建立web目录,目录下有index.jsp和WEB-INF目录,WEB-INF目录可以放JSP文件。
  2. 配置文件建立在src目录下,Spring 容器配置文件bean.xml、Spring mvc请求处理配置springmvc-servlet.xml、Mybatis核心配置文件SqlConfig.xml。
  3. 程序代码存放位置,src目录下, 创建不同的包,dao、controller、service、pojo、util包存放不同的Java类
  4. JSP文件存放位置,WEB-INF目录下创建一个jsp目录,此目录下存储。
  5. 静态资源存放位置,在web目录下,和WEB-INF同一级目录创建res目录。

SSM 整合案例_第1张图片

 

  • 引入jar包
  1. Spring、springmvc基础包及其依赖包
  2. Mybatis核心包及其依赖包
  3. 数据库驱动程序包
  4. 采用C标签,需要jstl-1.2.jar
  5. 引入spring框架和mybaits框架结合的jar,mybatis-spring-1.3.0.jar

SSM 整合案例_第2张图片

 

  • 配置Tomcat服务器

SSM 整合案例_第3张图片

 

  • 配置项目结构

SSM 整合案例_第4张图片

SSM 整合案例_第5张图片

 

 

  • 配置文件
  1. web.xml文件

xml version="1.0" encoding="UTF-8"?>
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">

    
        contextConfigLocation
        classpath:bean.xml
    

    
        org.springframework.web.context.ContextLoaderListener
    
    
        springmvc
        
            org.springframework.web.servlet.DispatcherServlet
        

        
            contextConfigLocation
            classpath:springmvc-servlet.xml
        

        1
    
    
        springmvc
        /
    

    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        characterEncodingFilter
        /*
    

  1. bean.xml

xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
        https://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="inter.service"/>
    
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        name="driverClassName" value="com.mysql.cj.jdbc.Driver">
        name="url" value="jdbc:mysql://localhost:3306/school">
        name="username" value="root">
        name="password" value="root">
    

        id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
            
            name="typeAliasesPackage" value="inter.pojo"/>
            
            name="dataSource" ref="dataSource"/>
            
            name="configLocation" value="classpath:sqlConfig.xml">
            
            name="mapperLocations" value="classpath:inter/dao/*.xml"/>
        
        
        class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            name="basePackage" value="inter.dao"/>
        

  1. Springmvc-servlet.xml

xml version="1.0" encoding="UTF-8"?>
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
        https://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="inter.controller"/>

    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        name="prefix" value="/WEB-INF/jsp/"/>
        
        name="suffix" value=".jsp"/>
    

    
    <mvc:annotation-driven>mvc:annotation-driven>
    
    <mvc:resources mapping="/css/**" location="/res/css/"/>
    <mvc:resources mapping="/js/**" location="/res/js/"/>
    <mvc:resources mapping="/img/**" location="/res/img/"/>

  1. SqlConfig.xml

   xml version="1.0" encoding="UTF-8" ?>
configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">



    name="logImpl" value="STDOUT_LOGGING"/>

    
        type="inter.pojo.User" alias="User">
    

  • 代码

包结构: inter

-->

controller

-->service

-->pojo

-->dao

-->util

Controller层下面的代码

package inter.controller;

import inter.pojo.StuTable;
import inter.pojo.eduCation;
import inter.service.EduService;
import inter.service.StuTableService;
import inter.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class StuTableController {
    @Autowired
    StuTableService stuTableService;
    @Autowired
    EduService eduService;
    //新增数据页面
    @RequestMapping(value = "/insertStuT")
    public ModelAndView insertStuT() {
        ModelAndView mv  = new ModelAndView();
        List eduList = eduService.showAllEdu();
        mv.addObject("eduList",eduList);
        mv.setViewName("insertStuT");
        return mv;
    }

    //保存新增数据
    @RequestMapping(value = "/saveStuT")
    public ModelAndView saveStuT(StuTable stuTable) {
        int num = stuTableService.saveStuT(stuTable);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:stuList");
        return mv;
    }

    //显示所有记录
    @RequestMapping(value = "/stuList")
    public ModelAndView showAllStu(String pageNow,StuTable stuTable) {
        ModelAndView mv = new ModelAndView();
        try {
            //获取总行数
            int totalCount = stuTableService.stuCount();
            Page page = null;

            if (pageNow!=null){
                page = new Page(totalCount,Integer.parseInt(pageNow));
            }else {
                page = new Page(totalCount,1);
            }
            StuTable stu = new StuTable();
            stu.setPageSize(page.getPageSize());
            stu.setStart(page.getStartPos());
            if (stuTable.getTel()!=null){
                stu.setTel(stuTable.getTel());
            }
            if (stuTable.getSname()!=null){
                stu.setTel(stuTable.getSname());
            }
            List list = stuTableService.showAllStu(stu);
            List eduList = eduService.showAllEdu();
            mv.addObject("eduList",eduList);
            mv.addObject("list", list);
            mv.addObject("page",page);
            mv.setViewName("stuList");
        }catch (Exception e){
            e.printStackTrace();
        }
        return mv;
    }

    //删除记录
    @RequestMapping(value = "/delStut")
    public ModelAndView delStu(int bh) {
        int sta = stuTableService.delStu(bh);
        ModelAndView mv = new ModelAndView();
        if (sta > 0) {
            mv.setViewName("redirect:stuList");
        }
        return mv;
    }

    //根据编号显示一条记录
    @RequestMapping(value = "/editStu1")
    public ModelAndView getStuByBh(int bh) {
        ModelAndView mv = new ModelAndView();
        StuTable stu1 = stuTableService.getStuByBh(bh);
        List eduList = eduService.showAllEdu();
        mv.addObject("eduList",eduList);
        mv.addObject("stu", stu1);
        mv.setViewName("editStu");
        return mv;
    }

    //修改数据
    @RequestMapping(value = "/updateStu")
    public String updateStu(StuTable stuTable) {
        int sta1 = stuTableService.updateStu(stuTable);
        return "redirect:stuList";//重定向,自动携带数据
    }
    //显示详情页面
    @RequestMapping (value = "/tipStu")
    public ModelAndView tipStu(int bh){
        ModelAndView mv = new ModelAndView();
        StuTable stu2 = stuTableService.getStuByBh(bh);
        mv.addObject("stu1", stu2);
        mv.setViewName("tipStu");
        return mv;
    }



}

Service下面的代码


package inter.service;

import inter.dao.IStuTableDao;
import inter.pojo.StuTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StuTableService {
    @Autowired
    IStuTableDao tableDao;
    //新增
    @Transactional
    public int saveStuT(StuTable stu){
        int num = tableDao.saveStuT(stu);
        return num;
    }
    //显示所有
    public List showAllStu(StuTable stuTable) {
        return tableDao.showAllStu(stuTable);
    }
    //删除记录
    public int delStu(int bh){
        return tableDao.delStu(bh);
    }
    //显示一条记录
    public StuTable getStuByBh(int bh){
        return tableDao.getStuByBh(bh);
    }
    //修改
    public int updateStu(StuTable stuTable){
        return  tableDao.updateStu(stuTable);
    }
    //显示记录数
    public int stuCount(){
        return tableDao.stuCount();
    }


    public IStuTableDao getTableDao() {
        return tableDao;
    }

    public void setTableDao(IStuTableDao tableDao) {
        this.tableDao = tableDao;
    }
}

 Pojo 代码

package inter.pojo;

public class StuTable {
    private int bh;
    private String sname;
    private String tel;
    private String birthday;
    private String gender;
    private String xl;
    private double cj;
    private String grqk;
    //下面两个参数,只是供传参使用
    private int start;//索引开始位置
    private int pageSize;//数据显示记录数

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getBh() {
        return bh;
    }

    public void setBh(int bh) {
        this.bh = bh;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getXl() {
        return xl;
    }

    public void setXl(String xl) {
        this.xl = xl;
    }

    public double getCj() {
        return cj;
    }

    public void setCj(double cj) {
        this.cj = cj;
    }

    public String getGrqk() {
        return grqk;
    }

    public void setGrqk(String grqk) {
        this.grqk = grqk;
    }
}

Dao 代码

package inter.dao;

import inter.pojo.StuTable;

import java.util.List;

public interface IStuTableDao {
    //数据保存
    int saveStuT(StuTable stuTable);

    //显示所有
    List showAllStu(StuTable stuTable);

    //删除
    int delStu(int bh);

    //根据编号显示一条记录
    StuTable getStuByBh(int bh);

    //修改
    int updateStu(StuTable stuTable);

    //显示记录数
    int stuCount();


}

Util 工具类 里面


package inter.util;

public class Page {
    private int pageNow=1; //当前页数
    private int pageSize=5; //每页显示的数量
    private int totalCount; //总记录数
    private int totalPageCount; //总页数
    private int startPos; //开始位置,从0开始
    private boolean hasFrist; //是否有首页
    private boolean hasPre; //是否有前一页
    private boolean hasNext; //是否有后一页
    private boolean hasLast; //是否有尾页 /**

    //通过构造函数,传入总记录数和当前页
    public Page(int totalCount, int pageNow){
        this.totalCount=totalCount;
        this.pageNow=pageNow;
    }
    //获取总页数
    public int getTotalPageCount() {
        totalPageCount=getTotalCount()/getPageSize();
        return (totalCount%pageSize==0)?totalPageCount:totalPageCount+1;
    }
    public void setTotalPageCount(int totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    //获取选择记录的初始位置
    public int getStartPos() {
        return (pageNow-1)*pageSize;
    }

    public void setStartPos(int startPos) {
        this.startPos = startPos;
    }
    //判断是否有第一页
    public boolean isHasFrist() {
        return (pageNow==1) ? false:true;
    }
    public void setHasFrist(boolean hasFrist) {
        this.hasFrist = hasFrist;
    }
    //如果有首页就有前一页
    public boolean isHasPre() {
        return isHasFrist() ? true:false;
    }
    public void setHasPre(boolean hasPre) {
        this.hasPre = hasPre;
    }
    //如果有尾页就有下一页
    public boolean isHasNext() {
        return isHasNext() ? true:false;
    }
    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }
    //判断是否有尾页
    public boolean isHasLast() {
        return (pageNow==getTotalCount()) ? false:true;
    }
    public void setHasLast(boolean hasLast) {
        this.hasLast = hasLast;
    }


    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }


}

JSP 页面代码部分

修改页面

<%@ page import="inter.pojo.StuTable" %><%--
  Created by IntelliJ IDEA.
  User: shun
  Date: 2023/6/6
  Time: 20:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    </span></span><span style="background-color:#2b2b2b;"><span style="color:#ffffff;">根据编号显示一条记录</span></span><span style="background-color:#2b2b2b;"><span style="color:#ffffff;">


method="post" action="/updateStu">
border="1" cellpadding="16" cellspacing="0">
    
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
colspan="2">修改页面
编号
            ${stu.bh}
            type="hidden" name="bh" value="${stu.bh}">
        
姓名
            type="text" placeholder="请输入姓名" name="sname" value="${stu.sname}">
        
电话
            type="text" placeholder="请输入电话" name="tel" value="${stu.tel}">
        
生日
            type="text" placeholder="请输入生日" name="birthday" value="${stu.birthday}">
        
性别
            <c:if test="${stu.gender==1}">
                type="radio" name="gender" value="1"  checked>
                type="radio" name="gender" value="0">
            c:if>
            <c:if test="${stu.gender==0}">
                type="radio" name="gender" value="1">
                type="radio" name="gender" value="0" checked>
            c:if>
        
学历
                
        
成绩
            type="text" placeholder="请输入成绩" name="cj" value="${stu.cj}">
        
个人情况
            type="text" placeholder="请输入个人情况" name="grqk" value="${stu.grqk}">
        
修改
            type="submit" value="修改">
        

    


新增页面

<%--
  Created by IntelliJ IDEA.
  User: shun
  Date: 2023/6/5
  Time: 19:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    </span></span><span style="background-color:#2b2b2b;"><span style="color:#ffffff;">信息输入</span></span><span style="background-color:#2b2b2b;"><span style="color:#ffffff;">


    

用户输入


    
action="/saveStuT" method="post">
        type="text" name="sname" placeholder="请输入姓名">

        type="text" name="tel" placeholder="请输入手机号">

        type="text" name="birthday" placeholder="请输入出生日期">

        type="radio" value="1" name="gender">
        type="radio" value="0" name="gender">

        
        

        type="text" name="cj" placeholder="请输入成绩">

        type="text"  name="grqk" placeholder="请输入个人情况">

        type="submit" value="保存">
    

显示页面

<%--
  Created by IntelliJ IDEA.
  User: shun
  Date: 2023/6/6
  Time: 19:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    </span></span><span style="background-color:#2b2b2b;"><span style="color:#ffffff;">显示所有记录</span></span><span style="background-color:#2b2b2b;"><span style="color:#ffffff;">
    


border="1px" cellspacing="0" cellpadding="10" align="center">
    
        
    
    
        
        
        
        
        
        
        
        
    
    <c:forEach items="${list}" var="stu">
        
            
            
            
            
            
            
            
            
        
    c:forEach>
    

        
    
colspan="8">
            href="/insertStuT">新增数据
            
method="post" action="/stuList" style="display:inline-block">
                type="text" name="username" placeholder="请输入查询姓名">
                type="text" name="tel" placeholder="请输入查询电话">
                type="submit" value="查询"/>
            
        
编号姓名电话性别学历成绩个人情况操作
${stu.bh}${stu.sname}${stu.tel}
                <c:if test="${stu.gender==1}">
                    
                c:if>
                <c:if test="${stu.gender==0}">
                    
                c:if>
            

                <c:forEach items="${eduList}" var="edu">
                    <c:if test="${edu.edubh==stu.xl}">
                        ${edu.eduname}
                    c:if>
                c:forEach>
            
${stu.cj}${stu.grqk}
                href="/delStut?bh=${stu.bh}">删除
                href="/editStu1?bh=${stu.bh}">修改
                href="/tipStu?bh=${stu.bh}">详情
            
colspan="8">
            
style="text-align: center">
                size=2>
                    ${page.totalPageCount}
                

                size=2>
                    ${page.pageNow}
                

                href="/stuList?pageNow=1">首页
                <c:choose>
                    <c:when test="${page.pageNow - 1 > 0}">
                        href="/stuList?pageNow=${page.pageNow - 1}">上一页
                    c:when>
                    <c:when test="${page.pageNow - 1 <= 0}">
                        href="/stuList?pageNow=1">上一页

                    c:when>
                c:choose>
                <c:choose>
                    <c:when test="${page.totalPageCount==0}">
                        href="/stuList?pageNow=${page.pageNow}">下一页

                    c:when>
                    <c:when test="${page.pageNow + 1 < page.totalPageCount}">
                        href="/stuList?pageNow=${page.pageNow + 1}">下一页

                    c:when>
                    <c:when test="${page.pageNow + 1 >= page.totalPageCount}">
                        href="/stuList?pageNow=${page.totalPageCount}">下一页

                    c:when>
                c:choose>
                <c:choose>
                    <c:when test="${page.totalPageCount==0}">
                        href="/stuList?pageNow=${page.pageNow}">尾页

                    c:when>
                    <c:otherwise>
                        href="/stuList?pageNow=${page.totalPageCount}">尾页

                    c:otherwise>
                c:choose>
            

        


详细页面

<%--
  Created by IntelliJ IDEA.
  User: shun
  Date: 2023/6/6
  Time: 20:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    </span></span><span style="background-color:#2b2b2b;"><span style="color:#ffffff;">详细</span></span><span style="background-color:#2b2b2b;"><span style="color:#ffffff;">


border="1" cellpadding="16" cellspacing="0">
    
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
colspan="2">详细页面
编号
            ${stu1.bh}
        
姓名
           ${stu1.sname}
        
电话
           ${stu1.tel}
        
生日
          ${stu1.birthday}
        
性别
          ${stu1.gender}
        
学历
            ${stu1.xl}
        
成绩
            ${stu1.cj}
        
个人情况
          ${stu1.grqk}
        


  • 运行

使用Tomcat运行,在浏览器中显示

显示页面

SSM 整合案例_第6张图片

 

新增数据页面

SSM 整合案例_第7张图片

 

修改页面

SSM 整合案例_第8张图片

 

详细页面

SSM 整合案例_第9张图片

 

  • 配置日志

在src下创建一个log4j.properties文件配置日志

# Global logging configuration
log4j.rootLogger = DEBUG,stdout
#Console output...
log4j.appender.stdout= org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

  • 配置事务

在 bean.xml 文件中配置


  
    id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        name="dataSource" ref="dataSource"/>
    


    <tx:annotation-driven transaction-manager="transactionManager"/>

你可能感兴趣的:(mybatis,java,spring)