<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<!-- 前端总控制器 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- ioc配置文件的位置 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!-- url :/默认处理servlet -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
package demo.dao;
import demo.bean.Course;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
import java.util.List;
/***
* 持久层
*/
@Repository
public class CourseDao {
@Autowired
QueryRunner queryRunner;
/***
* 查询显示所有课程
* @return hengqirong
* @throws SQLException
*/
public List<Course> Select() throws SQLException{
String sql="select * from course";
return queryRunner.query(sql,new BeanListHandler<Course>(Course.class));
}
/***
* 删除课程
* @param cid
* @return
* @throws SQLException
*/
public int delete(int cid)throws SQLException{
String sql="delete from course where cid=?";
return queryRunner.update(sql,cid);
}
/***
* 添加课程
* @param cname
* @return
* @throws SQLException
*/
public int add(String cname) throws SQLException{
String sql="INSERT INTO course (cname) VALUES (?)";
return queryRunner.update(sql,cname);
}
/****
* 根据id查询进行修改
* @param cid
* @return
* @throws SQLException
*/
public Course selectByCid(int cid) throws SQLException{
String sql="select * from course where cid=?";
return queryRunner.query(sql,new BeanHandler<Course>(Course.class),cid);
}
/***
* 课程修改
* @param cname
* @param cid
* @return
* @throws SQLException
*/
public int update(String cname,int cid)throws SQLException{
String sql="UPDATE course SET cname=? WHERE cid=?";
return queryRunner.update(sql,cname,cid);
}
}
package demo.dao;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
@Repository
public class ScoreDao {
@Autowired
QueryRunner queryRunner;
public void deleteScoreByCid(int cid) throws SQLException {
String sql="delete from score where cid =?";
queryRunner.update(sql,cid);
}
}
package demo.service;
import demo.bean.Course;
import demo.dao.CourseDao;
import demo.dao.ScoreDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.List;
/***
* 业务层 用来编写操作 业务逻辑功能实现
*/
@Service
public class CourseService {
@Autowired
CourseDao courseDao;
@Autowired
ScoreDao scoreDao;
/***
* 查询列表显示课程
* @return
* @throws SQLException
*/
public List<Course> Select() throws SQLException {
return courseDao.Select();
}
/***
* 删除课程
* @param cid 课程id
* @return
* @throws SQLException
*/
public int delete(String cid)throws SQLException{
int i = courseDao.delete(Integer.parseInt(cid));
if(i>0){
scoreDao.deleteScoreByCid(Integer.parseInt(cid));
}
return i;
}
/***
* 添加课程
* @param cname
* @return
* @throws SQLException
*/
public int add(String cname)throws SQLException{
return courseDao.add(cname);
}
/***
* 根据id查询课程
* @param cid
* @return
* @throws SQLException
*/
public Course selectByCid(String cid)throws SQLException{
return courseDao.selectByCid(Integer.parseInt(cid));
}
/***
* 修改课程
* @param cid
* @param cname
* @return
* @throws SQLException
*/
public int update( String cname,String cid)throws SQLException{
return courseDao.update(cname,Integer.parseInt(cid));
}
}
package demo.controller;
import demo.bean.Course;
import demo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.SQLException;
import java.util.List;
/***
* 控制层 : 处理请求 返回参数 获取参数 传参
*/
@RestController
@RequestMapping("/CourseController")
public class CourseController {
@Autowired
CourseService courseService;
@RequestMapping("Select")
public List<Course> Select() throws SQLException {
return courseService.Select();
}
@RequestMapping("delete")
public int delete(String cid) throws SQLException {
int result =courseService.delete(cid);
return result;
}
@RequestMapping("add")
public int add(String cname)throws SQLException{
int result=courseService.add(cname);
return result;
}
@RequestMapping("selectByCid")
public Course selectByCid(String cid)throws SQLException{
return courseService.selectByCid(cid);
}
@RequestMapping("update")
public int update(String cname, String cid)throws SQLException{
return courseService.update(cname,cid);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.3.1.min.js"></script>
</head>
<body>
添加课程名称:<input type="text" name="cname">
<button onclick="add()" >添加课程</button>
<button onclick="update()" name="cid">修改课程</button>
<table class="courseTab">
<tr>
<td>课程编号</td>
<td>课程名称</td>
<td>操作</td>
</tr>
</table>
</body>
</html>
<script>
//课程查询
$.ajax({
"url":"http://localhost/CourseController/Select",
"data":"",
"success":function(msg){
$.each(msg,function(i,n){
var tr=` <tr><td>${n.cid}</td>
<td>${n.cname}</td>
<td><button onclick="delCourse(${n.cid})" >删除课程</button><button onclick="selectcid(${n.cid})" >修改课程</button></td></tr>`
$(".courseTab").append(tr);
});
}
})
//删除课程
function delCourse(cid){
$.ajax({
"url":"http://localhost/CourseController/delete",
"data":{
"cid":cid
},
"success":function(msg){
if(msg>0){
alert("删除成功!");
}
else{
alert("删除失败!");
}
}
})
}
//添加课程
function add(){
$.ajax({
"url":"http://localhost/CourseController/add",
"data":{
"cname":$("input[name=cname]").val()
},
"success":function(msg){
if(msg>0){
alert("添加成功!");
}
else{
alert("添加失败!");
}
}
})
}
//根据要修改的课程cid进行查询填充
function selectcid(cid){
$.ajax({
"url":"http://localhost/CourseController/selectByCid",
"data":{
"cid":cid
},
"success":function(msg){
$("input[name=cname]").val(msg.cname);
$("button[name=cid]").val(msg.cid);
}
})
}
//修改课程
function update(){
$.ajax({
"url":"http://localhost/CourseController/update",
"data":{
"cname":$("input[name=cname]").val(),
"cid": $("button[name=cid]").val()
},
"success":function(msg){
if(msg>0){
alert("修改成功!");
$("input[name=cname]").val("");
}
else{
alert("修改失败!");
}
}
})
}
</script>
/*
Navicat Premium Data Transfer
Source Server : demo
Source Server Type : MySQL
Source Server Version : 80013
Source Host : localhost:3306
Source Schema : demo
Target Server Type : MySQL
Target Server Version : 80013
File Encoding : 65001
Date: 19/04/2022 15:55:33
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cid` int(11) NOT NULL AUTO_INCREMENT COMMENT '课程主键',
`cname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '课程名称',
PRIMARY KEY (`cid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES (1, 'JAVA');
INSERT INTO `course` VALUES (2, 'C语言');
SET FOREIGN_KEY_CHECKS = 1;
/*
Navicat Premium Data Transfer
Source Server : demo
Source Server Type : MySQL
Source Server Version : 80013
Source Host : localhost:3306
Source Schema : demo
Target Server Type : MySQL
Target Server Version : 80013
File Encoding : 65001
Date: 19/04/2022 15:55:40
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`sid` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`score` int(255) NULL DEFAULT NULL COMMENT '分数',
`cid` int(11) NULL DEFAULT NULL COMMENT '课程表关联键',
`sname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '学生名称',
PRIMARY KEY (`sid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES (1, 50, 1, '张三');
INSERT INTO `score` VALUES (2, 60, 2, '李四');
INSERT INTO `score` VALUES (3, 70, 1, '王五');
SET FOREIGN_KEY_CHECKS = 1;
以上我们用的springmvc三层架构思想(控制层Controller,业务层Service,持久层dao): MVC架构分层的主要作用是解耦。采用分层架构的好处,普遍接受的是系统分层有利于系统的维护,系统的扩展。增强系统的可维护性。
复制以上代码可以直接使用。