graduation Design.md 7.4 KB

项目开始

整体思路

1.设计SQL

2.开发后台

  1. 开发业务功能方法 List selectAll();
  2. 开发业务功能方法 List selectAll();
  3. 开发数据库接口 List selectAll();
  4. sql:** select * from user 查询用户信息**

3.前台页面

  1. 搜索框查询
  2. 操作按钮:新增、批量删除、编辑、删除
  3. 数据渲染 Table + 分页
  4. Form表单

学习记录

6.1 模块1-开发用户管理功能

导入脚手架

修改配置文件后在浏览器验证

image-20240601203116396

新建MySQL表

管理员表:

image-20240601203227510

用户表:

image-20240601203251242

信息表:

image-20240601203318470

创建User实体类:

package com.example.entity;

import java.math.BigDecimal;

public class User extends Account{
    /** ID */
    private Integer id;
    /** 用户名 */
    private String username;
    /** 密码 */
    private String password;
    /** 姓名 */
    private String name;
    /** 电话 */
    private String phone;
    /** 邮箱 */
    private String email;
    /** 头像 */
    private String avatar;
    /** 角色标识 */
    private String role;
    /** 性别 */
    private String sex;
    /** 账户余额 */
    private BigDecimal account;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public BigDecimal getAccount() {
        return account;
    }

    public void setAccount(BigDecimal account) {
        this.account = account;
    }
}

创建UserController:

package com.example.controller;

import com.example.common.Result;
import com.example.entity.Admin;
import com.example.entity.User;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户的http接口
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {

        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {

        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody User user) {

        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {

        return Result.success();
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(User user) {

        return Result.success();
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(User user,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {

        return Result.success();
    }
}

创建UserService:

package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.Constants;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Admin;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户业务处理
 **/
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public void add(User user) {
        userMapper.insert(user);
    }



}

创建UserMapper.xml

<?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.example.mapper.UserMapper">

<insert id="insert" parameterType="com.example.entity.User" useGeneratedKeys="true">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
         <if test="id != null">id,</if>
         <if test="username != null">username,</if>
         <if test="password != null">password,</if>
         <if test="name != null">name,</if>
         <if test="avatar != null">avatar,</if>
         <if test="role != null">role,</if>
         <if test="sex != null">sex,</if>
         <if test="phone != null">phone,</if>
         <if test="account != null">account,</if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="id != null">#{id},</if>
        <if test="username != null">#{username},</if>
        <if test="password != null">#{password},</if>
        <if test="name != null">#{name},</if>
        <if test="avatar != null">#{avatar},</if>
        <if test="role != null">#{role},</if>
        <if test="sex != null">#{sex},</if>
        <if test="phone != null">#{phone},</if>
        <if test="account != null">#{account},</if>
    </trim>

动态sql:此段代码用于防止数据提交不完整导致下面语句自动赋null值从而使预设的默认值失效!

insert into user (username,password,name,avatar,role,sex,phone,account)values
(#{username},#{password},#{name},#{avatar},#{role},#{sex},#{phone},#{account})
</insert>
</mapper>

模块2:开发后台用户管理功能

测试上述接口:

image-20240601204005887

使用admin账号获取token,使用获取的token新建一个用户测试:

image-20240601204106952

完善删除接口并测试:

image-20240601205841057

批量删除:image-20240601211255269