• 基于SSM 实现增删查改


    基于SSM 实现增删查改

    前言 : 本文主主要内容基于SSM 实现简单的用户登录,用户注册,用户注销,和修改用户名和密码,简单来说就是增删查改

    1. 创建springboot项目

    在这里插入图片描述


    这里我们项目就创建完成了, 当时不要着急 我们需要配置一下我们的文件 要不然直接运行会报错

    2. 配置相应配置文件

    在这里插入图片描述


    附上相应配置代码,建议放在 gitee 的代码片段

    在这里插入图片描述


    application.ymlo配置文件

    # 配置当前运行的环境 (配置文件)
    
    # spring > profiles > active
    spring:
      profiles:
        active: dev # 使用开发环境的配置文件
    
    
    
    # 配置 mybatis xml 保存路径
    mybatis:
      mapper-locations: classpath:mybatis/**Mapper.xml
    
    # 在公共yml 文件 来 配置 mybatis 的保存路径
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15


    application-dev.yml 配置文件

    # 开发环境的配置文件
    
    
    spring:
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/mycnblog?characterEncoding=utf8
        username: root
        password: 1234
        driver-class-name: com.mysql.cj.jdbc.Driver # 在 8.0 之前 是没有点 jc的 -> com.mysql.jdbc.Driver
    
    
    
    
    
    
    
    # 设置日志级别
    logging:
      level:
        com:
          example:
            demo: debug
            # 对具体类机型日志级别设定
    
    
    # 开启 MyBatis SQL 打印
    mybatis:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    3. 创建数据库与数据表


    这里如果我们是写项目的化应该是设计数据库,但是这里只是演示最简单的增删查改操作,所以就直接创建一个 用户表即可

    -- 创建数据库
    -- sql 语法 : create database 数据库名
    
    
    create database userinfo;
    
    
    -- 先选中 数据库 在来创建表
    use userinfo;
    -- 创建数据表
    
    -- sql 语法 : create table 表名 (列 类型);
    
    create table user(id int primary key auto_increment , username varchar(50) , password varchar(255));
    
    这里 密码 大小为 255 方便后面我们对密码进行加密
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4. 创建对应的包以及类

    在这里插入图片描述

    这里大致框架就完成了下面来写我们的功能 。

    5 功能

    5.1 注册功能

    图一 :

    在这里插入图片描述

    图二 :

    在这里插入图片描述

    图三 : 演示通过BCrypt 进行密码加密 和校验

    在这里插入图片描述

    补充 : shift + f6 ,上面敲错了名字,所以这里可以通过 shift + f6 快捷键进行修改操作

    在这里插入图片描述


    下面继续 :


    图四 :

    在这里插入图片描述

    图五 :

    在这里插入图片描述

    这里我们的注册功能就完成了,下面来看一下我们的登录功能


    5.2 登录功能

    图一 :

    在这里插入图片描述

    图二:

    在这里插入图片描述

    图三: 验证我们的方法

    在这里插入图片描述

    5.3 修改功能

    图一 :

    在这里插入图片描述

    图二 :

    在这里插入图片描述

    图三 :

    在这里插入图片描述

    图四:

    在这里插入图片描述

    图五:

    在这里插入图片描述

    这里我们的增删查改就完成了 ,布置小作业, 这里修改密码并未完成 另外 还有一个注销功能,你能完成吗?

    最后 附上 代码 :


    Config 中的 App类

    package com.example.demo.Config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class App implements WebMvcConfigurer {
    
        @Bean
        public BCryptPasswordEncoder getBCryptPasswordEncoder() {
            return new BCryptPasswordEncoder();
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new LoginInterceptors())
                    .addPathPatterns("/**") // 表示拦截所有 url
                    .excludePathPatterns("/user/login") // 登录功能不拦截
                    .excludePathPatterns("/user/add")// 注册功能不拦截
                    .excludePathPatterns("/css/**.css")
                    .excludePathPatterns("/js/**.js")
                    .excludePathPatterns("images/**");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28


    Config 中的 LoginInterceptors类

    package com.example.demo.Config;
    
    import com.example.demo.tools.Constant;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.servlet.HandlerInterceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    @Slf4j
    public class LoginInterceptors implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
            // 1. 获取 session对象
            HttpSession sessions = request.getSession(false);
    
            // 2. 获取 user 对象
            if(sessions != null && sessions.getAttribute(Constant.USERINFO_SESSION_KEY) != null){
                // 此时 已经登录
                return  true;
            }
    
    //        此时未登录
    
            log.info("用户未登录");
            // 这里没有写前端的所以就不重定向直接答应一下即可 --> 这里次采用 SLf4j 的日志打印
    
    //        response.sendRedirect("login.html"); --> 通过 response 进行重定向操作
            return false;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34


    controller 中的 UserController类

    package com.example.demo.controller;
    
    
    import com.example.demo.model.User;
    import com.example.demo.service.UserService;
    import com.example.demo.tools.Constant;
    import com.example.demo.tools.ResponseBodyMessage;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.util.StringUtils;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    @RestController
    @RequestMapping(value = "/user")
    public class UserController {
    
    
        @Autowired
        private UserService userService;
    
        @Autowired
        private BCryptPasswordEncoder bCryptPasswordEncoder;
    
        @RequestMapping(value = "/add")
        public ResponseBodyMessage<Boolean> insert(@RequestParam String username, @RequestParam String password) {
            // 注册页面 :
            // 1. 判断用户名是否已经存在 :
            User user = userService.selectByName(username);
            if (user != null) {
                // 此时用户名不为空, 说明 用户名已存在 直接返回
                return new ResponseBodyMessage<>(-1, "注册失败,用户名已存在", false);
            }
    
            // 2. 对密码进行加密操作
    
            password = bCryptPasswordEncoder.encode(password);
    
            // 密码加密后 再数据库新增数据
    
            Integer ret = userService.insert(username, password);
            // 3. 返回相应的信息
    
            if (ret != 1) {
                // 此时 注册失败
                return new ResponseBodyMessage<>(-1, "注册失败", false);
            }
            // 此时 ret == 1 注册成功
            return new ResponseBodyMessage<>(0, "注册成功", true);
    
        }
    
        @RequestMapping(value = "/login")
        public ResponseBodyMessage<User> login(@RequestParam String username, @RequestParam String password
                , HttpServletRequest request) {
            // 1. 校验用户和密码的合理性
    
            if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
                return new ResponseBodyMessage<>(-1, "用户或密码不合理", null);
            }
            // 2. 通过 selectByName 获取 user 对象
            User user = userService.selectByName(username);
            if (user == null) {
                // 此时用户为注册
                return new ResponseBodyMessage<>(-1, "用户未注册", null);
            }
    
            // 3. 校验密码
            boolean flag = bCryptPasswordEncoder.matches(password, user.getPassword());
    
            // 这里先将user 的密码置为空防止返回给前端密码
            user.setPassword("");
            if (!flag) {
                // 此时密码错误
                return new ResponseBodyMessage<>(-1, "用户或密码错误", user);
            }
    
            // 此时密码正确那么我们需要创建一个 session对象
    
            HttpSession session = request.getSession(true);
    
            session.setAttribute(Constant.USERINFO_SESSION_KEY, user);
    
            return new ResponseBodyMessage<>(0, "登录成功", user);
    
        }
    
    
        @RequestMapping("updatename")
        public ResponseBodyMessage<Boolean> updateByName(@RequestParam String username, HttpServletRequest request) {
            // 此时我们需要对用户登录进行校验
    //       HttpSession session =  request.getSession(false);
    //       if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
    //           // 此时用户为登录
    //           return new ResponseBodyMessage<>(-1,"用户未登录",false);
    //       }
    
            // 改造 判断当前用户名是否已经存在 数据库中
            User user2 = userService.selectByName(username); // 通过修改的代码来进行查找用户如果存在就说明用户名字重复了
            if(user2 != null){
                // 此时已经纯在
                return new ResponseBodyMessage<>(-1,"用户名已存在" ,false);
            }
    
            // 1. 获取 user 对象, 这里主要获取 id
    
            HttpSession session = request.getSession(false);
    
            User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
    
            // 然后开始修改 操作 :
            Integer ret = userService.updateByName(username, user.getId());
            if (ret == 1) {
                return new ResponseBodyMessage<>(0, "修改成功", true);
            }
    
            return new ResponseBodyMessage<>(-1, "修改失败", false);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124


    mapper中的 UserMapper

    package com.example.demo.mapper;
    
    
    import com.example.demo.mapper.UserMapper;
    import com.example.demo.model.User;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    
    @Mapper
    public interface UserMapper {
    
        public Integer insert(@Param("username") String username,  @Param("password") String password);
    
    
        User selectByName(@Param("username") String username);
    
        Integer updateByName(@Param("username") String username , @Param("id") Integer id);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19


    model中的 user类

    package com.example.demo.model;
    
    import lombok.Data;
    
    @Data
    public class User {
        private Integer id;
    
        private String username;
    
        private String password;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    service中的 UserService类

    package com.example.demo.service;
    
    
    import com.example.demo.mapper.UserMapper;
    import com.example.demo.model.User;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService {
    
        @Autowired
        UserMapper userMapper;
    
        public Integer insert(String username, String password) {
            return userMapper.insert(username, password);
        }
    
        public User selectByName(String username) {
            return userMapper.selectByName(username);
        }
    
        public Integer updateByName(String username, Integer id) {
            return userMapper.updateByName(username, id);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28


    tools 中的 ResponseBodyMessage类

    
    package com.example.demo.tools;
    
    
    import lombok.Data;
    
    @Data
    public class ResponseBodyMessage<T> {
        private Integer status;
        private String message;
        private T data;
    
        public ResponseBodyMessage(Integer status, String message, T data) {
            this.status = status;
            this.message = message;
            this.data = data;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19


    tools 中的 Constant类

    package com.example.demo.tools;
    
    public class Constant {
        public static final String USERINFO_SESSION_KEY = "USERINFO_SESSION_KEY";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这里还有一个加密演示就不放上来了没用

  • 相关阅读:
    [Linux入门]---yum软件安装及vim编辑器配置
    2022谷粒商城学习笔记(一)环境配置
    助力篇|常见金融风控数据分析内容汇总,助你面试道路畅通无阻
    js — 原生轮播图的制作
    【vue-admin-element】在侧边栏和主体部位之上添加区域
    MySQL约束
    第12章_数据库其它调优策略
    Leetcode日练笔记41 [二叉树recursion专题] #250 Count Univalue Subtrees /Medium {Python}
    可变参数与Collections工具类
    【C++ Primer Plus】第3章 处理数据
  • 原文地址:https://blog.csdn.net/mu_tong_/article/details/127794126