Mybatis----留言板

基础项目:留言板

截止到目前为止,我们已经学习了 Spring(只学习了DI)、Spring MVC、SpringBoot、Mybatis 这些知识了,已经满足了做简单项目的基本要求了,所以接下来我们就从0到1实现表白墙项目。

需求分析:实现一个简单的表白墙,用户访问表白墙页面时,可以在输入框中输出 from、to、message的基本信息,然后点击发送按钮就可以将信息提交到表白墙上。

数据库设计:需要一个存放留言信息的数据表,基本字段:id、from、to、message、delete_flag、create_time、update_time。

后端接口:只需要提供两个接口给前端,一个是提交留言的接口,另一个是获取留言信息的接口。
效果预览

不了解的可以去我之前文章观看基础版本的留言板

[SpringBoot]Spring MVC(5.0)----留言板_springboot留言功能-CSDN博客

思路

我们需要在数据库中保存一条一条的消息,那就需要一个类

复制代码
@Data
public class MessageInfo {
    private Integer id;
    private String from; //谁留言
    private String to; //留言对谁说
    private String message; //留言的内容
    private Integer deleteFlag; //删除的标志
    private Date createTime;
    private Date updateTime;
}

主体要实现两个功能:发布留言、查看所有留言

1.引入依赖以及配置文件:

复制代码
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true 

2.写 Mapper 接口

复制代码
package com.example.demo.mapper;

import com.example.demo.model.MessageInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface MessageInfoMapper {
    @Select("select `id`, `from`, `to`, `message` from message_info where delete_flag=0")
            List<MessageInfo> queryAll();
            @Insert("insert into message_info (`from`,`to`, `message`) values(#{from},#{to},#{message})")
            Integer addMessage(MessageInfo messageInfo);
}

接下来写 Service,调用接口那两个方法就 ok 了:

复制代码
package com.example.demo.service;

import com.example.demo.mapper.MessageInfoMapper;
import com.example.demo.model.MessageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MessageInfoService {
    @Autowired
    private MessageInfoMapper messageInfoMapper;
    public List<MessageInfo> queryAll() {
        return messageInfoMapper.queryAll();
    }
    public Integer addMessage(MessageInfo messageInfo) {
        return messageInfoMapper.addMessage(messageInfo);
    }
}

再来写 Controller 实现接口中的方法

复制代码
import com.example.demo.model.MessageInfo;
import com.example.demo.service.MessageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RequestMapping("/message")
@RestController
public class MessageController {
    @Autowired
    private MessageInfoService messageInfoService;

    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        return messageInfoService.queryAll();
    }

    @RequestMapping("/publish")
    public boolean publish(MessageInfo messageInfo) {
        System.out.println(messageInfo);
         if (StringUtils.hasLength(messageInfo.getFrom()) && StringUtils.hasLength(messageInfo.getTo()) && StringUtils.hasLength(messageInfo.getMessage())) {
             messageInfoService.addMessage(messageInfo);
             return true;
         }

         return false;
    }
 }

最后就是书写前端代码,对我们后端开发来说,前端代码还是需要好好练习的

复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <style>
        .container {
            width: 350px;
            height: 300px;
            margin: 0 auto;
            /* border: 1px black solid; */
            text-align: center;
        }

        .grey {
            color: grey;
        }

        .container .row {
            width: 350px;
            height: 40px;

            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .container .row input {
            width: 260px;
            height: 30px;
        }

        #submit {
            width: 350px;
            height: 40px;
            background-color: orange;
            color: white;
            border: none;
            margin: 10px;
            border-radius: 5px;
            font-size: 20px;
        }
    </style>
</head>

<body>
<div class="container">
    <h1>留言板</h1>
    <p class="grey">输入后点击提交, 会将信息显示下方空白处</p>
    <div class="row">
        <span>谁:</span> <input type="text" name="" id="from">
    </div>
    <div class="row">
        <span>对谁:</span> <input type="text" name="" id="to">
    </div>
    <div class="row">
        <span>说什么:</span> <input type="text" name="" id="say">
    </div>
    <input type="button" value="提交" id="submit" onclick="submit()">
    <!-- <div>A 对 B 说: hello</div> -->
</div>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
         $.ajax({
                url: "/message/getList",
                type: "get",
                success: function(result) {
                    if(result!=null&&result.length>0) {
                        for(x of result) {
                            var divE = "<div>"+x.from+ "对" + x.to + "说:" + x.message + "</div>";
                            $(".container").append(divE);
                        }

                    }
                }
            });
        function submit() {
            $.ajax({
                url: "/message/publish",
                type: "post",
                data: {
                    from: $("#from").val(),
                    to: $("#to").val(),
                    message: $("#say").val()
                },
                //http响应成功
                success:function(result) {
                    if(result == false) {
                        alert("输入不合法");
                    }else {
                        //展示信息
                        //1. 构造节点
                        // 修正:使用 jQuery 选择器获取输入值
                        var divE = "<div>"+$("#from").val()+ "对" + $("#to").val() + "说:" + $("#say").val() + "</div>";
                        //2. 把节点添加到页面上
                        $(".container").append(divE);

                        //3. 清空输入框的值
                        $("#from").val("");
                        $("#to").val("");
                        $("#say").val("");
                    }
                }
            });

        }
    </script>
</body>

</html>

我们这里主要是对一些内容的修改,因为我们之前用的Say,而现在用的message,这里我们就需要知道,哪里是前端的属性,哪里是后端实体类的属性

前端:id,以及data后面$(#"")的内容

后端:取自后端返回结果的内容以及data后面被赋值的部分

这样,一个留言板就实现了,它可以与数据库链接,有了持久性

相关推荐
DuelCode21 分钟前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
荔枝吻2 小时前
【沉浸式解决问题】idea开发中mapper类中突然找不到对应实体类
java·intellij-idea·mybatis
JAVA学习通3 小时前
Mybatis--动态SQL
sql·tomcat·mybatis
新world11 小时前
mybatis-plus从入门到入土(二):单元测试
单元测试·log4j·mybatis
RainbowSea15 小时前
问题 1:MyBatis-plus-3.5.9 的分页功能修复
java·spring boot·mybatis
双力臂4041 天前
MyBatis动态SQL进阶:复杂查询与性能优化实战
java·sql·性能优化·mybatis
慕y2741 天前
Java学习第十五部分——MyBatis
java·学习·mybatis
一勺菠萝丶1 天前
Spring Boot + MyBatis/MyBatis Plus:XML中循环处理List参数的终极指南
xml·spring boot·mybatis
coding and coffee2 天前
狂神说 - Mybatis 学习笔记 --下
java·后端·mybatis