微头条项目补充

1. 各层的接口和实现类

1.1 实体类和VO对象

NewsUser

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

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

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

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

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

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;
}

1.2 DAO层接口和实现类

BaseDao基础类,封装了公共的查询方法和公共的增删改方法

  • 注意,所有的Dao接口的实现类都要继承BaseDao

    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);
                      // 处理datetime类型字段和java.util.Data转换问题
                      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]);
              }
    
              // 执行 增删改 executeUpdate
              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层的所有接口

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层的所有实现类

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{
    
}

1.3 Service层接口和实现类

service层所有接口

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 {
    
}

1.4 Controller层接口和实现类

BaseController 用于将路径关联到处理方法的基础控制器

  • 所有的Controller都要继承该类

    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 {
    // 响应的MIME类型和乱码问题
    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类

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{
    
}

2.PostMan测试工具

2.1什么是PostMan

  • Postman是一个**接口测试工具**,在做接口测试的时候,Postman相当于一个客户端,它可以模拟用户发起的各类HTTP请求,将请求数据发送至服务端,获取对应的响应结果, 从而验证响应中的结果数据是否和预期值相匹配;并确保开发人员能够及时处理接口中的bug,进而保证产品上线之后的稳定性和安全性。 它主要是用来模拟各种HTTP请求的(如:get/post/delete/put..等等),Postman与浏览器的区别在于有的浏览器不能输出Json格式,而Postman更直观接口返回的结果。

2.2 怎么安装PostMan

  • 官网下载地址: https://www.getpostman.com ,或者使用资料中提供的安装包

  • 安装过程简单,一路next即可

  • 第一次启动postman 会要求输入用户名和密码,如果没有的话,关闭,再次启动就可以直接进入了

2.3 怎么使用PostMan

启动PostMan后,创建一个collection,在该collection下专门存放和微头条项目相关的测试

创建完毕后,增加新的接口测试

填写要测试的接口相关的路径,参数,请求体内容等信息

测试完毕后,可以选择将该接口的测试进行保存,方便后续随时再次测试

相关推荐
永乐春秋1 小时前
WEB攻防-通用漏洞&文件上传&js验证&mime&user.ini&语言特性
前端
鸽鸽程序猿1 小时前
【前端】CSS
前端·css
ggdpzhk1 小时前
VUE:基于MVVN的前端js框架
前端·javascript·vue.js
中云DDoS CC防护蔡蔡1 小时前
微信小程序被攻击怎么选择高防产品
服务器·网络安全·微信小程序·小程序·ddos
HPC_fac130520678162 小时前
以科学计算为切入点:剖析英伟达服务器过热难题
服务器·人工智能·深度学习·机器学习·计算机视觉·数据挖掘·gpu算力
yaoxin5211233 小时前
第二十七章 TCP 客户端 服务器通信 - 连接管理
服务器·网络·tcp/ip
学不会•3 小时前
css数据不固定情况下,循环加不同背景颜色
前端·javascript·html
活宝小娜5 小时前
vue不刷新浏览器更新页面的方法
前端·javascript·vue.js
程序视点5 小时前
【Vue3新工具】Pinia.js:提升开发效率,更轻量、更高效的状态管理方案!
前端·javascript·vue.js·typescript·vue·ecmascript
coldriversnow5 小时前
在Vue中,vue document.onkeydown 无效
前端·javascript·vue.js