栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SSM框架案例学习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SSM框架案例学习

SSM框架:SpringMVC+Spring+MyBatis

利用SSM框架设计教务管理系统,进行信息的CRUD(Create、Retrieve、Update、Delete)

学习来源:尚硅谷

实现功能:1.页面分页

                  2.数据校验:jQuery前端校验+后端校验

                  3.使用ajax无刷新的页面更新

                  4.对信息进行增删改查操作


相关技术:1.基础框架-SSM(SpringMVC+Spring+MyBatis)

                  2.数据库-MySQL

                  3.前端框架-bootstrap快速搭建界面

                  4.项目依赖管理-Maven

                  5.分页-pagehelper

                  6.MyBatis Generator


首先进行基础环境搭建:

1、创建一个maven工程

首先在eclipse中配置maven,打开eclipse,点开 windows-preferences-maven-Installations, 将安装的 maven 添加进去并选中, 然后apply,步骤如图。

  

 说明一下,首先点击Add,然后点击Directory,找到当时安装maven的路径,点击Finish。

然后在Maven下找到 User Settings, 然后添加settings.xml文件路径设置,点击 apply。

 到此,eclipse配置maven就完成了,然后使用maven创建项目。

2、引入项目依赖的jar包

在pom.xml中引入,包括Spring,SpringMVC,MyBatis,数据库连接池,驱动包以及其他包(JSTL,servlet-api,junit)


  
  
  
      org.springframework
      spring-webmvc
      5.3.7
    
    
    
    
      org.springframework
      spring-jdbc
      5.3.7
    
    
    
    
      org.springframework
      spring-aspects
      5.3.7
    
    
    
    
      org.mybatis
      mybatis
      3.4.2
    
    
    
    
      org.mybatis
      mybatis-spring
      1.3.1
    
    
    
    
      c3p0
      c3p0 
      0.9.1 
    
    
    
      mysql
      mysql-connector-java
      5.1.41
    
    
    
    
      jstl
      jstl
      1.2
    
    
    
      javax.servlet
      servlet-api
      2.5
     provided
    
    
    
    
      junit
      junit
      4.12
      
    
   

3、引入bootstrap前端框架

首先,Bootstrap 是最受欢迎的 HTML、CSS 和 JS 框架,用于开发响应式布局、移动设备优先的 WEB 项目。可以去官网自行下载。对于bookstrap的使用方法,只需要引入Bootstrap 核心 CSS 文件 和Bootstrap 核心JavaScript文件 即可。






注意:src中的路径对应文件所在的路径。

到此,现在就可以快速搭建较美观的界面了。

使用bookstrap搭建界面时,举个例子,要为按钮添加一个好看的样式,首先在bookstrap中文网里找到喜欢的按钮对应的代码块,复制class属性里的内容即可。

4、编写SSM整合的关键配置文件

在web.xml中配置文件,给spring,springmvc,mybatis配置文件。

(1)启动Spring的容器

 然后创建Spring的配置文件,名称为applicationContext.xml

 

提示:applicationContext.xml是Spring的配置文件,主要配置和与业务逻辑有关的。

(2)SpringMVC的前端控制器 拦截所有的请求

 然后创建SpringMVC的配置文件,名称为dispatcherServlet-servlet.xml

 (3)字符编码过滤器

注意:字符编码过滤器,一定要放在所有过滤器之前


		CharacterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
		
			forceRequestEncoding
			true
		
		
			forceResponseEncoding
			true
		
	
	
		CharacterEncodingFilter
		

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MapperTest {
	
	@Autowired
	DeptmentMapper deptmentmapper;
	
	@Autowired
	StudentMapper studentmapper;
	
	@Autowired
	SqlSession sqlSession;
	
	//测试DeptmentMapper
	@Test
	public void testCRUD() {
		System.out.println(deptmentmapper);
		
		
		
		//插入学生信息
		//studentmapper.insertSelective(new Student(null, "fengrui", "男", "20191610000", 1));
		
		//批量插入多个学生信息
		StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
		for(int i=0;i<3;i++) {
			String name = UUID.randomUUID().toString().substring(0, 5);
			mapper.insertSelective(new Student(null, name, "男", "123123", 1));
		}
		
	}
}

测试之后,打开数据库表里就有了增加的学生和班级信息了,到此,就测试完成了。接下来开始增删改查的操作了。


进行CRUD相关操作:

1.查询

 具体流程:1、访问首页面index.jsp

                   2、index.jsp页面发送查询学生信息列表的请求

                   3、控制器StudentController接收并处理请求,查询学生信息

                   4、结果页面list.jsp显示学生信息

因为要实现一个分页查询信息的功能,所以需要在pom.xml中引入PageHelper分页插件,具体代码如下:


    
      com.github.pagehelper
      pagehelper
      5.0.0
     

具体的使用方法:然后在控制器类StudentController中使用,在查询前先调用方法PageHelper.startPage(pn, 5);这里的传入参数分别是:页码,以及每页的大小。然后可以使用PageInfo包装查询后的结果,只需要将这个PageInfo交给页面就可以,PageInfo封装了详细的分页信息和查询出来的结果。最后将PageInfo添加到model中,在页面进行显示。(可以参考PageHelper网站有具体的使用方法)。

控制器类中需要使用@Autowired注解进行依赖注入,调用Service层的方法查询学生信息,同样的,Service层使用@Autowired注解自动注入,调用dao层来返回数据。Seivice层代码如下:

package com.atrjxy.crud.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atrjxy.crud.bean.Student;
import com.atrjxy.crud.dao.StudentMapper;

//业务逻辑组件
@Service
public class StudentService {
	
	@Autowired
	StudentMapper studentMapper;

	//查询所有学生信息
	public List getAll() {
		// TODO Auto-generated method stub
		return studentMapper.selectByExampleWithDept(null);
	}

}

查询学生信息的控制器类StudentController具体代码如下:

package com.atrjxy.crud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.atrjxy.crud.bean.Student;
import com.atrjxy.crud.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Controller
public class StudentController {
	
	@Autowired
	StudentService studentService;
	
	//查询学生信息(分页查询)
	@RequestMapping("/stus")
	public String getStus(@RequestParam(value = "pn",defaultValue = "1")Integer pn,Model model) {
		
		//引入PageHelper分页插件
		//在查询前只需要调用,传入参数:页码,以及每页的大小
		PageHelper.startPage(pn, 5);
		
		//startPage后面紧跟的这个查询就是分页查询
		List stus = studentService.getAll();
		
		//使用PageInfo包装查询后的结果,只需要将PageInfo交给页面就可以,PageInfo封装了详细的分页信息和查询出来的结果
		//5:代表显示的页数量
		PageInfo page = new PageInfo(stus,5);
		
		//将PageInfo添加到model中,在页面进行显示
		model.addAttribute("pageInfo", page);
		return "list";
	}
}

这样,后台代码就完成了,接下来搭建bootstrap分页页面。(具体使用方法可以参考bootstrap网站)

显示分页数据的页面list.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>




学生信息
<%
  pageContext.setAttribute("A_PATH",request.getContextPath());
%>






    
    
        
       
          学生信息管理系统
        
        
          
            
             
             
               
             
            
          
       
        
       
          
            
            
          
       
       
       
          
           
            学生信息列表
             
                   
# 姓名 性别 学号 班级 操作
${stu.sId } ${stu.sName } ${stu.sSex } ${stu.sSno } ${stu.deptment.deptName }
当前第${pageInfo.pageNum}页,共${pageInfo.pages}页,共${pageInfo.total}条记录

到此,显示分页查询数据就完成了。

为了实现客户端的无关性,接下来使用ajax让index.jsp页面直接发送ajax请求进行员工分页数据的查询,然后服务器将查询出的数据,以json字符串的格式返回给浏览器。浏览器收到json字符串,可以使用js对json字符串进行解析,使用js通过dom对页面数据进行增删改查。

在使用json字符串返回数据的时候,@ResponseBody注解可以自动将返回对象转换为json字符串,并且需要导入jackson包,所以在pom.xml添加依赖,具体代码如下:


   
	 com.fasterxml.jackson.core
	 jackson-databind
	 2.8.8
   

为了进行增删改查后是否操作成功,定义一个json通用的返回的类名称为Msg,包含状态码,提示信息以及用户返回给浏览器的数据,并且含有它们的get和set方法,处理成功和处理失败的方法,以及add方法(主要用于添加pageInfo分页对象)。然后在StudentController控制器中,对查询学生信息的方法getStusWithJson的返回类型为Msg类型。类Msg的代码如下:

package com.atrjxy.crud.bean;

import java.util.HashMap;
import java.util.Map;

//json通用的返回的类
public class Msg {
	//状态码 例如100-成功 200-失败
	private int code;
	
	//提示信息
	private String msg;
	
	//用户返回给浏览器的数据
	private Map extend = new HashMap();
	
	public static Msg success() {
		Msg result = new Msg();
		result.setCode(100);
		result.setMsg("处理成功");
		return result;
	}
	
	public static Msg fail() {
		Msg result = new Msg();
		result.setCode(200);
		result.setMsg("处理失败");
		return result;
	}
	
	public Msg add(String key,Object value) {
		this.getExtend().put(key, value);
		return this;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Map getExtend() {
		return extend;
	}

	public void setExtend(Map extend) {
		this.extend = extend;
	}
	
	
}

控制器类StudentCotroller相关代码如下:

//@ResponseBody注解可以自动将返回对象转换为json字符串,并且需要导入jackson包
	@RequestMapping("/stus")
	@ResponseBody
	public Msg getStusWithJson(@RequestParam(value = "pn",defaultValue = "1")Integer pn) {
		//引入PageHelper分页插件
				//在查询前只需要调用,传入参数:页码,以及每页的大小
				PageHelper.startPage(pn, 5);
				
				//startPage后面紧跟的这个查询就是分页查询
				List stus = studentService.getAll();
				
				//使用PageInfo包装查询后的结果,只需要将PageInfo交给页面就可以,PageInfo封装了详细的分页信息和查询出来的结果
				//5:代表显示的每页大小
				PageInfo page = new PageInfo(stus,5);
				return Msg.success().add("pageInfo", page);
	}

接下来使用ajax请求,收到的结果用js解析出来,使DOM增删改将数据显示出来。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/952727.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号