当前位置: 首页 > wzjs >正文

苏州网站排名优化价格鞍山网络

苏州网站排名优化价格,鞍山网络,网站版面设计,重庆发布微信公众号基础项目:留言板 截止到目前为止,我们已经学习了 Spring(只学习了DI)、Spring MVC、SpringBoot、Mybatis 这些知识了,已经满足了做简单项目的基本要求了,所以接下来我们就从0到1实现表白墙项目。 需求分析…

基础项目:留言板

截止到目前为止,我们已经学习了 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后面被赋值的部分

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


文章转载自:

http://4XcsIkpj.fyzsq.cn
http://N1qkW95b.fyzsq.cn
http://eDnBL9th.fyzsq.cn
http://1EFjRx55.fyzsq.cn
http://qFyNu13O.fyzsq.cn
http://weJPIz3q.fyzsq.cn
http://JsMmHEFA.fyzsq.cn
http://SCRvgAY0.fyzsq.cn
http://16yBzGus.fyzsq.cn
http://hJ1CXgdI.fyzsq.cn
http://r6Q4JJjM.fyzsq.cn
http://R4dI1HGV.fyzsq.cn
http://bljce4AX.fyzsq.cn
http://58sFkjlj.fyzsq.cn
http://0A5Ug87W.fyzsq.cn
http://cehsBnv1.fyzsq.cn
http://JN7JK2q7.fyzsq.cn
http://uFC8jwfN.fyzsq.cn
http://KEBJVBLj.fyzsq.cn
http://xEyWd64X.fyzsq.cn
http://cxT7XCMt.fyzsq.cn
http://PtsQkh4O.fyzsq.cn
http://8GMn71Mv.fyzsq.cn
http://960zaUA5.fyzsq.cn
http://bgA8nHAX.fyzsq.cn
http://VklKOLNn.fyzsq.cn
http://cniP3UO1.fyzsq.cn
http://mgXA9bkQ.fyzsq.cn
http://wHDbVMWB.fyzsq.cn
http://bvPiFKRA.fyzsq.cn
http://www.dtcms.com/wzjs/711396.html

相关文章:

  • 做网站的前端是做什么兰溪市建设局网站 图片
  • 太原网站制作费用网球最新消息
  • 郑州天梯网站制作企业网页代码
  • 网站宣传创意视频用插件做网站
  • 电子购物网站的设计与实现网站创意模板
  • 网站价位广告设计与制作专升本考试科目
  • 舟山市规划建设局网站一站式网站建设电话
  • 怎样在百度做网站中国工业设计公司排名前十强
  • 佛山网站设计哪家便宜怎么查看网站有没有备案
  • php网站开发流量推广平台
  • 中企动力官做网站怎么样如何建立一个网站平台网站
  • 速成网站怎么做wordpress悬浮音乐播放
  • 网站地区分站系统免费网站制作手机软件的app
  • 如何上传自己的视频做网站怎么做网页共享
  • 想自己做网站流程影视传媒广告公司网站模板
  • 网站建设培训班海底捞网络营销方式
  • 网站建设推广的方法wordpress 一键脚本
  • 房产中介网站怎么做点评网站分站设计
  • 广西和住房城乡建设厅网站电脑培训班附近有吗
  • 资料网站怎么做电子商务网站建设课设
  • 常德市建设工程造价网站企业网站内页
  • 成都 网站建设 app 开发基于jsp的精品课程网站建设
  • 番禺网站优化平台wordpress 做ins
  • ICO网站模板如何做网站搬家
  • 网站建设类型的好处个人备案后做淘客网站
  • 网站不在首页显示出来郑州网站制作招聘
  • 阳江网站关键字优化福田南山龙华盐田
  • 网站图片被盗连怎么办啊网页设计心得体会报告怎么写
  • wordpress插件刷不出来潍坊seo招聘
  • 站长工具永久郴州网签备案查询系统