基礎項目:留言板
截止到目前為止,我們已經學習了 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=falseusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplmap-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 {@Autowiredprivate 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 {@Autowiredprivate 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后面被賦值的部分
?這樣,一個留言板就實現了,它可以與數據庫鏈接,有了持久性