1.准备
jdk版本 8
mybatis-plus 版本3.3.2
1.相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mp的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>
<!--引入定时器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<!--swagger的依赖引入-->
<dependency>
<groupId>io.github.jianzhichun</groupId>
<artifactId>spring-boot-starter-swagger2</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
2.表
student表
/*
Navicat Premium Data Transfer
Source Server : wzy
Source Server Type : MySQL
Source Server Version : 80025
Source Host : localhost:3306
Source Schema : diyige
Target Server Type : MySQL
Target Server Version : 80025
File Encoding : 65001
Date: 22/08/2023 11:19:12
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(0) UNSIGNED NOT NULL AUTO_INCREMENT,
`picture` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`age` int(0) NULL DEFAULT NULL,
`sex` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`birthday` date NULL DEFAULT NULL,
`clazz_id` int(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 17 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (2, 'http://wzy165.oss-cn-qingdao.aliyuncs.com/3b8119f5-ab97-48e9-9935-7b3b862d4e23336c1003e99856b5603c209ea2dafa2.jpg', '崔3', 21, '女', '2000-03-23', 1);
INSERT INTO `student` VALUES (3, 'http://wzy165.oss-cn-qingdao.aliyuncs.com/3f2cb7e4-def5-495f-9bea-98237befaeeed1389a5189e5faf74b4cce60369cc34.jpg', '崔2', 21, '女', '2000-04-23', 3);
INSERT INTO `student` VALUES (4, 'http://wzy165.oss-cn-qingdao.aliyuncs.com/cb18b075-404e-4915-b20f-a548268c135e9ae2d72007bae3af25a5bcd3574dd44.jpg', '崔三', 18, '女', '2023-05-03', 3);
INSERT INTO `student` VALUES (5, 'http://wzy165.oss-cn-qingdao.aliyuncs.com/34f60cf2-53d8-45b3-92d0-a9b738562c90meinv.jpg', '王', 20, '男', '2023-05-14', 1);
INSERT INTO `student` VALUES (7, 'http://wzy165.oss-cn-qingdao.aliyuncs.com/0a4345c0-9d4c-4a2c-abef-ba4c7c66f78bd1389a5189e5faf74b4cce60369cc34.jpg', '崔', 18, '男', '2002-04-15', 2);
INSERT INTO `student` VALUES (8, 'http://wzy165.oss-cn-qingdao.aliyuncs.com/baf201cb-073e-492c-9dd9-e38af978996a336c1003e99856b5603c209ea2dafa2.jpg', '崔3', 18, '男', '2023-05-14', 2);
INSERT INTO `student` VALUES (10, 'http://wzy165.oss-cn-qingdao.aliyuncs.com/5f04780f-79f1-4e0f-808a-89ac2665a391336c1003e99856b5603c209ea2dafa2.jpg', '张3', 18, '男', '2023-05-15', 1);
INSERT INTO `student` VALUES (15, 'aaa.jsp', '崔', 20, '男', '2000-05-30', 2);
INSERT INTO `student` VALUES (18, NULL, '于清晨', 20, '女', '2023-08-29', 1);
SET FOREIGN_KEY_CHECKS = 1;
clazz表
/*
Navicat Premium Data Transfer
Source Server : wzy
Source Server Type : MySQL
Source Server Version : 80025
Source Host : localhost:3306
Source Schema : diyige
Target Server Type : MySQL
Target Server Version : 80025
File Encoding : 65001
Date: 22/08/2023 11:19:22
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for clazz
-- ----------------------------
DROP TABLE IF EXISTS `clazz`;
CREATE TABLE `clazz` (
`cid` int(0) UNSIGNED NOT NULL AUTO_INCREMENT,
`clazz_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`cid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of clazz
-- ----------------------------
INSERT INTO `clazz` VALUES (1, 'java');
INSERT INTO `clazz` VALUES (2, 'c');
INSERT INTO `clazz` VALUES (3, 'c++');
SET FOREIGN_KEY_CHECKS = 1;
3.静态资源
2.生成实体类
student 实体类
package com.wzy.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "学生类")
public class Student {
@TableId(type = IdType.AUTO)
@ApiModelProperty(value = "学生编号")
private Integer id;
@ApiModelProperty(value = "学生头像")
private String picture="http://wzy165.oss-cn-qingdao.aliyuncs.com/cb18b075-404e-4915-b20f-a548268c135e9ae2d72007bae3af25a5bcd3574dd44.jpg";
@ApiModelProperty(value = "学生姓名")
private String name;
@ApiModelProperty(value = "学生年龄")
private Integer age;
@ApiModelProperty(value = "学生性别")
private String sex;
@ApiModelProperty(value = "学生生日")
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
private Date birthday;
@ApiModelProperty(value = "对应的班级id")
@TableField(value = "clazz_id")
private Integer clazzId;
@ApiModelProperty(value = "班级类")
@TableField(exist = false)
private Clazz clazz;
}
clazz实体类
package com.wzy.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "班级类")
public class Clazz {
@TableId(type = IdType.AUTO)
@ApiModelProperty(value = "班级编号")
private Integer cid;
@ApiModelProperty(value = "班级名称")
@TableField(value = "clazz_name")
private String clazzName;
}
3.dao接口
StudentDao
package com.wzy.dao;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wzy.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface StudentDao extends BaseMapper<Student> {
IPage<Student> selectPageWithClazz(IPage<Student> page, @Param("ew") Wrapper<Student> queryWrapper);
}
ClazzDao
public interface ClazzDao extends BaseMapper<Clazz> {
}
4.mappr映射
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wzy.dao.StudentDao">
<resultMap id="student" type="com.wzy.entity.Student" autoMapping="true">
<id property="id" column="id"/>
<result property="clazzId" column="clazz_id"/>
<association property="clazz" javaType="com.wzy.entity.Clazz" autoMapping="true">
<id property="cid" column="cid"/>
<result property="clazzName" column="clazz_name"/>
</association>
</resultMap>
<select id="selectPageWithClazz" resultMap="student">
select * from student s join clazz c on s.clazz_id=c.cid
<if test="ew!=null and ew.customSqlSegment!=null">
${ew.customSqlSegment}
</if>
</select>
</mapper>
4.service
studentService
package com.wzy.service;
import com.baomidou.mybatisplus.extension.api.R;
import com.wzy.entity.Student;
import com.wzy.vo.Result;
import com.wzy.vo.StudentVo;
public interface StudentService {
public Result selectByPage(Integer current, Integer pageSize, StudentVo studentVo);
public Result insert(Student student);
public Result updeteById(Student student);
public Result deleteById(Integer id);
}
ClazzService
package com.wzy.service;
import com.wzy.vo.Result;
public interface ClazzService {
public Result selectFindAll();
}
4.2impl
StudentServiceImpl
package com.wzy.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzy.dao.StudentDao;
import com.wzy.entity.Student;
import com.wzy.service.StudentService;
import com.wzy.vo.Result;
import com.wzy.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Objects;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public Result selectByPage(Integer current, Integer pageSize, StudentVo studentVo) {
Page<Student> page = new Page<>(current,pageSize);
QueryWrapper<Student> wrapper = new QueryWrapper<>();
if (StringUtils.hasText(studentVo.getName())){
wrapper.like("name",studentVo.getName());
}
if (Objects.nonNull(studentVo.getStartDate())){
wrapper.ge("birthday",studentVo.getStartDate());
}
if (Objects.nonNull(studentVo.getEndDate())){
wrapper.le("birthday",studentVo.getEndDate());
}
IPage<Student> studentIPage = studentDao.selectPageWithClazz(page, wrapper);
return new Result(200,"查询成功",studentIPage);
}
@Override
public Result insert(Student student) {
int insert = studentDao.insert(student);
return insert>0?new Result(200,"添加成功",null):new Result(500,"添加失败",null);
}
@Override
public Result updeteById(Student student) {
int i = studentDao.updateById(student);
return i>0?new Result(200,"修改成功",null):new Result(500,"修改失败",null);
}
@Override
public Result deleteById(Integer id) {
int deleteById = studentDao.deleteById(id);
return deleteById>0?new Result(200,"删除成功",null):new Result(500,"修改失败",null);
}
}
ClazzServiceImpl
package com.wzy.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzy.dao.ClazzDao;
import com.wzy.entity.Clazz;
import com.wzy.service.ClazzService;
import com.wzy.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ClazzServiceImpl implements ClazzService {
@Autowired
private ClazzDao clazzDao;
@Override
public Result selectFindAll() {
QueryWrapper<Clazz> wrapper = new QueryWrapper<>();
List<Clazz> clazzes = clazzDao.selectList(wrapper);
return new Result(200,"查询成功",clazzes);
}
}
5.controller
package com.wzy.controller;
import com.baomidou.mybatisplus.extension.api.R;
import com.wzy.entity.Student;
import com.wzy.service.ClazzService;
import com.wzy.service.StudentService;
import com.wzy.vo.Result;
import com.wzy.vo.StudentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/student")
@Api(tags="学生管理")
public class StudentController {
@Autowired
private StudentService studentService;
@Autowired
private ClazzService clazzService;
@ApiOperation("分页指定查询学生接口")
/* @ApiImplicitParams({
@ApiImplicitParam(name = "current", value = "当前页数",
required = true, dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "pageSize", value = "每页有多少条数据",
required = true, dataType = "int", paramType = "query"),
})*/
@PostMapping("/listByPage/{current}/{pageSize}")
public Result istByPage(@PathVariable Integer current,@PathVariable Integer pageSize, @RequestBody StudentVo studentVo){
return studentService.selectByPage(current,pageSize,studentVo);
}
@ApiOperation("修改学生接口")
@PostMapping("insert")
public Result insert(@RequestBody Student student){
return studentService.insert(student);
}
@ApiOperation("修改学生接口")
@PutMapping
public Result updateById(@RequestBody Student student){
return studentService.updeteById(student);
}
@ApiOperation("删除学生接口")
@ApiImplicitParams(
@ApiImplicitParam(name = "id",value = "学生编号",required = true,defaultValue = "1",dataType = "int",paramType = "query")
)
@DeleteMapping
public Result deleteById(Integer id){
return studentService.deleteById(id);
}
@GetMapping
public Result select(){
return clazzService.selectFindAll();
}
}
6.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生信息展示</title>
<script src="/js/vue.js"></script>
<script src="/js/axios.min.js"></script>
<script src="/js/index.js"></script>
<link rel="stylesheet" href="/css/index.css">
<style>
.el-form-item__label {
text-align: right;
vertical-align: middle;
float: left;
font-size: 12px;
color: #606266;
line-height: 40px;
padding: 0 12px 0 0;
box-sizing: border-box;
}
</style>
</head>
<body>
<div id="app">
<!--修改对话框的开始-->
<el-dialog
title="提示"
:visible.sync="updateVisible"
width="50%">
<el-form ref="form" :model="updateSecForm" :rules="sectionFormRule" label-width="80px">
<el-form-item label="请输入姓名" prop="">
<el-input v-model="updateSecForm.name" placeholder="请输入姓名"></el-input>
</el-form-item>
<el-form-item label="请输入年龄" prop="">
<el-input v-model="updateSecForm.age" placeholder="请输入年龄"></el-input>
</el-form-item>
<el-form-item label="请输入性别" prop="">
<el-input v-model="updateSecForm.sex" placeholder="请输入性别"></el-input>
</el-form-item>
<el-form-item label="出生日期" prop="">
<el-date-picker
v-model="updateSecForm.birthday"
type="date"
placeholder="请选择出生日期">
</el-date-picker>
</el-form-item>
<el-form-item label="请选择班级" prop="">
<el-select v-model="updateSecForm.clazzId" placeholder="请选择班级" style="width: 100%">
<el-option v-for="c in clazz " :label="c.clazzName" :value="c.cid"></el-option>
</el-select>
</el-form-item>
</el-form>
<el-button @click="updateVisible = false">取 消</el-button>
<el-button type="primary" @click="update">确 定</el-button>
</span>
</el-dialog>
<!--添加对话框的开始-->
<el-dialog
title="提示"
:visible.sync="dialogVisible"
width="50%">
<el-form ref="form" :model="addSecForm" :rules="sectionFormRule" label-width="80px">
<el-form-item label="请输入姓名" prop="">
<el-input v-model="addSecForm.name" placeholder="请输入姓名"></el-input>
</el-form-item>
<el-form-item label="请输入年龄" prop="">
<el-input v-model="addSecForm.age" placeholder="请输入年龄"></el-input>
</el-form-item>
<el-form-item label="请输入性别" prop="">
<el-input v-model="addSecForm.sex" placeholder="请输入性别"></el-input>
</el-form-item>
<el-form-item label="出生日期" prop="">
<el-date-picker
v-model="addSecForm.birthday"
type="date"
placeholder="请选择出生日期">
</el-date-picker>
</el-form-item>
<el-form-item label="请选择班级" prop="">
<el-select v-model="addSecForm.clazzId" placeholder="请选择班级" style="width: 100%">
<el-option v-for="c in clazz " :label="c.clazzName" :value="c.cid"></el-option>
</el-select>
</el-form-item>
</el-form>
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="insert">确 定</el-button>
</span>
</el-dialog>
<el-form :inline="true" :model="studentSearch" class="demo-form-inline" style="height:100px" >
<el-date-picker
v-model="studentSearch.startDate"
type="date"
placeholder="开始日期">
</el-date-picker>
<el-date-picker
v-model="studentSearch.endDate"
type="date"
placeholder="结束日期">
</el-date-picker>
<el-input v-model="studentSearch.name" placeholder="请输入姓名" style="width: 100px"></el-input>
<el-button type="primary" @click="search">查询</el-button>
<el-button type="primary" @click="resetSearchForm">重置</el-button>
<el-button type="success" @click="dialogVisible = true">添加</el-button>
</el-form-item>
</el-form>
<el-table
:data="student"
border
style="width: 100%">
<el-table-column
label="序号"
type="index"
width="130"
align="center">
</el-table-column>
<el-table-column
label="头像"
align="center">
<template slot-scope="scope">
<img :src="scope.row.picture" width="100px" height="80px"/>
</template>
</el-table-column>
<el-table-column
prop="name"
label="姓名"
width="110"
align="center">
</el-table-column>
<el-table-column
prop="age"
label="年龄"
width="130"
align="center">
</el-table-column>
<el-table-column
prop="sex"
label="性别"
width="130"
align="center">
</el-table-column>
<el-table-column
prop="birthday"
label="出生日期"
width="130"
align="center">
</el-table-column>
<el-table-column
prop="clazz.clazzName"
label="所在班级"
width="130"
align="center">
</el-table-column>
<el-table-column
label="操作"
width="220"
align="center">
<template slot-scope="scope">
<el-button type="primary" icon="el-icon-setting" size="small" @click="editSec(scope.row)" >编辑</el-button>
<el-button type="danger" icon="el-icon-delete" size="small" @click="editSection(scope.row.id)" >删除
</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="current"
:page-sizes="[5, 10, 15, 20]"
:page-size="pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="total">
</el-pagination>
</div>
</body>
<script>
let app=new Vue({
el:"#app",
data:{
updateSecForm:{},
sectionFormRule:{},
updateVisible:false,
clazz:[],
/*添加对话框*/
addSecForm:{},
dialogVisible: false,
studentSearch:{},
student:[],
//当前页
current:1,
//当前每页显示的条数
pageSize:5,
/*数据总条数*/
total: 0,
},
created(){
this.studentFindAll();
this.selectClazz();
},
methods:{
//删除事件
editSection(id) {
//提示框
this.$confirm('确认删出商品?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'info',
}).then(() => {
let that = this;
axios.delete("/student?id="+id).then(function (result) {
if (result.data.code == 200) {
//1.弹出一个信息
that.$message.success(result.data.msg);
//2.重新加载界面
that.studentFindAll();
} else {
//1.弹出一个信息
that.$message.error(result.data.msg);
}
})
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
},
//修改学生信息
update(){
let that=this
axios.put("/student",this.updateSecForm).then(result=>{
if (result.data.code==200){
that.$message.success(result.data.msg);
that.updateVisible=false;
that.studentFindAll();
}else {
that.$message.error(result.data.msg)
}
})
},
//编辑按钮事件
editSec(row){
this.updateVisible=true;
//当前行赋值给修改表单对象
this.updateSecForm=JSON.parse(JSON.stringify(row));
},
//确认添加学生方法
insert() {
let that = this;
axios.post("/student/insert", this.addSecForm).then(result => {
if (result.data.code == 200) {
that.$message.success(result.data.msg);
that.addSecForm = {};
that.dialogVisible = false;
that.studentFindAll();
} else {
that.$message.error(result.data.msg)
}
})
},
//重置方法
resetSearchForm() {
this.currentPage = 1
this.studentSearch = {}
this.studentFindAll();
},
//查询方法
search() {
this.currentPage = 1;
this.studentFindAll();
},
//每页显示的条数触发时的事件
handleSizeChange(val) {
this.pageSize = val;
this.studentFindAll();
},
//当前页码发生改变时触发的事件
handleCurrentChange(val) {
this.current = val;
this.studentFindAll();
},
//查询所有班级信息
selectClazz(){
axios.get("/student").then(result=>{
this.clazz=result.data.data
})
},
//查询所有学生信息
studentFindAll(){
axios.post("/student/listByPage/"+this.current+"/"+this.pageSize,this.studentSearch).then(result=>{
this.student=result.data.data.records;
this.total=result.data.data.total;
console.log(result)
})
}
}
})
</script>
</html>