开发工具:idea或eclipse
数据库:mysql 数据库连接工具:navcat

package cn.tzp.news.controller;import cn.tzp.news.domain.Article;
import cn.tzp.news.domain.Comment;
import cn.tzp.news.domain.Favorite;
import cn.tzp.news.domain.User;
import cn.tzp.news.service.ArticleService;
import cn.tzp.news.service.CommentService;
import cn.tzp.news.service.FavoriteService;
import cn.tzp.news.service.UserService;
import cn.tzp.news.utils.CheckCodeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.List;/*** @program: news_demo* @description: 用户管理* @author: tzp* @create: 2023-02-13 13:10**/
@Controller
@Slf4j
@RequestMapping("/user")
public class UserController {@AutowiredUserService userService;@AutowiredArticleService articleService;@AutowiredFavoriteService favoriteService;@AutowiredCommentService commentService;//    注册页面@RequestMapping("/register")public String register(Model model) {return "register";}//提供注册页面验证码@RequestMapping("/register/checkCode")@ResponseBodypublic void getCheck(HttpServletResponse response, HttpServletRequest request) {//调用验证码工具类提供验证码CheckCodeUtil.getCheckCode(request,response);}//    注册页面提交表单返回结果@PostMapping("/register/result")@ResponseBodypublic Map sendRegisterResult(@RequestBody Map<String,String> map1,HttpSession session){Map map = userService.registerResult(map1, session);
//        log.info("用户属性{}",map1);
//        log.info("map{}",map);return map;}//前端登录页面@RequestMapping("/login")public String userLogin(Model model){return "login";}//前端登录页面结果返回@PostMapping("/login/result")@ResponseBodypublic Map sendLoginResult(@RequestBody Map<String,String> map1,HttpSession session){Map map = userService.loginResult(map1, session);log.info("用户属性{}",map1);log.info("map{}",map);
//        log.info("前端验证码{}",checkcode);return map;}//前端用户账号退出@RequestMapping("/quit")public String userQuit(HttpSession session){//销毁保存的sessionsession.invalidate();return "redirect:/index";}//前端用户点击邮箱内链接激活结果@RequestMapping("/active")@ResponseBodypublic String userActive(@RequestParam("code")String code,HttpSession session){String msg = userService.activeResult(code,session);return msg;}//用户手动点击激活@RequestMapping("per_active")@ResponseBodypublic  Map personActive(HttpSession session){User user = (User) session.getAttribute("user");user=userService.activeEmail(user);//将激活码写进去数据库userService.updateById(user);HashMap<String, Object> map = new HashMap<>();map.put("flag",200);return map;}//    个人主页,暂时无用@RequestMapping("/home")public String home(Model model) {return "home";}//我的消息,暂时无用@RequestMapping("/message")public String message(Model model) {return "message";}//个人设置页面@RequestMapping("/set")public String set(Model model,HttpSession session) {User user =(User) session.getAttribute("user");model.addAttribute("user",user);return "set";}//用户中心,返回用户发帖和帖子收藏以及评论@RequestMapping("/user_center")public String user_center(Model model,HttpSession session) {//返回用户发表的新闻User user =(User) session.getAttribute("user");QueryWrapper<Article> wrapper = new QueryWrapper<>();wrapper.eq("author_id",user.getUid()).orderByDesc("create_time");List<Article> list = articleService.list(wrapper);model.addAttribute("lists",list);//返回用户的收藏List<Article> favArts = userService.getFavArt(user.getUid());model.addAttribute("favArts",favArts);//需要返回添加时间QueryWrapper<Favorite> queryWrapper = new QueryWrapper<>();queryWrapper.eq("uid",user.getUid()).orderByDesc("add_time");List<Favorite> favorites = favoriteService.list(queryWrapper);model.addAttribute("favorites",favorites);//返回用户评论QueryWrapper<Comment> comWrapper = new QueryWrapper<>();comWrapper.eq("uid",user.getUid()).orderByDesc("com_time");List<Comment> commentList = commentService.list(comWrapper);model.addAttribute("commentList",commentList);return "user_center";}//进行用户个人新闻的删除@RequestMapping("/delArt")@ResponseBodypublic Map delArt(Integer aid,HttpSession session){log.info("{}", aid);boolean b = articleService.removeById(aid);HashMap<String, Object> map = new HashMap<>();if (b){map.put("flag",200);map.put("msg","删除成功");}else {map.put("flag",400);map.put("msg","删除失败,请稍后尝试");}return map;}//进行用户收藏夹的删除@RequestMapping("/delFav")@ResponseBodypublic Map delFavorite(Integer aid,HttpSession session){User user =(User) session.getAttribute("user");QueryWrapper<Favorite> wrapper = new QueryWrapper<>();wrapper.eq("aid",aid).eq("uid",user.getUid());boolean b = favoriteService.remove(wrapper);HashMap<String, Object> map = new HashMap<>();if (b){map.put("flag",200);map.put("msg","删除成功");}else {map.put("flag",400);map.put("msg","删除失败,请稍后尝试");}return map;}//进行用户评论的删除@RequestMapping("/delCom")@ResponseBodypublic Map delComment(Integer comid,HttpSession session){User user =(User) session.getAttribute("user");QueryWrapper<Comment> wrapper = new QueryWrapper<>();wrapper.eq("comid",comid);boolean b = commentService.remove(wrapper);HashMap<String, Object> map = new HashMap<>();if (b){map.put("flag",200);map.put("msg","删除成功");}else {map.put("flag",400);map.put("msg","删除失败,请稍后尝试");}return map;}//上传图片等资源文件@ResponseBody@RequestMapping(value = "/uploadHeadImg")public Map uploadFile(HttpServletRequest request, @RequestParam("file") MultipartFile file,HttpSession session) throws IOException {Map<String, Object> map = new HashMap<>();Map<String, Object> map2 = new HashMap<>();if (file != null) {
//            String webapp = request.getSession().getServletContext().getRealPath("/");//存放到项目静态资源下String webapp = "src/main/resources/";try {//图片名字String substring = file.getOriginalFilename();
//                System.out.println(substring);//使用uuid替代原来名字String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();// 图片的路径+文件名称
//                String fileName = "/static/upload/" + substring;//使用uuid上传将上传的图片重命名,但是遇到改名之后上传较慢,需要等待传输才能回显String uuidName = uuid + "." + substring.substring(substring.lastIndexOf(".") + 1);
//                System.out.println(uuidName);String fileName = "/static/upload_headImg/" + uuidName;
//                System.out.println(fileName);// 图片的在服务器上面的物理路径File destFile = new File(webapp, fileName);
//                log.info("真实路径{}",destFile);// 生成upload目录File parentFile = destFile.getParentFile();if (!parentFile.exists()) {parentFile.mkdirs();// 自动生成upload目录}// 把上传的临时图片,复制到当前项目的webapp路径FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(destFile));map = new HashMap<>();map2 = new HashMap<>();map.put("code", 0);//0表示成功,1失败map.put("msg", "上传成功");//提示消息map.put("data", map2);
//                map2.put("src", fileName);//图片url
//                map2.put("src", "/upload/" + substring);//图片urlmap2.put("src", "/upload_headImg/"+uuidName);//图片url
//                map2.put("title", substring);//图片名称,这个会显示在输入框里map2.put("title", uuidName);//图片名称,这个会显示在输入框里log.info("图片地址为{}",uuidName);//将用户头像保存到数据库User user = (User) session.getAttribute("user");user.setHeadImage("/upload_headImg/"+uuidName);userService.updateById(user);} catch (Exception e) {e.printStackTrace();}}return map;}//修改用户基本信息@PostMapping("/alterInfo")@ResponseBodypublic Map alterUserInfo(@RequestBody User user,HttpSession session){
//        log.info("用户信息{}",user);Map<String, Object> map = new HashMap<>();//从session取出已有数据然后替换,保存到数据库User sessionUser = (User) session.getAttribute("user");sessionUser.setTelephone(user.getTelephone());sessionUser.setEmail(user.getEmail());sessionUser.setSex(user.getSex());sessionUser.setNewsName(user.getNewsName());//更新用户信息boolean flag = userService.updateById(sessionUser);if (flag){//修改sessionsession.setAttribute("user",sessionUser);map.put("flag",200);}return map;}//修改用户密码@PostMapping("/alterPassword")@ResponseBodypublic Map alterPassword(@RequestBody Map<String,String> map1,HttpSession session){
//        log.info("修改密码{}",map1);Map map = userService.alterPass(map1, session);return map;}}

package cn.tzp.news.controller;import cn.tzp.news.domain.Article;
import cn.tzp.news.domain.Category;
import cn.tzp.news.domain.Comment;
import cn.tzp.news.domain.User;
import cn.tzp.news.service.ArticleService;
import cn.tzp.news.service.CateService;
import cn.tzp.news.service.CommentService;
import cn.tzp.news.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;/*** @program: demo* @description: 文章管理* @author: tzp* @create: 2023-02-13 09:38**/
@Controller
@Slf4j
public class ArticleController {@AutowiredArticleService articleService;@AutowiredCateService cateService;@AutowiredUserService userService;@AutowiredCommentService commentService;@RequestMapping("article_pub")public String article_Pub(@RequestParam(value = "id",required = false)Integer id,Model model) {Article byId = articleService.getById(id);model.addAttribute("byId",byId);List<Category> categories = cateService.list();model.addAttribute("categories",categories);return "article_pub";}//上传图片等资源文件@ResponseBody@RequestMapping(value = "/upload/uploadFile")public Map uploadFile(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {Map<String, Object> map = new HashMap<>();Map<String, Object> map2 = new HashMap<>();if (file != null) {
//            String webapp = request.getSession().getServletContext().getRealPath("/");//存放到项目静态资源下String webapp = "src/main/resources/";try {//图片名字String substring = file.getOriginalFilename();
//                System.out.println(substring);//使用uuid替代原来名字String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();// 图片的路径+文件名称
//                String fileName = "/static/upload/" + substring;//使用uuid上传将上传的图片重命名,但是遇到改名之后上传较慢,需要等待传输才能回显String uuidName = uuid + "." + substring.substring(substring.lastIndexOf(".") + 1);
//                System.out.println(uuidName);String fileName = "/static/upload/" + uuidName;
//                System.out.println(fileName);// 图片的在服务器上面的物理路径File destFile = new File(webapp, fileName);
//                log.info("真实路径{}",destFile);// 生成upload目录File parentFile = destFile.getParentFile();if (!parentFile.exists()) {parentFile.mkdirs();// 自动生成upload目录}// 把上传的临时图片,复制到当前项目的webapp路径FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(destFile));map = new HashMap<>();map2 = new HashMap<>();map.put("code", 0);//0表示成功,1失败map.put("msg", "上传成功");//提示消息map.put("data", map2);
//                map2.put("src", fileName);//图片url
//                map2.put("src", "/upload/" + substring);//图片urlmap2.put("src", "/upload/"+uuidName);//图片url
//                map2.put("title", substring);//图片名称,这个会显示在输入框里map2.put("title", uuidName);//图片名称,这个会显示在输入框里log.info("图片地址为{}",uuidName);} catch (Exception e) {e.printStackTrace();}}return map;}//文章保存@PostMapping("/articleSave")@ResponseBodypublic Map articleSave(@RequestBody Article article, HttpSession session) {Map map1 = articleService.savaArticle(article, session);return map1;}//通过列表点击文章跳转@GetMapping("/article/details/{id}")public String findArticalDetail(@PathVariable("id")Integer id,Model model){
//        Article articleById = articleService.getById(id);
//        //点击数+1
//        articleById.setCheckNum(articleById.getCheckNum()+1);
//        articleService.updateById(articleById);//获取点击新闻的内容Article articleById = articleService.findMessageId(id);//获取该新闻下评论QueryWrapper<Comment> wrapper = new QueryWrapper<>();List<Comment> commentList = commentService.list(wrapper.eq("aid", id).orderByAsc("com_time"));//获取相关评论的用户信息List<User> comUser = commentService.getComUser(id);model.addAttribute("articleById",articleById);model.addAttribute("commentList",commentList);model.addAttribute("comUser",comUser);return "article_details";}//访问指定新闻分类@GetMapping("/cate/CateArticle/{cid}")public String CateArticle(@PathVariable("cid")Integer cid, Model model){Category cateById = cateService.getById(cid);model.addAttribute("cateById",cateById);return "article";}//新闻分类查询结果返回分页结果@RequestMapping("/cate/queryCaetAll")@ResponseBodypublic Map queryCateAll(Integer pageNum,Integer pageSize,Integer cateId,HttpSession session) {//使用MP自带的分页插件分页查询数据Page<Article> catePage = new Page<>(pageNum, pageSize);//分页查询结果QueryWrapper<Article> cateQuery = new QueryWrapper<>();//查询条件,用户有登录显示为用户等级的查询,没有登录查询的是最低的等级User user = (User) session.getAttribute("user");if (user!=null){cateQuery.eq("cid",cateId).eq("lid",user.getLid()).eq("status",1).orderByDesc("create_time");}else {cateQuery.eq("cid",cateId).eq("lid",1).eq("status",1).orderByDesc("create_time");}Page<Article> catepages = articleService.page(catePage, cateQuery);List<Article> cateArtList = catepages.getRecords();long total = catepages.getTotal();Map<String, Object> map = new HashMap<>();map.put("data",cateArtList);map.put("count",total);map.put("status",200);
//        map.put("cate",cate);return map;}//    进行新闻收藏@PostMapping("/article/fav")@ResponseBodypublic Map artFavorite(Integer id,HttpSession session){Map map = articleService.artFavorite(id, session);return map;}//新闻搜索@PostMapping("/article/search")public String search(String keyword,Model model,HttpSession session){//返回搜素的内容model.addAttribute("keyword",keyword);//返回搜素结果List<Article> articles = articleService.searchArt(session, keyword);model.addAttribute("articles",articles);return "search";}//展示其他等级新闻@RequestMapping("/article/other/{lid}")public  String otherArticle(@PathVariable("lid")Integer lid,Model model,@RequestParam(value = "level",required = false)Integer level,@RequestParam(value = "cid",required = false)Integer cid,@RequestParam(value = "keyword",required = false)String keyword){
//                                @RequestParam(value = "pageNum")Integer pageNum,
//                                @RequestParam(value = "pageSize")Integer pageSize){model.addAttribute("level",lid);model.addAttribute("cid",cid);model.addAttribute("keyword",keyword);
//        model.addAttribute("level",lid);
//        model.addAttribute("level",lid);//        log.info("{}",level);
//        log.info("{}",cid);
//        log.info("{}",keyword);return "other_level";}//返回其他等级查询结果,可分页@RequestMapping("/article/other/levelResult")@ResponseBodypublic Map queryOtherLevResult(Integer pageNum,Integer pageSize,Integer cid,Integer level,String keyword) {Map<String, Object> map = new HashMap<>();try {IPage<Article> page = articleService.findOtherLevPage(pageNum, pageSize,cid,level,keyword);List<Article> addCateList = page.getRecords();long total = page.getTotal();map.put("data",addCateList);map.put("count",total);map.put("status",200);} catch (Exception e) {log.error(e.getMessage());}return map;//        System.out.println(pageNum+":"+pageSize+":"+cid+":"+level+":"+keyword);
//        //使用MP自带的分页插件分页查询数据
//        Page<Article> levelPage = new Page<>(pageNum, pageSize);
//        //分页查询结果QueryWrapper<Article> cateQuery = new QueryWrapper<>();
//        QueryWrapper<Article> wrapper = new QueryWrapper<>();
//        //查询条件
//        wrapper.eq("lid",level);
//        //条件筛选
//        if (StringUtils.isNotBlank(keyword)){
//            wrapper.like("title",keyword).or().like("content",keyword);
//        }
//        if (cid!=null){
//            wrapper.eq("cid",cid);
//        }
//
//        Page<Article> levpages = articleService.page(levelPage, wrapper);
//        List<Article> levArtList = levpages.getRecords();
//
//        long total = levpages.getTotal();
//        Map<String, Object> map = new HashMap<>();
//        map.put("data",levArtList);
//        map.put("count",total);
//        map.put("status",200);
//        map.put("cate",cate);
//        return map;}}

基于springboot layui新闻发布网站前后端源码相关推荐

  1. 一款基于SpringBoot+layui 开源的固定设备资产管理系统源码 源码免费分享

    淘源码:国内专业的免费源码下载平台 分享一款开源的固定设备资产管理系统源码,系统可对常用资产设备进行信息化管理,包含自定义支持各类设备.自带导入导出.维护工作统计.采购管理.文档管理.合同管理等功能, ...

  2. 毕设:基于SpringBoot+Vue 实现云音乐(前后端分离)

    文章目录 一.简介 2.项目介绍 二.功能 2.功能介绍 三.核心技术 1.系统架构图 2.技术选型 五.运行 3.截图 前端界面 后台管理界面 总结 1.完整工程 2.其他 一.简介 2.项目介绍 ...

  3. SpringBoot+LayUI+MybatisPlus+Echarts图表 前后端分离 实现数据统计功能

    前言: 小伙伴们,大家好,我是狂奔の蜗牛rz,当然你们可以叫我蜗牛君,我是一个学习Java快一年时间的小菜鸟,同时还有一个伟大的梦想,那就是有朝一日,成为一个优秀的Java架构师. 首先给各位粉丝朋友 ...

  4. [整站源码]thinkphp家纺针织床上用品类网站模板+前后端源码

    模板介绍: 本模板自带eyoucms内核,无需再下载eyou系统,原创设计.手工书写DIV+CSS,完美兼容IE7+.Firefox.Chrome.360浏览器等:主流浏览器:结构容易优化:多终端均可 ...

  5. [整站源码]thinkphp古筝古琴书画培训类网站模板+前后端源码

    模板介绍: 本模板自带eyoucms内核,无需再下载eyou系统,原创设计.手工书写DIV+CSS,完美兼容IE7+.Firefox.Chrome.360浏览器等:主流浏览器:结构容易优化:多终端均可 ...

  6. [整站源码]thinkphp美肤微形美容仪器网站模板+前后端源码

    模板介绍: 本模板自带eyoucms内核,无需再下载eyou系统,原创设计.手工书写DIV+CSS,完美兼容IE7+.Firefox.Chrome.360浏览器等:主流浏览器:结构容易优化:多终端均可 ...

  7. 羊了个羊游戏网站源码_带前后端源码,带教程

    羊了个羊游戏源码_带前后端源码_及配置教程说明 这是啥游戏?据悉,这是一款卡通背景的消除闯关游戏.玩家们需要点击上方卡牌,被选中的卡牌会下移到底部的木框中,框内最多可以储存7张卡牌,当有3张相同的卡牌 ...

  8. 基于JAVA响应式交友网站计算机毕业设计源码+数据库+lw文档+系统+部署

    基于JAVA响应式交友网站计算机毕业设计源码+数据库+lw文档+系统+部署 基于JAVA响应式交友网站计算机毕业设计源码+数据库+lw文档+系统+部署 本源码技术栈: 项目架构:B/S架构 开发语言: ...

  9. 基于JAVA陕菜食谱网站计算机毕业设计源码+数据库+lw文档+系统+部署

    基于JAVA陕菜食谱网站计算机毕业设计源码+数据库+lw文档+系统+部署 基于JAVA陕菜食谱网站计算机毕业设计源码+数据库+lw文档+系统+部署 本源码技术栈: 项目架构:B/S架构 开发语言:Ja ...

最新文章

  1. Android GIF 编解码
  2. 2、RabbitMQ-simplest thing(简单队列)
  3. Tool之Adobe:解决Adobe acrobat pro将PDF文件进行批量保存单页PDF文件(图文教程)
  4. TVM:通过Python接口(AutoTVM)来编译和优化模型
  5. plt文件怎么转化为txt文件
  6. 反超 PowerDesigner,这个国产数据库建模工具很强
  7. android数据线接口分类,安卓手机数据线接口类型
  8. 计算机win是什么键,win键是哪个键,电脑win键在哪
  9. Dominant Resource Fairness: Fair Allocation of Multiple Resource Types
  10. MATLAB平台文字识别算法实现
  11. 一个简单的文本编辑小程序
  12. 如何重新设置苹果id密码_苹果手机ID密码忘了?别着急,这二种方法轻松帮你搞定!...
  13. 《Python编程快速上手——让繁琐的工作自动化》读书笔记4
  14. Ubuntu系统下编译C语言程序
  15. swift 中的get和set
  16. 计算2000年1月1日到2008年1月1日 相距多少天。
  17. GNS3安装ASA 8.42防火墙 ASA5520,包含asa842-vmlinuz 和asa842-initrd免费下载
  18. b站“视频评论区”抽奖 讲解(含JS源码)
  19. left join 索引笔记
  20. Android逆向——网易云音乐排行榜api(下)

热门文章

  1. 禁止群租将推动房租和房价暴涨
  2. 腾讯安全反病毒实验室解读“Wannacry”勒索软件
  3. java根据模板导出PDF详细教程(无bug版)
  4. 从摆摊开始,他和兄弟们开辟了一条致富路
  5. 视频监控系统选择硬盘,绿盘、蓝盘、紫盘、黑盘、红盘到底选择哪个?
  6. 【JavaSE】图书管理系统之MySQL版本
  7. Word控件Spire.Doc 【超链接】教程(4):如何修改Word文档中的超文本
  8. 【小随笔】你更喜欢哪个红(附代码)
  9. 如何用一台cisco1921-K9解决目前国内常见访问全网需求和流量区分策略?
  10. 针对importNew 网站中的《面试总结》一文中涉及到的问题,自己的一点总结(2)