SpringBoot+Mybatis后台管理系统(二)

简介: SpringBoot+Mybatis后台管理系统(二)

4.4 service实现类


package com.caq.boot.service.Impl;
import com.caq.boot.mapper.UserMapper;
import com.caq.boot.pojo.User;
import com.caq.boot.service.Crud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CrudImpl implements Crud {
    @Autowired
    UserMapper userMapper;
    @Override
    public User getAcc(String name, String age) {
        return userMapper.selectByNameAndAge(name,age);
    }
    @Override
    public void insertAcc(String name, int age, String email) {
        userMapper.insertUser(name,age,email);
    }
    @Override
    public void updateAcc(String name, int age, String email,int id) {
        userMapper.updateUser(name, age, email, id);
    }
    @Override
    public User selectById(int id) {
        return userMapper.SelectById(id);
    }
    @Override
    public void deleteUserById(int id) {
        userMapper.deleteUser(id);
    }
    @Override
    public List<User> listAllAcc() {
        return userMapper.selectAll();
    }
}


4.5 LoginController

控制登录相关的请求


package com.caq.boot.controller;
import com.caq.boot.pojo.User;
import com.caq.boot.service.Impl.CrudImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
    @Autowired
    CrudImpl crud;
    //跳转到登录页
    @GetMapping({"/", "/login"})
    public String login() {
        return "login";
    }
    //提交表单进行验证
    @PostMapping("/login")
    public String index(@RequestParam("name") String name,
                        @RequestParam("age") String age,
                        HttpSession session,
                        Model model) {
        User acc = crud.getAcc(name,age);
        if (acc != null){
            session.setAttribute("login",acc);
            return "redirect:/index";
        }else {
            model.addAttribute("msg","账号或密码错误");
            return "login";
        }
    }
//    重定向后的判断
    @GetMapping("/index")
    public String index(HttpSession session,
                        Model model){
        Object login = session.getAttribute("login");
        if (login != null){
            return "index";
        }else {
            model.addAttribute("msg","请重新登录");
            return "login";
        }
    }
}


4.5 RegisterController


package com.caq.boot.controller;
import com.caq.boot.service.Impl.CrudImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
@Controller
public class RegisterController {
    @Autowired
    CrudImpl crud;
    @GetMapping("register.html")
    public String register(){
        return "register";
    }
    @PostMapping("register")
    public String submit(@RequestParam("name") String name,
                         @RequestParam("age") int age,
                         @RequestParam("email") String email
    ){
        crud.insertAcc(name,age,email);
        return "redirect:/login.html";
    }
    @GetMapping("/login.html")
    public String login(){
        return "login";
    }
}


4.6 tablesController


表格相关的控制


package com.caq.boot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class TableController {
    @GetMapping("/tables.html")
    public String tables(){
        return "tables";
    }
}


4.7 Crud相关的控制


package com.caq.boot.controller;
import com.caq.boot.pojo.User;
import com.caq.boot.service.Impl.CrudImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
public class CrudController {
    @Autowired
    CrudImpl crud;
    @GetMapping("tables")
    public String listAll(Model model,
                          HttpSession session) {
        if (session.getAttribute("login") != null){
            List<User> users = crud.listAllAcc();
            model.addAttribute("listUsers", users);
            return "tables";
        }else {
            model.addAttribute("msg","请先登录");
            return "login";
        }
    }
    //新增用户点击的处理
    @GetMapping("insert_page")
    public String insert(){
        return "insert_page";
    }
    //新增用户后提交表单的处理
    @PostMapping("insert_page")
    public String insertAcc(@RequestParam("name") String name,
                            @RequestParam("age") int age,
                            @RequestParam("email") String email) {
        crud.insertAcc(name, age, email);
        return "redirect:/tables";
    }
    //同上
    @GetMapping("update_page/{id}")
    public String update(@PathVariable("id") int id,
                         Model model){
        User user = crud.selectById(id);
        model.addAttribute("user",user);
        return "update_page";
    }
    @PostMapping("update_page")
    public String update(@RequestParam("name") String name,
                         @RequestParam("age") int age,
                         @RequestParam("email") String email,
                         @RequestParam("id") int id
                         ){
        crud.updateAcc(name,age,email,id);
        return "redirect:/tables";
    }
    //删除用户的请求处理
    @GetMapping("delete/{id}")
    public String delete(@PathVariable("id")int id){
        crud.deleteUserById(id);
        return "redirect:/tables";
    }
}


4.8 前端页面的处理


前端框架

前端页面可以通过网上的UI模板套用


主要是关键的位置,会出现什么样的请求我们处理好这些请求即可


前端页面用到了很多Thymeleaf的知识,下面我会着重写这一块相关的


Layui - 经典开源模块化前端 UI 框架(官方文档镜像站) (layuiweb.com)


Examples · Bootstrap (getbootstrap.com)


能在这里拿很多你想要的样式,表单啊、按钮…等等


image.png


个性化提示

我们定位到登录页,可以在这里做一个错误提示


例如:如果没有登录就去访问类路径下的资源,那么提示一些警告。


我们在Welcome Back!下面新建一行,这一行来提示错误信息。通过在controller层的判断,如果没有登录就用Model忘请求域中存入错误信息Model.addAttribute(“msg”,密码或账户错误)


我们存取过后呢,在前端页面怎么显示?通过thymeleaf来获取即可


<p style="color: red"  th:text="${msg}"></p>
1
<div class="text-center">
    <h1 class="h4 text-gray-900 mb-4">Welcome Back!</h1>
    <p style="color: red"  th:text="${msg}"></p>
</div>
<form class="user" method="post" th:action="@{/login}">
    <div class="form-group">
        <input name="name" type="email" class="form-control form-control-user"
               id="exampleInputEmail" aria-describedby="emailHelp"
               placeholder="Enter Email Address...">
    </div>
    <div class="form-group">
        <input name="age" type="password" class="form-control form-control-user"
               id="exampleInputPassword" placeholder="Password">
    </div>
    <div class="form-group">
        <div class="custom-control custom-checkbox small">
            <input type="checkbox" class="custom-control-input" id="customCheck">
            <label class="custom-control-label" for="customCheck">Remember
                Me</label>
        </div>
    </div>
    <input type="submit" class="btn btn-primary btn-user btn-block">
</form>


input标签的说明


input标签要注意加上name属性,不然提交的时候请求域中获取不到输入的值


概念


payload,翻译过来是有效载荷


payload 字面意思“有效载荷,有效负荷,有效载重”。


要解释什么是有效载重,用货运行业打个比方:

比如有一位客户需要支付一笔费用委托货车司机运送一车石油,石油本身的重量、车子的重量、司机的重量等等,这些都属于载重(load)。但是对于该客户来说,他关心的只有石油的重量,所以石油的重量是有效载重(payload,也就是付费的重量)。


所以抽象一下,payload 可以理解为一系列信息中最为关键的信息。

对于程序员来说就是在程序中 起关键作用的代码。


安全方面:


通常在传输数据时,为了使数据传输更可靠,要把原始数据分批传输,并且在每一批数据的头和尾都加上一定的辅助信息,

比如数据量的大小、校验位等,这样就相当于给已经分批的原始数据加一些外套,这些外套起标示作用,使得原始数据不易丢失,

一批数据加上“外套”就形成了传输通道的基本传输单元,叫做数据帧或数据包,而其中的原始数据就是payload


我来模拟一个登录请求,查看一下它提交的信息


image.png


表单提交的说明

表单提交是post请求


thymeleaf的循环

th:each 迭代 th:each="user:${listUsers}"


其中listUsers是一个数组,user是遍历的元素


通过这种方式就可以把数据库所有的数据展示到表格中了==(获取到对象后,通过对象的get方法获取对应的属性)==


<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
    <!--                                <button class="layui-btn layui-btn-sm" οnclick="window.location.href='insert_page.html'" type="button" >新增</button>-->
    <a th:href="@{/insert_page}" class="layui-btn layui-btn-sm">新增</a>
    <thead>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>age</th>
            <th>email</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="user:${listUsers}">
            <td th:text="${user.getId()}"></td>
            <td th:text="${user.getName()}"></td>
            <td th:text="${user.getAge()}"></td>
            <td th:text="${user.getEmail()}"></td>
            <td>
                <a th:href="@{/update_page/ } + ${user.getId()}" class="layui-btn layui-btn-warm layui-btn-sm">编辑</a>
                <a th:href="@{/delete/} + ${user.getId()}" class="layui-btn layui-btn-danger layui-btn-sm">删除</a>
            </td>
        </tr>
    </tbody>
</table>


编辑和删除按钮的使用

把按钮改成超链接标签更好使用,可以直接选择跳转


刚开始学习的时候面临一个问题,如果能够点击按钮的时候获取到id的值呢?因为我们删除和编辑都是基于id进行的


当然还是thymeleaf的方式,通过路径的拼接即可获得


前面我们通过thymeleaf的遍历把数据库中所有的数据都遍历了出来,这样我们就可以通过调用对象的get方法获取用户的id值了


<td>
    <a th:href="@{/update_page/ } + ${user.getId()}" class="layui-btn layui-btn-warm layui-btn-sm">编辑</a>
    <a th:href="@{/delete/} + ${user.getId()}" class="layui-btn layui-btn-danger layui-btn-sm">删除</a>
</td>


5.自我总结


后端算刚刚入门了,前期的学习有点仓促。


后面的学习中还是要稳扎稳打,学一段时间后一定要进行复习,然后通过项目进行练习所学的技术。


以上共勉!

相关文章
|
9月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
1428 1
Spring boot 使用mybatis generator 自动生成代码插件
|
开发框架 前端开发 网络协议
Spring Boot结合Netty和WebSocket,实现后台向前端实时推送信息
【10月更文挑战第18天】 在现代互联网应用中,实时通信变得越来越重要。WebSocket作为一种在单个TCP连接上进行全双工通信的协议,为客户端和服务器之间的实时数据传输提供了一种高效的解决方案。Netty作为一个高性能、事件驱动的NIO框架,它基于Java NIO实现了异步和事件驱动的网络应用程序。Spring Boot是一个基于Spring框架的微服务开发框架,它提供了许多开箱即用的功能和简化配置的机制。本文将详细介绍如何使用Spring Boot集成Netty和WebSocket,实现后台向前端推送信息的功能。
3596 1
|
12月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
895 0
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
664 2
|
Java 数据库连接 Maven
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和MyBatis Generator,使用逆向工程来自动生成Java代码,包括实体类、Mapper文件和Example文件,以提高开发效率。
711 2
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
758 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
|
前端开发 Java Apache
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
本文详细讲解了如何整合Apache Shiro与Spring Boot项目,包括数据库准备、项目配置、实体类、Mapper、Service、Controller的创建和配置,以及Shiro的配置和使用。
3373 2
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
|
Java 数据库连接 API
springBoot:后端解决跨域&Mybatis-Plus&SwaggerUI&代码生成器 (四)
本文介绍了后端解决跨域问题的方法及Mybatis-Plus的配置与使用。首先通过创建`CorsConfig`类并设置相关参数来实现跨域请求处理。接着,详细描述了如何引入Mybatis-Plus插件,包括配置`MybatisPlusConfig`类、定义Mapper接口以及Service层。此外,还展示了如何配置分页查询功能,并引入SwaggerUI进行API文档生成。最后,提供了代码生成器的配置示例,帮助快速生成项目所需的基础代码。
773 1
|
SQL Java 数据库连接
mybatis使用二:springboot 整合 mybatis,创建开发环境
这篇文章介绍了如何在SpringBoot项目中整合Mybatis和MybatisGenerator,包括添加依赖、配置数据源、修改启动主类、编写Java代码,以及使用Postman进行接口测试。
306 0
mybatis使用二:springboot 整合 mybatis,创建开发环境
|
JavaScript NoSQL Java
CC-ADMIN后台简介一个基于 Spring Boot 2.1.3 、SpringBootMybatis plus、JWT、Shiro、Redis、Vue quasar 的前后端分离的后台管理系统
CC-ADMIN后台简介一个基于 Spring Boot 2.1.3 、SpringBootMybatis plus、JWT、Shiro、Redis、Vue quasar 的前后端分离的后台管理系统
516 0

热门文章

最新文章