一个练习项目,好玩的bbs-java

news/2024/9/25 23:11:53

java这个我是用springboot做的

目录结构

 

 

 

application.yml

spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/my_bbs?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true
    username: rootpassword: "123456"server:port: 1087

主要代码IndexController.java

package com.example.study_bbs_java.controller;import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.study_bbs_java.entity.Countinfo;
import com.example.study_bbs_java.entity.Userinfo;
import com.example.study_bbs_java.entity.Postinfo;
import com.example.study_bbs_java.entity.Replyinfo;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.jdbc.core.PreparedStatementCreator;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.SQLException;import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;@RestController
@ResponseBody
public class IndexController {@AutowiredJdbcTemplate jdbcTemplate;int pagesize = 20;String secretKey = "saacac3423@21212";@RequestMapping(value ="/")public String index(HttpServletResponse response){response.setHeader("Server", "java-springboot");return "此站接口使用java实现,<a href='api.html' target='_blank'>接口列表</a>";}@RequestMapping(value = "/user/register")public HashMap register(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");String username = params.get("username") != null ? params.get("username").toString() : "";String password = params.get("password") != null ? params.get("password").toString() : "";String nickname = params.get("nickname") != null ? params.get("nickname").toString() : "";String passwordMd5 = getMD5(password);Userinfo userinfo1 = new Userinfo();try {String sql1 = "select id,username,nickname,addTime,sessionId from user where username='" + username + "'";userinfo1 = jdbcTemplate.queryForObject(sql1, new BeanPropertyRowMapper<>(Userinfo.class));} catch (Exception e) {//
        }HashMap res = new HashMap();if(userinfo1.getId() > 0){res.put("code", 0);res.put("msg", "");res.put("data", "用户名已经存在");}else{String sql2 = "insert into user(username, password, nickname) value('"+username+"', '"+passwordMd5+"', '"+nickname+"')";jdbcTemplate.execute(sql2);res.put("code", 0);res.put("msg", "");res.put("data", userinfo1);}return res;}@RequestMapping(value = "/user/login")public HashMap login(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");String username = params.get("username") != null ? params.get("username").toString() : "";String password = params.get("password") != null ? params.get("password").toString() : "";String passwordMd5 = getMD5(password);String sql1 = "select id,username,nickname,addTime,sessionId from user where username='"+username+"' and password='"+passwordMd5+"'";Userinfo userinfo1 = jdbcTemplate.queryForObject(sql1,new BeanPropertyRowMapper<>(Userinfo.class));HashMap res = new HashMap();if(userinfo1.getId() > 0) {String sessionId = getMD5(secretKey + userinfo1.getId() + userinfo1.getAddTime());String sql2 = "update user set sessionId='" + sessionId + "' where id=" + userinfo1.getId();jdbcTemplate.execute(sql2);userinfo1.setSessionId(sessionId);res.put("code", 0);res.put("msg", "");res.put("data", userinfo1);}else{res.put("code", 0);res.put("msg", "");res.put("data", "用户名或者密码错误");}return res;}@RequestMapping(value = "/user/logout")public HashMap logout(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");String sessionId = params.get("sessionId") != null ? params.get("sessionId").toString() : "";Userinfo userinfo1 = getloginuserinfo(sessionId);String sql1 = "update user set sessionId='' where sessionId='"+sessionId+"'";jdbcTemplate.execute(sql1);userinfo1.setSessionId("");HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", userinfo1);return res;}@RequestMapping(value = "/user/getuserinfo")public HashMap getuserinfo(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");String sessionId = params.get("sessionId") != null ? params.get("sessionId").toString() : "";String sql1 = "select id,username,nickname,addTime,sessionId from user where sessionId='"+sessionId+"'";Userinfo userinfo1 = jdbcTemplate.queryForObject(sql1,new BeanPropertyRowMapper<>(Userinfo.class));HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", userinfo1);return res;}@RequestMapping(value = "/post/list")public HashMap postlist(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");String keyword = params.get("keyword") != null ? params.get("keyword").toString() : "";Integer page = params.get("page") != null ? Integer.parseInt(params.get("page").toString()) : 1;Integer start = (page - 1) * pagesize;String addsql = " isDel=0 ";if(!keyword.equals("")){addsql = addsql + " and title='%"+keyword+"%'";}String sql1 = "select count(1) as count from content where "+addsql;Countinfo postcount = jdbcTemplate.queryForObject(sql1,new BeanPropertyRowMapper<>(Countinfo.class));String sql2 = "select id,title,userId,userNickename,replyNum,updateTime from content where "+addsql+" order by updateTime desc limit "+start+","+pagesize;List<Map<String, Object>> postlist = jdbcTemplate.queryForList(sql2);if(postlist.size() > 0){for(int i = 0; i < postlist.size(); i++){Map<String, Object> item = postlist.get(i);String updateTime = DateTimeFormatter((Date)item.get("updateTime"));item.put("updateTime", updateTime);postlist.set(i, item);}}long totalpage = Math.round(Math.ceil(postcount.getCount() / (double)pagesize));HashMap res1 = new HashMap();res1.put("totalpage", totalpage);res1.put("data", postlist);HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", res1);return res;}@RequestMapping(value = "/post/detail")public HashMap postdetail(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");Integer id = params.get("id") != null ? Integer.parseInt(params.get("id").toString()) : 0;String sql1 = "select id,title,content,userId,userNickename,replyNum,updateTime from content where isDel=0 and id="+id;Postinfo postinfo1 = jdbcTemplate.queryForObject(sql1,new BeanPropertyRowMapper<>(Postinfo.class));HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", postinfo1);return res;}@RequestMapping(value = "/post/add")public HashMap postadd(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");final String title = params.get("title") != null ? params.get("title").toString() : "";final String content = params.get("content") != null ? params.get("content").toString() : "";String sessionId = params.get("sessionId") != null ? params.get("sessionId").toString() : "";final Userinfo userinfo1 = getloginuserinfo(sessionId);if(userinfo1.getId() <= 0){HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", "请先登录");return res;}final String sql1 = "insert into content(title, content, userId, userNickename) value(?, ?, ?, ?)";KeyHolder keyHolder = new GeneratedKeyHolder();int count = jdbcTemplate.update(new PreparedStatementCreator() {@Overridepublic PreparedStatement createPreparedStatement(Connection conn)throws SQLException {PreparedStatement pstmt=conn.prepareStatement(sql1, Statement.RETURN_GENERATED_KEYS);pstmt.setString(1, title);pstmt.setString(2, content);pstmt.setInt(3, userinfo1.getId());pstmt.setString(4, userinfo1.getNickname());return pstmt;}},keyHolder);HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", keyHolder.getKey().longValue());return res;}@RequestMapping(value = "/post/edit")public HashMap postedit(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");Integer id = params.get("id") != null ? Integer.parseInt(params.get("id").toString()) : 0;String title = params.get("title") != null ? params.get("title").toString() : "";String content = params.get("content") != null ? params.get("content").toString() : "";String sessionId = params.get("sessionId") != null ? params.get("sessionId").toString() : "";Userinfo userinfo1 = getloginuserinfo(sessionId);if(userinfo1.getId() <= 0){HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", "请先登录");return res;}String sql1 = "update content set title='"+title+"',content='"+content+"',userId="+userinfo1.getId()+",userNickename='"+userinfo1.getNickname()+"' where id="+id+" and userId="+userinfo1.getId();jdbcTemplate.execute(sql1);HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", "");return res;}@RequestMapping(value = "/post/delete")public HashMap postdelete(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");Integer id = params.get("id") != null ? Integer.parseInt(params.get("id").toString()) : 0;String sessionId = params.get("sessionId") != null ? params.get("sessionId").toString() : "";Userinfo userinfo1 = getloginuserinfo(sessionId);if(userinfo1.getId() <= 0){HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", "请先登录");return res;}String sql1 = "update content set isDel=1 where id="+id+" and userId="+userinfo1.getId();jdbcTemplate.execute(sql1);HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", "");return res;}@RequestMapping(value = "/reply/list")public HashMap replylist(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");Integer contentId = params.get("contentId") != null ? Integer.parseInt(params.get("contentId").toString()) : 0;Integer page = params.get("page") != null ? Integer.parseInt(params.get("page").toString()) : 1;Integer start = (page - 1) * pagesize;String addsql = " isDel=0 and contentId="+contentId+" ";String sql1 = "select count(1) as count from reply where "+addsql;Countinfo replycount = jdbcTemplate.queryForObject(sql1,new BeanPropertyRowMapper<>(Countinfo.class));String sql2 = "select id,content,replyUserId,replyUserNickename,addTime from reply where "+addsql+" order by id asc limit "+start+","+pagesize;List<Map<String, Object>> replylist = jdbcTemplate.queryForList(sql2);if(replylist.size() > 0){for(int i = 0; i < replylist.size(); i++){Map<String, Object> item = replylist.get(i);String addTime = DateTimeFormatter((Date)item.get("addTime"));item.put("addTime", addTime);replylist.set(i, item);}}long totalpage = Math.round(Math.ceil(replycount.getCount() / (double)pagesize));HashMap res1 = new HashMap();res1.put("totalpage", totalpage);res1.put("data", replylist);HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", res1);return res;}@RequestMapping(value = "/reply/detail")public HashMap replydetail(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");Integer id = params.get("id") != null ? Integer.parseInt(params.get("id").toString()) : 0;String sql1 = "select id,content,replyUserId,replyUserNickename,addTime from reply where isDel=0 and id="+id;Replyinfo replyinfo1 = jdbcTemplate.queryForObject(sql1,new BeanPropertyRowMapper<>(Replyinfo.class));HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", replyinfo1);return res;}@RequestMapping(value = "/reply/add")public HashMap replyadd(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");final Integer contentId = params.get("contentId") != null ? Integer.parseInt(params.get("contentId").toString()) : 0;final String content = params.get("content") != null ? params.get("content").toString() : "";String sessionId = params.get("sessionId") != null ? params.get("sessionId").toString() : "";final Userinfo userinfo1 = getloginuserinfo(sessionId);if(userinfo1.getId() <= 0){HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", "请先登录");return res;}String sql2 = "update content set replyNum=replyNum+1 where id="+contentId;jdbcTemplate.execute(sql2);final String sql1 = "insert into reply(contentId, content, replyUserId, replyUserNickename) value(?, ?, ?, ?)";KeyHolder keyHolder = new GeneratedKeyHolder();int count = jdbcTemplate.update(new PreparedStatementCreator() {@Overridepublic PreparedStatement createPreparedStatement(Connection conn)throws SQLException {PreparedStatement pstmt=conn.prepareStatement(sql1, Statement.RETURN_GENERATED_KEYS);pstmt.setInt(1, contentId);pstmt.setString(2, content);pstmt.setInt(3, userinfo1.getId());pstmt.setString(4, userinfo1.getNickname());return pstmt;}},keyHolder);HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", keyHolder.getKey().longValue());return res;}@RequestMapping(value = "/reply/edit")public HashMap replyedit(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");Integer id = params.get("id") != null ? Integer.parseInt(params.get("id").toString()) : 0;String content = params.get("content") != null ? params.get("content").toString() : "";String sessionId = params.get("sessionId") != null ? params.get("sessionId").toString() : "";Userinfo userinfo1 = getloginuserinfo(sessionId);if(userinfo1.getId() <= 0){HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", "请先登录");return res;}String sql1 = "update reply set content='"+content+"',replyUserId="+userinfo1.getId()+",replyUserNickename='"+userinfo1.getNickname()+"' where id="+id+" and replyUserId="+userinfo1.getId();jdbcTemplate.execute(sql1);HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", "");return res;}@RequestMapping(value = "/reply/delete")public HashMap replydelete(@RequestParam Map<String,Object> params, HttpServletResponse response){response.setHeader("Server", "java-springboot");Integer id = params.get("id") != null ? Integer.parseInt(params.get("id").toString()) : 0;String sessionId = params.get("sessionId") != null ? params.get("sessionId").toString() : "";Userinfo userinfo1 = getloginuserinfo(sessionId);if(userinfo1.getId() <= 0){HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", "请先登录");return res;}Replyinfo replyinfo1 = new Replyinfo();try {String sql0 = "select id,content,replyUserId,replyUserNickename,addTime,contentId from reply where isDel=0 and id=" + id;replyinfo1 = jdbcTemplate.queryForObject(sql0, new BeanPropertyRowMapper<>(Replyinfo.class));}catch (Exception e){//
        }if(replyinfo1.getId() > 0) {String sql2 = "update content set replyNum=replyNum-1 where id=" + replyinfo1.getContentId();jdbcTemplate.execute(sql2);String sql1 = "update reply set isDel=1 where id=" + id + " and replyUserId=" + userinfo1.getId();jdbcTemplate.execute(sql1);}HashMap res = new HashMap();res.put("code", 0);res.put("msg", "");res.put("data", "");return res;}private String DateTimeFormatter(Date currentDate){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String formattedDate = sdf.format(currentDate);return formattedDate;}private Userinfo getloginuserinfo(String sessionId){Userinfo userinfo1 = new Userinfo();try{String sql1 = "select id,username,nickname,addTime,sessionId from user where sessionId='"+sessionId+"'";userinfo1 = jdbcTemplate.queryForObject(sql1,new BeanPropertyRowMapper<>(Userinfo.class));}catch(Exception e){//
        }return userinfo1;}private String getMD5(String plainText) {String md5 = new String();try {MessageDigest md = MessageDigest.getInstance("MD5");md.update(plainText.getBytes());byte b[] = md.digest();int i;StringBuffer buf = new StringBuffer("");for (int offset = 0; offset < b.length; offset++) {i = b[offset];if (i < 0)i += 256;if (i < 16)buf.append("0");buf.append(Integer.toHexString(i));}md5 = buf.toString();} catch (Exception e) {e.printStackTrace();}return md5;}
}

 

启动输出:

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/46113.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

从 Docker Hub 拉取镜像受阻?这些解决方案帮你轻松应对

最近一段时间 Docker 镜像一直是 Pull 不下来的状态,感觉除了挂🪜,想直连 Docker Hub 是几乎不可能的。更糟糕的是,很多原本可靠的国内镜像站,例如一些大厂和高校运营的,也陆续关停了,这对我们这些个人开发者和中小企业来说是挺难受的。之前,通过这些镜像站,我们可以…

redis自学(47)服务端优化

持久化配置 Redis的持久化虽然可以保证数据安全,但也会带来很多额外的开销,因此持久化请遵循下列建议: ① 用来做缓存的redis实例尽量不要开启持久化功能 ② 建议关闭RDB持久化功能,使用AOF持久化(RDB的数据安全性一直是有问题的,两次RDB的时间比较长,又不能频繁的RDB…

一个练习项目,好玩的bbs-1

目录结构 nginx配置:upstream bbs_upstream {server 127.0.0.1:1081; #phpserver 127.0.0.1:1086; #csharpeserver 127.0.0.1:1087; #javaserver 127.0.0.1:1084; #ruby-sinatraserver 127.0.0.1:1104; #ruby-buskerserver 127.0.0.1:1105; #ruby-ramazeserver 127.0.0.1:1080…

同态加密为什么被称为密码学的圣杯?

同态加密是一种支持数据密态处理的密码学技术,可以广泛应用于云计算、医疗、金融等领域。1.什么是同态加密? 全同态加密是一种加密技术,允许在不解密的前提下,对密文进行一些有意义的运算,使得解密后的结果与在明文上做 “相同计算” 得到的结果相同。同态加密被称为密码学…

未知进程占用显存排查

现象 nvitop 查看gpu 使用情况,会看到 ‘No Such Process’ 这样的进程占用了显存; 使用ps 查不到该命令。原因 大概率是主进程挂了,或者被终止了,但是子进程仍然占用着显存。解决方法 方法1: 如果确定进程都是python 启动的,执行下面的命令; 如果不是python,但是知道…

n模块不支持windows!!!!!!!

需要升级 node 版本。本着不想卸载 node 再重新安装的原则,因为node 的环境配置以及各种相关配置有些繁琐,所以就想着使用命令的方式进行升级。 在网上找了一些升级 node 的命令,最常见的是安装 node 的 n 模块,n 模块 是用来管理 node 版本的。开始下载: npm install -g …

服务器存储瘫痪数据恢复

一、服务器数据恢复故障描述 断电导致整个存储瘫痪,加电后存储无法使用。 经过诊断后认为是断电导致存储阵列损坏。 整个存储是由12块日立硬盘(3T SAS硬盘)组成的RAID-6磁盘阵列,被分成一个卷,分配给几台Vmware的ESXI主机做共享存储。整个卷中存放了大量的Windows虚拟机,…

服务器虚拟机文件被损坏

删除整个存储瘫痪,重启后无法使用,经过诊断后认为误删导致存储阵列损坏。 由于虚拟机的数量很多,每台都验证,所需的时间会很长,因此对整个VMFS卷做检测。在检测VMFS卷的过程中发现有部分虚拟机或虚拟机的文件被破坏。一、恢复数据 1、生成数据; 经过对几台重要的虚拟机验…