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

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

首页部分成绩管理页面(查询/添加功能)制作(在上篇博客基础上继续)

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

新建 courseManger.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 课程名称 操作
1 java 编辑 删除
2 PHP 编辑 删除
3 C++ 编辑 删除
4 C# 编辑 删除
<
1
>
4
20

新建 CourseController.java

package com.neuedu.controller;

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

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

@Controller
@RequestMapping("/course")
public class CourseController {
	@Autowired
	private CourseService courseService;
	
	@RequestMapping({"/","/list"})
	public String 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/";
	}
}

修改 index.jsp

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


	
		
		
		
	
	
		 
		
		
	

这节功能中的 添加 页面单独写  新建:courseAdd.jsp

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


	
		
		课程添加
	
	
		
课程名称:
课程学时:
课程学分:

新建接口 TbCourseMapper.java

package com.neuedu.mapper;

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);
}

配置文件 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 com.neuedu.po.TbCourse;

public interface CourseService {
	void save(TbCourse tbCourse);
}

配置文件 CourseServiceImpl.java

package com.neuedu.service.impl;

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

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);
	}

}

输出:点击 课程管理 --进入课程管理页面--点击 添加 ---进入添加页面---输入信息--点击提交---信息保存至数据库
初识 Spring(18)---(SpringMVC实战--构建学生管理系统(08))_第1张图片

初识 Spring(18)---(SpringMVC实战--构建学生管理系统(08))_第2张图片

问题是,数据库与页面没有进行绑定,页面是写死的,没办法从数据库获取并展示信息,继续修改代码

绑定数据库,使数据库中的信息显示在页面上

修改接口 TbCourseMapper.java

package com.neuedu.mapper;

import java.util.List;

import com.neuedu.po.TbCourse;

public interface TbCourseMapper {
    int deleteByPrim aryKey(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();  //新增代码
}

修改配置文件 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);
	
	List getList();   //新增代码
}

修改配置文件 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.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() {
		
		return courseMapper.getList();
	}                                         //新增代码

}

修改 CourseController.java

package com.neuedu.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 com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;

@Controller
@RequestMapping("/course")
public class CourseController {
	@Autowired
	private CourseService courseService;
	
	@RequestMapping({"/","/list"})                     //新增代码
	public String list(Model model) { 
		//查询课程列表
		List list = courseService.getList();
		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/";
	}
}

修改 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 } 编辑 删除
<
1
>
4
20

输出:点击 课程管理 --进入课程管理页面--保存至数据库的信息已添加到页面

 

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