初识 Spring(20)---(SpringMVC实战--构建学生管理系统(10))

SpringMVC实战--构建学生管理系统(10)

首页部分课程管理页面(分页功能)制作(在上篇博客基础上继续)

(源代码见仓库:https://gitee.com/jianghao233/course)

思路:

1.前台可以给后台传递的数据:1.当前页   2.每页显示多少条记录

2.SQL分页语句:select * from tb_course LIMIT A,B   (从A开始,显示B条)

制作分页:前端传递的是当前页,但后台需要的是从那一条数据开始查询 == (当前页数-1 *每页显示条数B)

录入信息:

初识 Spring(20)---(SpringMVC实战--构建学生管理系统(10))_第1张图片

制作分页功能:

修改代码:

新建Commons.java

package com.neuedu.commons;

public interface Commons {
//定义静态常量
	//每页显示条数
	public static final int RECORD = 5;
}

修改 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


	
		
		
		
	
	
		 
		

修改 CourseService.java

package com.neuedu.service;

import java.util.List;

import com.neuedu.po.TbCourse;

public interface CourseService {
	void save(TbCourse tbCourse);
	TbCourse getCourseById(Integer courseid);    //修改代码
	List getList(Integer nowPage);     //修改代码   
	
	void update(TbCourse tbCourse);
	void delete(Integer courseid);
}
 

修改CourseServiceImpl.java

package com.neuedu.service.impl;

import java.util.List;

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

import com.neuedu.commons.Commons;
import com.neuedu.mapper.TbCourseMapper;
import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;
@Service
public class CourseServiceImpl implements CourseService {
	
	@Autowired
	private TbCourseMapper courseMapper;
	@Override
	public void save(TbCourse tbCourse) {
		courseMapper.insertSelective(tbCourse);
	}
	@Override
	public List getList(Integer nowPage) {    //修改代码
		//获取每页显示条数
		int record = Commons.RECORD;                   //新增代码
		//通过计算得出从第几条开始查询
		int start = (nowPage - 1) * record;
		return courseMapper.getList(start,record);     //新增代码
	}
	@Override
	public TbCourse getCourseById(Integer courseid) {
	
		return courseMapper.selectByPrimaryKey(courseid);
	}
	@Override
	public void update(TbCourse tbCourse) {
		courseMapper.updateByPrimaryKeySelective(tbCourse);
		
	}
	@Override
	public void delete(Integer courseid) {
		courseMapper.deleteByPrimaryKey(courseid);
		
	}

}

修改TbCourseMapper.java

package com.neuedu.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.neuedu.po.TbCourse;

public interface TbCourseMapper {
    int deleteByPrimaryKey(Integer courseid);

    int insert(TbCourse record);

    int insertSelective(TbCourse record);

    TbCourse selectByPrimaryKey(Integer courseid);

    int updateByPrimaryKeySelective(TbCourse record);

    int updateByPrimaryKey(TbCourse record);
    
    public List getList(@Param("start") int start,@Param("record")int record);

	//修改代码
}

修改TbCourseMapper.xml




  
    
    
    
    
    
  
  
    courseid, coursename, hour, score, picurl
  
  
  
    delete from tb_course
    where courseid = #{courseid,jdbcType=INTEGER}
  
  
    insert into tb_course (courseid, coursename, hour, 
      score, picurl)
    values (#{courseid,jdbcType=INTEGER}, #{coursename,jdbcType=VARCHAR}, #{hour,jdbcType=INTEGER}, 
      #{score,jdbcType=DOUBLE}, #{picurl,jdbcType=VARCHAR})
  
  
    insert into tb_course
    
      
        courseid,
      
      
        coursename,
      
      
        hour,
      
      
        score,
      
      
        picurl,
      
    
    
      
        #{courseid,jdbcType=INTEGER},
      
      
        #{coursename,jdbcType=VARCHAR},
      
      
        #{hour,jdbcType=INTEGER},
      
      
        #{score,jdbcType=DOUBLE},
      
      
        #{picurl,jdbcType=VARCHAR},
      
    
  
  
    update tb_course
    
      
        coursename = #{coursename,jdbcType=VARCHAR},
      
      
        hour = #{hour,jdbcType=INTEGER},
      
      
        score = #{score,jdbcType=DOUBLE},
      
      
        picurl = #{picurl,jdbcType=VARCHAR},
      
    
    where courseid = #{courseid,jdbcType=INTEGER}
  
  
    update tb_course
    set coursename = #{coursename,jdbcType=VARCHAR},
      hour = #{hour,jdbcType=INTEGER},
      score = #{score,jdbcType=DOUBLE},
      picurl = #{picurl,jdbcType=VARCHAR}
    where courseid = #{courseid,jdbcType=INTEGER}
  
  
  
  

修改 CourseController.java

package com.neuedu.controller;

import java.util.List;
import java.util.Map;

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 org.springframework.web.bind.annotation.ResponseBody;


import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;

import com.sun.javafx.css.CssError.InlineStyleParsingError;


@Controller
@RequestMapping("/course")
public class CourseController {
	@Autowired
	private CourseService courseService;
	
	@RequestMapping({"/","/list"})
	public String list(@RequestParam(value="nowPage",required=false,defaultValue="1") Integer nowPage,Model model) {                          
		//查询课程列表 (修改代码)
		List list = courseService.getList(nowPage);   //修改代码
		model.addAttribute("list",list);        
		
		
		return "admin/courseManager";
	}
	//显示添加页面
	@RequestMapping("/showAdd")
	public String showAdd() {
		return "admin/courseAdd";
	}
	//保存课程信息
	@RequestMapping("/save")
	public String save(TbCourse tbCourse){
		//调用service保存课程信息
		courseService.save(tbCourse);
		return "redirect:/course/";
	}
	
	//显示编辑页面
	@RequestMapping("/showModify")
	public String showModify(Integer courseid,Map map) {
		TbCourse tbCourse = courseService.getCourseById(courseid);
		map.put("c", tbCourse);
		return "admin/courseModify";
	}
	
	@RequestMapping("/modify")
	public String modify(TbCourse tbCourse) {
		courseService.update(tbCourse);
		return "redirect:/course/";
		
	}
	
	@RequestMapping("/delete")
	public String delete(Integer courseid) {
		courseService.delete(courseid);
		return "redirect:/course/";
	}
}

输出:在代码中,当前页为 1 ,每页显示 5 条 ,即可得到如图页面
初识 Spring(20)---(SpringMVC实战--构建学生管理系统(10))_第2张图片
但这时目录是写死的,后台也应该向前台传递数据,要能够跳转到第二页去,当前页也要随着修改,继续修改代码

修改代码修改TbCourseMapper.java

package com.neuedu.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.neuedu.po.TbCourse;

public interface TbCourseMapper {
    int deleteByPrimaryKey(Integer courseid);

    int insert(TbCourse record);

    int insertSelective(TbCourse record);

    TbCourse selectByPrimaryKey(Integer courseid);

    int updateByPrimaryKeySelective(TbCourse record);

    int updateByPrimaryKey(TbCourse record);
    
    public List getList(@Param("start") int start,@Param("record")int record);

    public int getCount();  //新增代码
}

修改 修改TbCourseMapper.xml




  
    
    
    
    
    
  
  
    courseid, coursename, hour, score, picurl
  
  
  
    delete from tb_course
    where courseid = #{courseid,jdbcType=INTEGER}
  
  
    insert into tb_course (courseid, coursename, hour, 
      score, picurl)
    values (#{courseid,jdbcType=INTEGER}, #{coursename,jdbcType=VARCHAR}, #{hour,jdbcType=INTEGER}, 
      #{score,jdbcType=DOUBLE}, #{picurl,jdbcType=VARCHAR})
  
  
    insert into tb_course
    
      
        courseid,
      
      
        coursename,
      
      
        hour,
      
      
        score,
      
      
        picurl,
      
    
    
      
        #{courseid,jdbcType=INTEGER},
      
      
        #{coursename,jdbcType=VARCHAR},
      
      
        #{hour,jdbcType=INTEGER},
      
      
        #{score,jdbcType=DOUBLE},
      
      
        #{picurl,jdbcType=VARCHAR},
      
    
  
  
    update tb_course
    
      
        coursename = #{coursename,jdbcType=VARCHAR},
      
      
        hour = #{hour,jdbcType=INTEGER},
      
      
        score = #{score,jdbcType=DOUBLE},
      
      
        picurl = #{picurl,jdbcType=VARCHAR},
      
    
    where courseid = #{courseid,jdbcType=INTEGER}
  
  
    update tb_course
    set coursename = #{coursename,jdbcType=VARCHAR},
      hour = #{hour,jdbcType=INTEGER},
      score = #{score,jdbcType=DOUBLE},
      picurl = #{picurl,jdbcType=VARCHAR}
    where courseid = #{courseid,jdbcType=INTEGER}
  
  
  
  
  
                                      //新增代码

修改 CourseService.java

package com.neuedu.service;

import java.util.List;

import com.neuedu.po.TbCourse;

public interface CourseService {
	void save(TbCourse tbCourse);
	TbCourse getCourseById(Integer courseid);
	List getList(Integer nowPage);
	
	void update(TbCourse tbCourse);
	void delete(Integer courseid);
	
	int getCount();  //新增代码
}
 

修改CourseServiceImpl.java

package com.neuedu.service.impl;

import java.util.List;

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

import com.neuedu.commons.Commons;
import com.neuedu.mapper.TbCourseMapper;
import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;
@Service
public class CourseServiceImpl implements CourseService {
	
	@Autowired
	private TbCourseMapper courseMapper;
	@Override
	public void save(TbCourse tbCourse) {
		courseMapper.insertSelective(tbCourse);
	}
	@Override
	public List getList(Integer nowPage) {
		//获取每页显示条数
		int record = Commons.RECORD;
		//通过计算得出从第几条开始查询
		int start = (nowPage - 1) * record;
		return courseMapper.getList(start,record);
	}
	@Override
	public TbCourse getCourseById(Integer courseid) {
	
		return courseMapper.selectByPrimaryKey(courseid);
	}
	@Override
	public void update(TbCourse tbCourse) {
		courseMapper.updateByPrimaryKeySelective(tbCourse);
		
	}
	@Override
	public void delete(Integer courseid) {
		courseMapper.deleteByPrimaryKey(courseid);
		
	}
	@Override
	public int getCount() {    //新增代码
		
		return courseMapper.getCount();
	}                          //新增代码

}

修改CourseController.java

package com.neuedu.controller;

import java.util.List;
import java.util.Map;

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 org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.commons.Commons;
import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;

import com.sun.javafx.css.CssError.InlineStyleParsingError;


@Controller
@RequestMapping("/course")
public class CourseController {
	@Autowired
	private CourseService courseService;
	
	@RequestMapping({"/","/list"})
	public String list(@RequestParam(value="nowPage",required=false,defaultValue="1") Integer nowPage,Model model) { 
		//查询课程列表
		List list = courseService.getList(nowPage);    
		//总课程数
		int count = courseService.getCount();   //新增代码
		//总页数
		int totalPage = (int)Math.ceil(((double)count)/Commons.RECORD);   //新增代码
		
		model.addAttribute("list",list);                    //新增代码
		model.addAttribute("nowPage",nowPage);
		model.addAttribute("count",count);
		model.addAttribute("totalPage",totalPage);
		return "admin/courseManager";                        //新增代码
	}
	//显示添加页面
	@RequestMapping("/showAdd")
	public String showAdd() {
		return "admin/courseAdd";
	}
	//保存课程信息
	@RequestMapping("/save")
	public String save(TbCourse tbCourse){
		//调用service保存课程信息
		courseService.save(tbCourse);
		return "redirect:/course/";
	}
	
	//显示编辑页面
	@RequestMapping("/showModify")
	public String showModify(Integer courseid,Map map) {
		TbCourse tbCourse = courseService.getCourseById(courseid);
		map.put("c", tbCourse);
		return "admin/courseModify";
	}
	
	@RequestMapping("/modify")
	public String modify(TbCourse tbCourse) {
		courseService.update(tbCourse);
		return "redirect:/course/";
		
	}
	
	@RequestMapping("/delete")
	public String delete(Integer courseid) {
		courseService.delete(courseid);
		return "redirect:/course/";
	}
}

修改 courseManager.jsp

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


	
		
		
		
		
		
		
		
		
	
	
		
课程列表
课程ID 课程名称 课程学时 课程学分 操作
${c.courseid } ${c.coursename } ${c.hour } ${c.score } 编辑 删除
//修改代码 <
${nowPage }
>
${totalPage }
${count }
//修改代码

输出:点击课程管理---进入课程管理页面--点击 》 ---进入第二页  2为总页数   10为总课程数

你可能感兴趣的:(Java,#,Spring,突破Spring框架)