第八章 微头条项目开发
一 项目简介
1.1 微头条业务简介
微头条新闻发布和浏览平台,主要包含业务如下
- 用户功能
- 头条新闻
- 新闻的分页浏览
- 通过标题关键字搜索新闻
- 查看新闻详情
- 新闻的修改和删除
- 权限控制
1.2 技术栈介绍
前端技术栈
- ES6作为基础JS语法
- nodejs用于运行环境
- npm用于项目依赖管理工具
- vite用于项目的构建架工具
- Vue3用于项目数据的渲染框架
- Axios用于前后端数据的交互
- Router用于页面的跳转
- Pinia用于存储用户的数据
- LocalStorage作为用户校验token的存储手段
- Element-Plus提供组件
后端技术栈
- JAVA作为开发语言,版本为JDK17
- Tomcat作为服务容器,版本为10.1.7
- Mysql8用于项目存储数据
- Servlet用于控制层实现前后端数据交互
- JDBC用于实现数据的CURD
- Druid用于提供数据源的连接池
- MD5用于用户密码的加密
- Jwt用于token的生成和校验
- Jackson用于转换JSON
- Filter用于用户登录校验和跨域处理
- Lombok用于处理实体类
1.3 功能展示
头条首页信息搜索

登录功能

注册功能

权限控制功能

发布头条功能

修改头条功能

删除头条功能

二 前端项目环境搭建


- 进入项目后打开集成终端或者在src上右击选择在集成终端中打开



三 后端项目环境搭建
3.1 数据库准备
news_users 用户表
news_type 新闻类型表
news_headline 新闻信息表
数据库创建SQL
3.2 MVC项目架构模式
MVC(Model View Controller)是软件工程中的一种**软件架构模式,它把软件系统分为模型、视图和控制器**三个基本部分。用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。
MVC模式下,项目中的常见包
M:
- 实体类包(pojo /entity /bean) 专门存放和数据库对应的实体类和一些VO对象
- 数据库访问包(dao/mapper) 专门存放对数据库不同表格CURD方法封装的一些类
- 服务包(service) 专门存放对数据进行业务逻辑预算的一些类
C:
- 控制层包(controller)
V:
- web目录下的视图资源 html css js img 等
- 前端工程化后,在后端项目中已经不存在了

3.3 搭建项目
3.3.1 创建WEB项目

3.3.2 导入依赖

3.3.3 准备包结构

- controller 控制层代码,主要由Servlet组成
- service 服务层代码,主要用于处理业务逻辑
- dao 数据访问层,主要用户定义对于各个表格的CURD的方法
- pojo 实体类层,主要用于存放和数据库对应的实体类以及一些VO对象
- util 工具类包,主要用存放一些工具类
- common 公共包,主要用户存放一些其他公共代码
- filters 过滤器包,专门用于存放一些过滤器
- test 测试代码包,专门用于定义一些测试的功能代码,上线前应该删掉,后期用maven可以自动处理掉
3.5 准备工具类
3.5.1 异步响应规范格式类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package com.atguigu.headline.common;
public class Result<T> { private Integer code; private String message; private T data; public Result(){} protected static <T> Result<T> build(T data) { Result<T> result = new Result<T>(); if (data != null) result.setData(data); return result; } public static <T> Result<T> build(T body, Integer code, String message) { Result<T> result = build(body); result.setCode(code); result.setMessage(message); return result; } public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) { Result<T> result = build(body); result.setCode(resultCodeEnum.getCode()); result.setMessage(resultCodeEnum.getMessage()); return result; }
public static<T> Result<T> ok(T data){ Result<T> result = build(data); return build(data, ResultCodeEnum.SUCCESS); } public Result<T> message(String msg){ this.setMessage(msg); return this; } public Result<T> code(Integer code){ this.setCode(code); return this; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.atguigu.headline.common;
public enum ResultCodeEnum {
SUCCESS(200,"success"), USERNAME_ERROR(501,"usernameError"), PASSWORD_ERROR(503,"passwordError"), NOTLOGIN(504,"notLogin"), USERNAME_USED(505,"userNameUsed") ;
private Integer code; private String message; private ResultCodeEnum(Integer code, String message) { this.code = code; this.message = message; } public Integer getCode() { return code; } public String getMessage() { return message; } }
|
3.5.2 MD5加密工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.atguigu.headline.util;
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;
public final class MD5Util { public static String encrypt(String strSrc) { try { char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; byte[] bytes = strSrc.getBytes(); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(bytes); bytes = md.digest(); int j = bytes.length; char[] chars = new char[j * 2]; int k = 0; for (int i = 0; i < bytes.length; i++) { byte b = bytes[i]; chars[k++] = hexChars[b >>> 4 & 0xf]; chars[k++] = hexChars[b & 0xf]; } return new String(chars); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new RuntimeException("MD5加密出错!!+" + e); } } }
|
3.5.3 JDBCUtil连接池工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| package com.atguigu.headline.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties;
public class JDBCUtil { private static ThreadLocal<Connection> threadLocal =new ThreadLocal<>();
private static DataSource dataSource; static{ Properties properties =new Properties(); InputStream resourceAsStream = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); try { properties.load(resourceAsStream); } catch (IOException e) { throw new RuntimeException(e); }
try { dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { throw new RuntimeException(e); }
} public static DataSource getDataSource(){ return dataSource; }
public static Connection getConnection(){ Connection connection = threadLocal.get(); if (null == connection) { try { connection = dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } threadLocal.set(connection); }
return connection; }
public static void releaseConnection(){ Connection connection = threadLocal.get(); if (null != connection) { threadLocal.remove(); try { connection.setAutoCommit(true); connection.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } }
|
1 2 3 4 5 6 7
| driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/top_news username=root password=root initialSize=5 maxActive=10 maxWait=1000
|
3.5.4 JwtHelper工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package com.atguigu.headline.util;
import com.alibaba.druid.util.StringUtils; import io.jsonwebtoken.*;
import java.util.Date;
public class JwtHelper { private static long tokenExpiration = 24*60*60*1000; private static String tokenSignKey = "123456";
public static String createToken(Long userId) { String token = Jwts.builder()
.setSubject("YYGH-USER") .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration)) .claim("userId", userId) .signWith(SignatureAlgorithm.HS512, tokenSignKey) .compressWith(CompressionCodecs.GZIP) .compact(); return token; }
public static Long getUserId(String token) { if(StringUtils.isEmpty(token)) return null; Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token); Claims claims = claimsJws.getBody(); Integer userId = (Integer)claims.get("userId"); return userId.longValue(); }
public static boolean isExpiration(String token){ try { boolean isExpire = Jwts.parser() .setSigningKey(tokenSignKey) .parseClaimsJws(token) .getBody() .getExpiration().before(new Date()); return isExpire; }catch(Exception e) { return true; } } }
|
3.5.5 JSON转换的WEBUtil工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package com.atguigu.headline.util;
import com.atguigu.headline.common.Result; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.BufferedReader; import java.io.IOException; import java.text.SimpleDateFormat;
public class WebUtil { private static ObjectMapper objectMapper; static{ objectMapper=new ObjectMapper(); objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); } public static <T> T readJson(HttpServletRequest request,Class<T> clazz){ T t =null; BufferedReader reader = null; try { reader = request.getReader(); StringBuffer buffer =new StringBuffer(); String line =null; while((line = reader.readLine())!= null){ buffer.append(line); }
t= objectMapper.readValue(buffer.toString(),clazz); } catch (IOException e) { throw new RuntimeException(e); } return t; } public static void writeJson(HttpServletResponse response, Result result){ response.setContentType("application/json;charset=UTF-8"); try { String json = objectMapper.writeValueAsString(result); response.getWriter().write(json); } catch (IOException e) { throw new RuntimeException(e); } } }
|
3.6 准备各层的接口和实现类
3.6.1 准备实体类和VO对象
NewsUser
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.atguigu.headline.pojo;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable;
@AllArgsConstructor @NoArgsConstructor @Data public class NewsUser implements Serializable { private Integer uid; private String username; private String userPwd; private String nickName; }
|
NewsType
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.atguigu.headline.pojo;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable;
@AllArgsConstructor @NoArgsConstructor @Data public class NewsType implements Serializable { private Integer tid; private String tname; }
|
NewsHeadline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.atguigu.headline.pojo;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; import java.util.Date;
@AllArgsConstructor @NoArgsConstructor @Data public class NewsHeadline implements Serializable { private Integer hid; private String title; private String article; private Integer type; private Integer publisher; private Integer pageViews; private Date createTime; private Date updateTime; private Integer isDeleted;
}
|
HeadlineQueryVo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.atguigu.headline.pojo.vo;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable;
@AllArgsConstructor @NoArgsConstructor @Data public class HeadlineQueryVo implements Serializable { private String keyWords; private Integer type ; private Integer pageNum; private Integer pageSize; }
|
HeadlinePageVo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.atguigu.headline.pojo.vo;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable;
@AllArgsConstructor @NoArgsConstructor @Data public class HeadlinePageVo implements Serializable { private Integer hid; private String title; private Integer type; private Integer pageViews; private Long pastHours; private Integer publisher; }
|
HeadlineDetailVo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.atguigu.headline.pojo.vo;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable;
@AllArgsConstructor @NoArgsConstructor @Data public class HeadlineDetailVo implements Serializable { private Integer hid; private String title; private String article; private Integer type; private String typeName; private Integer pageViews; private Long pastHours; private Integer publisher; private String author; }
|
3.6.2 DAO层接口和实现类

BaseDao基础类,封装了公共的查询方法和公共的增删改方法
注意,所有的Dao接口的实现类都要继承BaseDao
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| package com.atguigu.headline.dao;
import com.atguigu.headline.util.JDBCUtil; import java.lang.reflect.Field; import java.sql.*; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List;
public class BaseDao { public <T> T baseQueryObject(Class<T> clazz, String sql, Object ... args) { T t = null; Connection connection = JDBCUtil.getConnection(); PreparedStatement preparedStatement = null; ResultSet resultSet = null; int rows = 0; try { preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < args.length; i++) { preparedStatement.setObject(i + 1, args[i]); }
resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { t = (T) resultSet.getObject(1); } } catch (Exception e) { throw new RuntimeException(e); } finally { if (null != resultSet) { try { resultSet.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (null != preparedStatement) { try { preparedStatement.close(); } catch (SQLException e) { throw new RuntimeException(e); }
} JDBCUtil.releaseConnection(); } return t; }
public <T> List<T> baseQuery(Class clazz, String sql, Object ... args){ List<T> list =new ArrayList<>(); Connection connection = JDBCUtil.getConnection(); PreparedStatement preparedStatement=null; ResultSet resultSet =null; int rows = 0; try { preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < args.length; i++) { preparedStatement.setObject(i+1,args[i]); }
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount();
while (resultSet.next()) { Object obj =clazz.getDeclaredConstructor().newInstance();
for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnLabel(i); Object value = resultSet.getObject(columnName); if(value.getClass().equals(LocalDateTime.class)){ value= Timestamp.valueOf((LocalDateTime) value); } Field field = clazz.getDeclaredField(columnName); field.setAccessible(true); field.set(obj,value); }
list.add((T)obj); }
} catch (Exception e) { throw new RuntimeException(e); } finally { if (null !=resultSet) { try { resultSet.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (null != preparedStatement) { try { preparedStatement.close(); } catch (SQLException e) { throw new RuntimeException(e); } } JDBCUtil.releaseConnection(); } return list; } public int baseUpdate(String sql,Object ... args) { Connection connection = JDBCUtil.getConnection(); PreparedStatement preparedStatement=null; int rows = 0; try { preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < args.length; i++) { preparedStatement.setObject(i+1,args[i]); }
rows = preparedStatement.executeUpdate();
} catch (SQLException e) { throw new RuntimeException(e); } finally { if (null != preparedStatement) { try { preparedStatement.close(); } catch (SQLException e) { throw new RuntimeException(e); }
} JDBCUtil.releaseConnection(); } return rows; } }
|
dao层的所有接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.atguigu.headline.dao; public interface NewsHeadLineDao { }
package com.atguigu.headline.dao; public interface NewsTypeDao { }
package com.atguigu.headline.dao; public interface NewsUserDao {
}
|
dao层所有实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.atguigu.headline.dao.impl; import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsHeadLineDao; public class NewsHeadlineDaoImpl extends BaseDao implements NewsHeadLineDao{ }
package com.atguigu.headline.dao.impl; import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsTypeDao; public class NewsTypeDaoImpl extends BaseDao implements NewsTypeDao{ }
package com.atguigu.headline.dao.impl; import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsUserDao; public class NewsUserDaoImpl extends BaseDao implements NewsUserDao{ }
|
3.6.3 Service层接口和实现类

service层所有接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.atguigu.headline.service; public interface NewsHeadlineService { }
package com.atguigu.headline.service; public interface NewsTypeService { List<NewsType> findAll(); }
package com.atguigu.headline.service; public interface NewsUserService { }
|
service层所有实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.atguigu.headline.service.impl; import com.atguigu.headline.service.NewsHeadlineService; public class NewsHeadlineServiceImpl implements NewsHeadlineService { }
package com.atguigu.headline.service.impl; import com.atguigu.headline.service.NewsTypeService; public class NewsTypeServiceImpl implements NewsTypeService { }
package com.atguigu.headline.service.impl; import com.atguigu.headline.service.NewsUserService; public class NewsUserServiceImpl implements NewsUserService { }
|
3.6.4 Controller层接口和实现类
BaseController 用于将路径关联到处理方法的基础控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package com.atguigu.headline.controller;
import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.lang.reflect.Method;
public class BaseController extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/json;charset=UTF-8");
String requestURI = req.getRequestURI(); String[] split = requestURI.split("/"); String methodName =split[split.length-1]; Class clazz = this.getClass(); try { Method method=clazz.getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class); method.setAccessible(true); method.invoke(this,req,resp); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } } }
|
所有的Controller类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.atguigu.headline.controller; import jakarta.servlet.annotation.WebServlet; @WebServlet("/headline/*") public class NewsHeadlineController extends BaseController { }
package com.atguigu.headline.controller; import jakarta.servlet.annotation.WebServlet; @WebServlet("/type/*") public class NewsTypeController { }
package com.atguigu.headline.controller; import jakarta.servlet.annotation.WebServlet; @WebServlet("/user/*") public class NewsUserController extends BaseController{ }
package com.atguigu.headline.controller; import jakarta.servlet.annotation.WebServlet; @WebServlet("/portal/*") public class PortalController extends BaseController{ }
|
3.7 开发跨域CORS过滤器
3.7.1 什么是跨域
同源策略(Sameoriginpolicy)是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现。**同源策略会阻止一个域的javascript脚本和另外一个域的内容进行交互。所谓同源(即指在同一个域)就是两个页面具有相同的协议(protocol),主机(host)和端口号**
3.7.2 为什么会产生跨域
前后端分离模式下,客户端请求前端服务器获取视图资源,然后客户端自行向后端服务器获取数据资源,前端服务器的 协议,IP和端口和后端服务器很可能是不一样的,这样就产生了跨域

3.7.3 如何解决跨域
前端项目代理模式处理

后端跨域过滤器方式处理

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.atguigu.headline.filters;
import jakarta.servlet.*; import jakarta.servlet.annotation.WebFilter; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/*") public class CrosFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) servletResponse; HttpServletRequest request =(HttpServletRequest) servletRequest; response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With"); if(!request.getMethod().equalsIgnoreCase("OPTIONS")){ filterChain.doFilter(servletRequest, servletResponse); } } }
|
- 未来我们使用框架,直接用一个@CrossOrigin 就可以解决跨域问题了
四 PostMan测试工具
4.1 什么是PostMan
- Postman是一个**
接口测试工具**,在做接口测试的时候,Postman相当于一个客户端,它可以模拟用户发起的各类HTTP请求,将请求数据发送至服务端,获取对应的响应结果, 从而验证响应中的结果数据是否和预期值相匹配;并确保开发人员能够及时处理接口中的bug,进而保证产品上线之后的稳定性和安全性。 它主要是用来模拟各种HTTP请求的(如:get/post/delete/put..等等),Postman与浏览器的区别在于有的浏览器不能输出Json格式,而Postman更直观接口返回的结果。
4.2 怎么安装PostMan
4.3 怎么使用PostMan
启动PostMan后,创建一个collection,在该collection下专门存放和微头条项目相关的测试
创建完毕后,增加新的接口测试
填写要测试的接口相关的路径,参数,请求体内容等信息
测试完毕后,可以选择将该接口的测试进行保存,方便后续随时再次测试
五 登录注册功能
5.1 登录表单提交

需求描述
- 用户在客户端输入用户名密码并向后端提交,后端根据用户名和密码判断登录是否成功,用户有误或者密码有误响应不同的提示信息
uri:
请求方式:
请求参数
1 2 3 4
| { "username":"zhangsan", "userPwd":"123456" }
|
响应示例
1 2 3 4 5 6 7
| { "code":"200", "message":"success" "data":{ "token":"... ..." } }
|
1 2 3 4 5
| { "code":"501", "message":"用户名有误" "data":{} }
|
1 2 3 4 5
| { "code":"503", "message":"密码有误" "data":{} }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.common.ResultCodeEnum; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.NewsUser; import com.atguigu.headline.service.NewsUserService; import com.atguigu.headline.service.impl.NewsUserServiceImpl; import com.atguigu.headline.util.JwtHelper; import com.atguigu.headline.util.MD5Util; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.util.HashMap; import java.util.Map;
@WebServlet("/user/*") public class NewsUserController extends BaseController{
private NewsUserService newsUserService =new NewsUserServiceImpl();
protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { NewsUser newsUser = WebUtil.readJson(req, NewsUser.class);
Result result =null; NewsUser loginNewsUser =newsUserService.findByUserName(newsUser.getUsername()); if (null != loginNewsUser) { if(loginNewsUser.getUserPwd().equals(MD5Util.encrypt(newsUser.getUserPwd()))){ Map<String,Object> data =new HashMap<>(); String token = JwtHelper.createToken(loginNewsUser.getUid().longValue()); data.put("token",token); result=Result.ok(data); }else{ result=Result.build(null, ResultCodeEnum.PASSWORD_ERROR); } }else{ result=Result.build(null, ResultCodeEnum.USERNAME_ERROR); } WebUtil.writeJson(resp,result); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.atguigu.headline.service;
import com.atguigu.headline.pojo.NewsUser;
public interface NewsUserService {
NewsUser findByUserName(String username); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.atguigu.headline.service.impl;
import com.atguigu.headline.dao.NewsUserDao; import com.atguigu.headline.dao.impl.NewsUserDaoImpl; import com.atguigu.headline.pojo.NewsUser; import com.atguigu.headline.service.NewsTypeService; import com.atguigu.headline.service.NewsUserService; import com.atguigu.headline.util.MD5Util;
public class NewsUserServiceImpl implements NewsUserService { private NewsUserDao newsUserDao =new NewsUserDaoImpl(); @Override public NewsUser findByUserName(String username) { return newsUserDao.findByUserName(username); } }
|
1 2 3 4 5 6 7 8 9 10 11
| package com.atguigu.headline.dao; import com.atguigu.headline.pojo.NewsUser;
public interface NewsUserDao {
NewsUser findByUserName(String username); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.atguigu.headline.dao.impl;
import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsUserDao; import com.atguigu.headline.pojo.NewsUser; import java.util.List;
public class NewsUserDaoImpl extends BaseDao implements NewsUserDao {
@Override public NewsUser findByUserName(String username) { String sql ="select uid,username,user_pwd userPwd ,nick_name nickName from news_user where username = ?"; List<NewsUser> newsUserList = baseQuery(NewsUser.class, sql, username); if (null != newsUserList && newsUserList.size()>0){ return newsUserList.get(0); } return null; } }
|
5.2 根据token获取完整用户信息
需求描述
- 客户端发送请求,提交token请求头,后端根据token请求头获取登录用户的详细信息并响应给客户端进行存储
uri
请求方式
请求头
响应示例
1 2 3 4 5 6 7 8 9 10 11 12
| { "code": 200, "message": "success", "data": { "loginUser": { "uid": 1, "username": "zhangsan", "userPwd": "", "nickName": "张三" } } }
|
1 2 3 4 5
| { "code": 504, "message": "notLogin", "data": null }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.common.ResultCodeEnum; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.NewsUser; import com.atguigu.headline.service.NewsUserService; import com.atguigu.headline.service.impl.NewsUserServiceImpl; import com.atguigu.headline.util.JwtHelper; import com.atguigu.headline.util.MD5Util; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.util.HashMap; import java.util.Map;
@WebServlet("/user/*") public class NewsUserController extends BaseController{
private NewsUserService newsUserService =new NewsUserServiceImpl();
protected void getUserInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String token = req.getHeader("token"); Result result =Result.build(null,ResultCodeEnum.NOTLOGIN); if(null!= token){ if (!JwtHelper.isExpiration(token)) { Integer uid = JwtHelper.getUserId(token).intValue(); NewsUser newsUser =newsUserService.findByUid(uid); newsUser.setUserPwd(""); Map<String,Object> data =new HashMap<>(); data.put("loginUser",newsUser); result=Result.ok(data);
} } WebUtil.writeJson(resp,result); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.atguigu.headline.service;
import com.atguigu.headline.pojo.NewsUser;
public interface NewsUserService {
NewsUser findByUid(Integer uid); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.atguigu.headline.service.impl;
import com.atguigu.headline.dao.NewsUserDao; import com.atguigu.headline.dao.impl.NewsUserDaoImpl; import com.atguigu.headline.pojo.NewsUser; import com.atguigu.headline.service.NewsTypeService; import com.atguigu.headline.service.NewsUserService; import com.atguigu.headline.util.MD5Util;
public class NewsUserServiceImpl implements NewsUserService { private NewsUserDao newsUserDao =new NewsUserDaoImpl();
@Override public NewsUser findByUid(Integer uid) { return newsUserDao.findByUid(uid); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.atguigu.headline.dao;
import com.atguigu.headline.pojo.NewsUser;
public interface NewsUserDao {
NewsUser findByUid(Integer uid); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.atguigu.headline.dao.impl;
import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsUserDao; import com.atguigu.headline.pojo.NewsUser; import java.util.List;
public class NewsUserDaoImpl extends BaseDao implements NewsUserDao { @Override public NewsUser findByUid(Integer uid) { String sql ="select uid,username,user_pwd userPwd ,nick_name nickName from news_user where uid = ?"; List<NewsUser> newsUserList = baseQuery(NewsUser.class, sql, uid); if (null != newsUserList && newsUserList.size()>0){ return newsUserList.get(0); } return null; } }
|
5.3 注册时用户名占用校验
需求说明
- 用户在注册时输入用户名时,立刻将用户名发送给后端,后端根据用户名查询用户名是否可用并做出响应
uri:
请求方式:
请求参数
响应示例
1 2 3 4 5
| { "code":"200", "message":"success" "data":{} }
|
1 2 3 4 5
| { "code":"505", "message":"用户名占用" "data":{} }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.common.ResultCodeEnum; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.NewsUser; import com.atguigu.headline.service.NewsUserService; import com.atguigu.headline.service.impl.NewsUserServiceImpl; import com.atguigu.headline.util.JwtHelper; import com.atguigu.headline.util.MD5Util; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.util.HashMap; import java.util.Map;
@WebServlet("/user/*") public class NewsUserController extends BaseController{
private NewsUserService newsUserService =new NewsUserServiceImpl();
protected void checkUserName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); NewsUser newsUser = newsUserService.findByUserName(username); Result result=null; if (null == newsUser){ result=Result.ok(null); }else{ result=Result.build(null,ResultCodeEnum.USERNAME_USED); } WebUtil.writeJson(resp,result); } }
|
5.4 注册表单提交
需求说明
- 客户端将新用户信息发送给服务端,服务端将新用户存入数据库,存入之前做用户名是否被占用校验,校验通过响应成功提示,否则响应失败提示
uri:
请求方式:
请求参数
1 2 3 4 5
| { "username":"zhangsan", "userPwd":"123456", "nickName":"张三" }
|
响应示例
1 2 3 4 5
| { "code":"200", "message":"success" "data":{} }
|
1 2 3 4 5
| { "code":"505", "message":"用户名占用" "data":{} }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.common.ResultCodeEnum; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.NewsUser; import com.atguigu.headline.service.NewsUserService; import com.atguigu.headline.service.impl.NewsUserServiceImpl; import com.atguigu.headline.util.JwtHelper; import com.atguigu.headline.util.MD5Util; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map;
@WebServlet("/user/*") public class NewsUserController extends BaseController{
private NewsUserService newsUserService =new NewsUserServiceImpl();
protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { NewsUser newsUser = WebUtil.readJson(req, NewsUser.class); NewsUser usedUser = newsUserService.findByUserName(newsUser.getUsername()); Result result=null; if (null == usedUser){ newsUserService.registUser(newsUser); result=Result.ok(null); }else{ result=Result.build(null,ResultCodeEnum.USERNAME_USED); } WebUtil.writeJson(resp,result); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.atguigu.headline.service;
import com.atguigu.headline.pojo.NewsUser;
public interface NewsUserService {
int registUser(NewsUser newsUser); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.atguigu.headline.service.impl;
import com.atguigu.headline.dao.NewsUserDao; import com.atguigu.headline.dao.impl.NewsUserDaoImpl; import com.atguigu.headline.pojo.NewsUser; import com.atguigu.headline.service.NewsTypeService; import com.atguigu.headline.service.NewsUserService; import com.atguigu.headline.util.MD5Util;
public class NewsUserServiceImpl implements NewsUserService { @Override public int registUser(NewsUser newsUser) { newsUser.setUserPwd(MD5Util.encrypt(newsUser.getUserPwd())); return newsUserDao.insertNewsUser(newsUser); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.atguigu.headline.dao;
import com.atguigu.headline.pojo.NewsUser;
public interface NewsUserDao {
int insertNewsUser(NewsUser newsUser); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.atguigu.headline.dao.impl;
import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsUserDao; import com.atguigu.headline.pojo.NewsUser; import java.util.List;
public class NewsUserDaoImpl extends BaseDao implements NewsUserDao { @Override public int insertNewsUser(NewsUser newsUser) { String sql ="insert into news_user values(DEFAULT,?,?,?)"; return baseUpdate(sql,newsUser.getUsername(),newsUser.getUserPwd(),newsUser.getNickName()); } }
|
六 头条首页功能
6.1 查询所有头条分类

需求说明
uri:
请求方式
请求参数
响应示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| { "code":"200", "message":"OK" "data": [ { "tid":"1", "tname":"新闻" }, { "tid":"2", "tname":"体育" }, { "tid":"3", "tname":"娱乐" }, { "tid":"4", "tname":"科技" }, { "tid":"5", "tname":"其他" } ] }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.pojo.NewsType; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo; import com.atguigu.headline.service.NewsHeadlineService; import com.atguigu.headline.service.NewsTypeService; import com.atguigu.headline.service.impl.NewsHeadlineServiceImpl; import com.atguigu.headline.service.impl.NewsTypeServiceImpl; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map;
@WebServlet("/portal/*") public class PortalController extends BaseController{
private NewsHeadlineService headlineService=new NewsHeadlineServiceImpl(); private NewsTypeService newsTypeService=new NewsTypeServiceImpl();
protected void findAllTypes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<NewsType> newsTypeList =newsTypeService.findAll(); WebUtil.writeJson(resp,Result.ok(newsTypeList)); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.atguigu.headline.service;
import com.atguigu.headline.pojo.NewsType;
import java.util.List;
public interface NewsTypeService {
List<NewsType> findAll(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.atguigu.headline.service.impl;
import com.atguigu.headline.dao.NewsTypeDao; import com.atguigu.headline.dao.impl.NewsTypeDaoImpl; import com.atguigu.headline.pojo.NewsType; import com.atguigu.headline.service.NewsTypeService;
import java.util.List;
public class NewsTypeServiceImpl implements NewsTypeService { private NewsTypeDao newsTypeDao =new NewsTypeDaoImpl(); @Override public List<NewsType> findAll() { return newsTypeDao.findAll(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.atguigu.headline.dao;
import com.atguigu.headline.pojo.NewsType;
import java.util.List;
public interface NewsTypeDao {
List<NewsType> findAll(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.atguigu.headline.dao.impl;
import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsTypeDao; import com.atguigu.headline.pojo.NewsType;
import java.util.List;
public class NewsTypeDaoImpl extends BaseDao implements NewsTypeDao { @Override public List<NewsType> findAll() { String sql ="select tid,tname from news_type"; return baseQuery(NewsType.class, sql); } }
|
6.2 分页带条件查询所有头条

需求说明
- 客户端向服务端发送查询关键字,新闻类别,页码数,页大小
- 服务端根据条件搜索分页信息,返回含页码数,页大小,总页数,总记录数,当前页数据等信息,并根据时间降序,浏览量降序排序
uri:
请求方式:
请求参数:
1 2 3 4 5 6
| { "keyWords":"马斯克", "type":0, "pageNum":1, "pageSize":"10" }
|
响应示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| { "code":"200", "message":"success" "data":{ "pageInfo":{ "pageData":[ { "hid":"1", "title":"尚硅谷宣布 ... ...", "type":"1", "pageViews":"40", "pastHours":"3" , "publisher":"1" }, { "hid":"1", "title":"尚硅谷宣布 ... ...", "type":"1", "pageViews":"40", "pastHours":"3", "publisher":"1" }, { "hid":"1", "title":"尚硅谷宣布 ... ...", "type":"1", "pageViews":"40", "pastHours":"3", "publisher":"1" } ], "pageNum":1, "pageSize":10, "totalPage":20, "totalSize":200 } } }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.pojo.NewsType; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo; import com.atguigu.headline.service.NewsHeadlineService; import com.atguigu.headline.service.NewsTypeService; import com.atguigu.headline.service.impl.NewsHeadlineServiceImpl; import com.atguigu.headline.service.impl.NewsTypeServiceImpl; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map;
@WebServlet("/portal/*") public class PortalController extends BaseController{
private NewsHeadlineService headlineService=new NewsHeadlineServiceImpl(); private NewsTypeService newsTypeService=new NewsTypeServiceImpl();
protected void findNewsPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HeadlineQueryVo headLineQueryVo = WebUtil.readJson(req, HeadlineQueryVo.class); Map<String,Object> pageInfo =headlineService.findPage(headLineQueryVo); Map<String,Object> pageInfoMap=new HashMap<>(); pageInfoMap.put("pageInfo",pageInfo); WebUtil.writeJson(resp, Result.ok(pageInfoMap)); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.atguigu.headline.service;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List; import java.util.Map;
public interface NewsHeadlineService {
Map<String, Object> findPage(HeadlineQueryVo headLineQueryVo); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.atguigu.headline.service.impl;
import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.dao.impl.NewsHeadlineDaoImpl; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo; import com.atguigu.headline.service.NewsHeadlineService;
import java.util.HashMap; import java.util.List; import java.util.Map;
public class NewsHeadlineServiceImpl implements NewsHeadlineService { private NewsHeadLineDao newsHeadLineDao =new NewsHeadlineDaoImpl();
@Override public Map<String, Object> findPage(HeadlineQueryVo headLineQueryVo) { Map<String,Object> pageInfo =new HashMap<>(); List<HeadlinePageVo> pageData =newsHeadLineDao.findPageList(headLineQueryVo); int totalSize = newsHeadLineDao.findPageCount(headLineQueryVo); int pageSize =headLineQueryVo.getPageSize(); int totalPage=totalSize%pageSize == 0 ? totalSize/pageSize : totalSize/pageSize+1; int pageNum= headLineQueryVo.getPageNum(); pageInfo.put("pageData",pageData); pageInfo.put("pageNum",pageNum); pageInfo.put("pageSize",pageSize); pageInfo.put("totalPage",totalPage); pageInfo.put("totalSize",totalSize);
return pageInfo; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.atguigu.headline.dao;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List;
public interface NewsHeadLineDao {
int findPageCount(HeadlineQueryVo headLineQueryVo);
List<HeadlinePageVo> findPageList(HeadlineQueryVo headLineQueryVo); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| package com.atguigu.headline.dao.impl;
import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.LinkedList; import java.util.List;
public class NewsHeadlineDaoImpl extends BaseDao implements NewsHeadLineDao{
@Override public int findPageCount(HeadlineQueryVo headLineQueryVo) { List<Object> args =new LinkedList<>(); String sql="select count(1) from news_headline where is_deleted=0 "; StringBuilder sqlBuffer =new StringBuilder(sql) ; String keyWords = headLineQueryVo.getKeyWords(); if (null != keyWords && keyWords.length()>0){ sqlBuffer.append("and title like ? "); args.add("%"+keyWords+"%"); } Integer type = headLineQueryVo.getType(); if(null != type && type != 0){ sqlBuffer.append("and type = ? "); args.add(type); }
Object[] argsArr = args.toArray(); System.out.println(sqlBuffer.toString()); Long totalSize = baseQueryObject(Long.class, sqlBuffer.toString(), argsArr); return totalSize.intValue(); }
@Override public List<HeadlinePageVo> findPageList(HeadlineQueryVo headLineQueryVo) { List<Object> args =new LinkedList<>(); String sql="select hid,title,type,page_views pageViews,TIMESTAMPDIFF(HOUR,create_time,NOW()) pastHours,publisher from news_headline where is_deleted=0 "; StringBuilder sqlBuffer =new StringBuilder(sql) ; String keyWords = headLineQueryVo.getKeyWords(); if (null != keyWords && keyWords.length()>0){ sqlBuffer.append("and title like ? "); args.add("%"+keyWords+"%"); } Integer type = headLineQueryVo.getType(); if(null != type && type != 0){ sqlBuffer.append("and type = ? "); args.add(type); }
sqlBuffer.append("order by pastHours , page_views desc "); sqlBuffer.append("limit ? , ?"); args.add((headLineQueryVo.getPageNum()-1)*headLineQueryVo.getPageSize()); args.add(headLineQueryVo.getPageSize());
Object[] argsArr = args.toArray(); System.out.println(sqlBuffer.toString()); List<HeadlinePageVo> pageData = baseQuery(HeadlinePageVo.class, sqlBuffer.toString(), argsArr);
return pageData; } }
|
6.3 查看头条详情

需求说明
- 用户点击”查看全文”时,向服务端发送新闻id
- 后端根据新闻id查询完整新闻文章信息并返回
- 后端要同时让新闻的浏览量+1
uri
1
| portal/showHeadlineDetail
|
请求方式
请求参数
响应示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| { "code":"200", "message":"success", "data":{ "headline":{ "hid":"1", "title":"马斯克宣布 ... ...", "article":"... ..." "type":"1", "typeName":"科技", "pageViews":"40", "pastHours":"3" , "publisher":"1" , "author":"张三" } } }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.pojo.NewsType; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo; import com.atguigu.headline.service.NewsHeadlineService; import com.atguigu.headline.service.NewsTypeService; import com.atguigu.headline.service.impl.NewsHeadlineServiceImpl; import com.atguigu.headline.service.impl.NewsTypeServiceImpl; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map;
@WebServlet("/portal/*") public class PortalController extends BaseController{
private NewsHeadlineService headlineService=new NewsHeadlineServiceImpl(); private NewsTypeService newsTypeService=new NewsTypeServiceImpl();
protected void showHeadlineDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Integer hid =Integer.parseInt(req.getParameter("hid")); HeadlineDetailVo headlineDetailVo =headlineService.findHeadlineDetail(hid); Map<String ,Object> data =new HashMap<>(); data.put("headline",headlineDetailVo); WebUtil.writeJson(resp,Result.ok(data)); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.atguigu.headline.service;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List; import java.util.Map;
public interface NewsHeadlineService {
HeadlineDetailVo findHeadlineDetail(Integer hid); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.atguigu.headline.service.impl;
import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.dao.impl.NewsHeadlineDaoImpl; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo; import com.atguigu.headline.service.NewsHeadlineService;
import java.util.HashMap; import java.util.List; import java.util.Map;
public class NewsHeadlineServiceImpl implements NewsHeadlineService { private NewsHeadLineDao newsHeadLineDao =new NewsHeadlineDaoImpl(); @Override public HeadlineDetailVo findHeadlineDetail(Integer hid) { newsHeadLineDao.increasePageViews(hid); return newsHeadLineDao.findHeadlineDetail(hid); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.atguigu.headline.dao;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List;
public interface NewsHeadLineDao {
HeadlineDetailVo findHeadlineDetail(Integer hid); int increasePageViews(Integer hid); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.atguigu.headline.dao.impl;
import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.LinkedList; import java.util.List;
public class NewsHeadlineDaoImpl extends BaseDao implements NewsHeadLineDao{ @Override public HeadlineDetailVo findHeadlineDetail(Integer hid) { String sql ="select hid,title,article,type, tname typeName ,page_views pageViews,TIMESTAMPDIFF(HOUR,create_time,NOW()) pastHours,publisher,nick_name author from news_headline h left join news_type t on h.type = t.tid left join news_user u on h.publisher = u.uid where hid = ?"; List<HeadlineDetailVo> headlineDetailVoList = baseQuery(HeadlineDetailVo.class, sql, hid); if(null != headlineDetailVoList && headlineDetailVoList.size()>0) return headlineDetailVoList.get(0); return null; } @Override public int increasePageViews(Integer hid) { String sql ="update news_headline set page_views = page_views +1 where hid =?"; return baseUpdate(sql,hid); } }
|
七 头条发布修改和删除
7.1 登录校验
需求说明
- 客户端在进入发布页前、发布新闻前、进入修改页前、修改前、删除新闻前先向服务端发送请求携带token请求头
- 后端接收token请求头后,校验用户登录是否过期并做响应
- 前端根据响应信息提示用户进入登录页还是进入正常业务页面
uri
请求方式
请求参数
请求头
响应示例
1 2 3 4 5
| { "code":"200", "message":"success", "data":{} }
|
1 2 3 4 5
| { "code":"504", "message":"loginExpired", "data":{} }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.common.ResultCodeEnum; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.NewsUser; import com.atguigu.headline.service.NewsUserService; import com.atguigu.headline.service.impl.NewsUserServiceImpl; import com.atguigu.headline.util.JwtHelper; import com.atguigu.headline.util.MD5Util; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.util.HashMap; import java.util.Map;
@WebServlet("/user/*") public class NewsUserController extends BaseController{
private NewsUserService newsUserService =new NewsUserServiceImpl();
protected void checkLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String token = req.getHeader("token"); Result result =Result.build(null,ResultCodeEnum.NOTLOGIN); if(null!= token){ if (!JwtHelper.isExpiration(token)) { result=Result.ok(null); } } WebUtil.writeJson(resp,result); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.atguigu.headline.filters;
import com.atguigu.headline.common.Result; import com.atguigu.headline.common.ResultCodeEnum; import com.atguigu.headline.util.JwtHelper; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.*; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request =(HttpServletRequest) servletRequest; String token = request.getHeader("token"); boolean flag =false; if (null != token ){ boolean expiration = JwtHelper.isExpiration(token); if (!expiration ){ flag=true; } } if (flag){ filterChain.doFilter(servletRequest,servletResponse); }else{ WebUtil.writeJson((HttpServletResponse) servletResponse, Result.build(null, ResultCodeEnum.NOTLOGIN)); } } }
|
1 2 3 4 5 6 7 8 9
| <filter> <filter-name>loginFilter</filter-name> <filter-class>com.atguigu.headline.filters.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>loginFilter</filter-name> <url-pattern>/headline/*</url-pattern> </filter-mapping>
|
7.2 提交发布头条

需求说明
- 用户在客户端输入发布的新闻信息完毕后
- 发布前先请求后端的登录校验接口验证登录
- 登录通过则提交新闻信息
- 后端将新闻信息存入数据库
uri
请求方式
请求头
请求参数
1 2 3 4 5
| { "title":"尚硅谷宣布 ... ...", "article":"... ...", "type":"1" }
|
响应示例
1 2 3 4 5
| { "code":"200", "message":"success", "data":{} }
|
1 2 3 4 5
| { "code":"504", "message":"loginExpired", "data":{} }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.service.NewsHeadlineService; import com.atguigu.headline.service.impl.NewsHeadlineServiceImpl; import com.atguigu.headline.util.JwtHelper; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.util.HashMap; import java.util.Map;
@WebServlet("/headline/*") public class NewsHeadlineController extends BaseController {
private NewsHeadlineService newsHeadlineService =new NewsHeadlineServiceImpl();
protected void publish(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { NewsHeadline newsHeadline = WebUtil.readJson(req, NewsHeadline.class); String token = req.getHeader("token"); Long userId = JwtHelper.getUserId(token); newsHeadline.setPublisher(userId.intValue()); newsHeadlineService.addNewsHeadline(newsHeadline); WebUtil.writeJson(resp,Result.ok(null)); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.atguigu.headline.service;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List; import java.util.Map;
public interface NewsHeadlineService {
int addNewsHeadline(NewsHeadline newsHeadline); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.atguigu.headline.service.impl;
import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.dao.impl.NewsHeadlineDaoImpl; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo; import com.atguigu.headline.service.NewsHeadlineService;
import java.util.HashMap; import java.util.List; import java.util.Map;
public class NewsHeadlineServiceImpl implements NewsHeadlineService { private NewsHeadLineDao newsHeadLineDao =new NewsHeadlineDaoImpl(); public int addNewsHeadline(NewsHeadline newsHeadline) { return newsHeadLineDao.addNewsHeadline(newsHeadline); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.atguigu.headline.dao;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List;
public interface NewsHeadLineDao {
int addNewsHeadline(NewsHeadline newsHeadline); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.atguigu.headline.dao.impl;
import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.LinkedList; import java.util.List;
public class NewsHeadlineDaoImpl extends BaseDao implements NewsHeadLineDao{ @Override public int addNewsHeadline(NewsHeadline newsHeadline) { String sql = "insert into news_headline values(DEFAULT,?,?,?,?,0,NOW(),NOW(),0)";
return baseUpdate( sql, newsHeadline.getTitle(), newsHeadline.getArticle(), newsHeadline.getType(), newsHeadline.getPublisher() ); } }
|
7.3 修改头条回显

需求说明
- 前端先调用登录校验接口,校验登录是否过期
- 登录校验通过后 ,则根据新闻id查询新闻的完整信息并响应给前端
uri
1
| headline/findHeadlineByHid
|
请求方式
请求参数
响应示例
1 2 3 4 5 6 7 8 9 10 11 12
| { "code":"200", "message":"success", "data":{ "headline":{ "hid":"1", "title":"马斯克宣布", "article":"... ... ", "type":"2" } } }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.service.NewsHeadlineService; import com.atguigu.headline.service.impl.NewsHeadlineServiceImpl; import com.atguigu.headline.util.JwtHelper; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.util.HashMap; import java.util.Map;
@WebServlet("/headline/*") public class NewsHeadlineController extends BaseController { private NewsHeadlineService newsHeadlineService =new NewsHeadlineServiceImpl();
protected void findHeadlineByHid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Integer hid = Integer.parseInt(req.getParameter("hid")); NewsHeadline newsHeadline =newsHeadlineService.findHeadlineByHid(hid); Map<String ,Object> data =new HashMap<>(); data.put("headline",newsHeadline); WebUtil.writeJson(resp,Result.ok(data));
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.atguigu.headline.service;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List; import java.util.Map;
public interface NewsHeadlineService {
NewsHeadline findHeadlineByHid(Integer hid); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.atguigu.headline.service.impl;
import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.dao.impl.NewsHeadlineDaoImpl; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo; import com.atguigu.headline.service.NewsHeadlineService;
import java.util.HashMap; import java.util.List; import java.util.Map;
public class NewsHeadlineServiceImpl implements NewsHeadlineService { private NewsHeadLineDao newsHeadLineDao =new NewsHeadlineDaoImpl(); @Override public NewsHeadline findHeadlineByHid(Integer hid) { return newsHeadLineDao.findHeadlineByHid(hid); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.atguigu.headline.dao;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List;
public interface NewsHeadLineDao { NewsHeadline findHeadlineByHid(Integer hid); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.atguigu.headline.dao.impl;
import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.LinkedList; import java.util.List;
public class NewsHeadlineDaoImpl extends BaseDao implements NewsHeadLineDao{ @Override public NewsHeadline findHeadlineByHid(Integer hid) { String sql ="select hid,title,article,type,publisher,page_views pageViews from news_headline where hid =?"; List<NewsHeadline> newsHeadlineList = baseQuery(NewsHeadline.class, sql, hid); if(null != newsHeadlineList && newsHeadlineList.size()>0) return newsHeadlineList.get(0); return null; } }
|
7.4 保存修改
需求描述
- 客户端将新闻信息修改后,提交前先请求登录校验接口校验登录状态
- 登录校验通过则提交修改后的新闻信息,后端接收并更新进入数据库
uri
请求方式
请求参数
1 2 3 4 5 6
| { "hid":"1", "title":"尚硅谷宣布 ... ...", "article":"... ...", "type":"2" }
|
响应示例
1 2 3 4 5
| { "code":"200", "message":"success", "data":{} }
|
1 2 3 4 5
| { "code":"504", "message":"loginExpired", "data":{} }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.service.NewsHeadlineService; import com.atguigu.headline.service.impl.NewsHeadlineServiceImpl; import com.atguigu.headline.util.JwtHelper; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.util.HashMap; import java.util.Map;
@WebServlet("/headline/*") public class NewsHeadlineController extends BaseController { private NewsHeadlineService newsHeadlineService =new NewsHeadlineServiceImpl();
protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { NewsHeadline newsHeadline = WebUtil.readJson(req, NewsHeadline.class); newsHeadlineService.updateNewsHeadline(newsHeadline); WebUtil.writeJson(resp,Result.ok(null)); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.atguigu.headline.service;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List; import java.util.Map;
public interface NewsHeadlineService { int updateNewsHeadline(NewsHeadline newsHeadline); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.atguigu.headline.service.impl;
import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.dao.impl.NewsHeadlineDaoImpl; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo; import com.atguigu.headline.service.NewsHeadlineService;
import java.util.HashMap; import java.util.List; import java.util.Map;
public class NewsHeadlineServiceImpl implements NewsHeadlineService { private NewsHeadLineDao newsHeadLineDao =new NewsHeadlineDaoImpl(); @Override public int updateNewsHeadline(NewsHeadline newsHeadline) { return newsHeadLineDao.updateNewsHeadline(newsHeadline); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.atguigu.headline.dao;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List;
public interface NewsHeadLineDao { int updateNewsHeadline(NewsHeadline newsHeadline); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.atguigu.headline.dao.impl;
import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.LinkedList; import java.util.List;
public class NewsHeadlineDaoImpl extends BaseDao implements NewsHeadLineDao{ @Override public int updateNewsHeadline(NewsHeadline newsHeadline) { String sql ="update news_headline set title = ?, article= ? , type =? , update_time = NOW() where hid = ? "; return baseUpdate( sql, newsHeadline.getTitle(), newsHeadline.getArticle(), newsHeadline.getType(), newsHeadline.getHid() ); } }
|
7.5 删除头条

需求说明
- 将要删除的新闻id发送给服务端
- 服务端校验登录是否过期,未过期则直接删除,过期则响应登录过期信息
uri
请求方式
请求参数
响应示例
1 2 3 4 5
| { "code":"200", "message":"success", "data":{} }
|
1 2 3 4 5 6
| { "code":"504", "message":"loginExpired", "data":{} }
|
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package com.atguigu.headline.controller;
import com.atguigu.headline.common.Result; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.service.NewsHeadlineService; import com.atguigu.headline.service.impl.NewsHeadlineServiceImpl; import com.atguigu.headline.util.JwtHelper; import com.atguigu.headline.util.WebUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.util.HashMap; import java.util.Map;
@WebServlet("/headline/*") public class NewsHeadlineController extends BaseController { private NewsHeadlineService newsHeadlineService =new NewsHeadlineServiceImpl();
protected void removeByHid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Integer hid = Integer.parseInt(req.getParameter("hid")); newsHeadlineService.removeByHid(hid); WebUtil.writeJson(resp,Result.ok(null)); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.atguigu.headline.service;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List; import java.util.Map;
public interface NewsHeadlineService { int removeByHid(Integer hid); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.atguigu.headline.service.impl;
import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.dao.impl.NewsHeadlineDaoImpl; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo; import com.atguigu.headline.service.NewsHeadlineService;
import java.util.HashMap; import java.util.List; import java.util.Map;
public class NewsHeadlineServiceImpl implements NewsHeadlineService { private NewsHeadLineDao newsHeadLineDao =new NewsHeadlineDaoImpl(); @Override public int removeByHid(Integer hid) { return newsHeadLineDao.removeByHid(hid); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.atguigu.headline.dao;
import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.List;
public interface NewsHeadLineDao { int removeByHid(Integer hid); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.atguigu.headline.dao.impl;
import com.atguigu.headline.dao.BaseDao; import com.atguigu.headline.dao.NewsHeadLineDao; import com.atguigu.headline.pojo.NewsHeadline; import com.atguigu.headline.pojo.vo.HeadlineDetailVo; import com.atguigu.headline.pojo.vo.HeadlinePageVo; import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import java.util.LinkedList; import java.util.List;
public class NewsHeadlineDaoImpl extends BaseDao implements NewsHeadLineDao{ @Override public int removeByHid(Integer hid) { String sql ="update news_headline set is_deleted =1 , update_time =NOW() where hid = ? "; return baseUpdate(sql,hid); }
|