实体类的字段 意义不知道

你好!我在网上读取一些项目,其中有一个项目是将实体的字段,写成了type,我不值,会出现问题,传值也会出现问题,不知道它这个type是干什么用的哈
它在实体类中是 Byte
在Mysql中字段类型是 tinyint

img

如果是Id的,哪么为何不用这个Comment实体类的Id呢?

1、这个type是用来区分songId是歌曲的id还是歌单的ID,根据代码,type = 0时是歌曲的Id,要保存到comment的songId字段中,否则是歌单的Id,要保存到songListId字段中,所以不能用comment的Id,
你完全可以使用以下代码替换一下原代码


 public Object addComment(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
//        userId.trim();@ApiParam("主键Id")String userId,
        String userId = request.getParameter("userId");           //用户id
        String type = request.getParameter("type");               //类型(0歌曲1歌单 2专辑?不是这个字节!!!看下面)
        String songId = request.getParameter("songId");           //歌曲id
        String songListId = request.getParameter("songListId");   //歌单id
        String content = request.getParameter("content").trim();  //评论内容

        if (StringUtils.isEmpty(type) || type.equals("''")) {
            System.out.println("type不能为空");
            return null;
        }
        int in = Integer.parseInt(type);


        //保存到评论的对象中
        Comment comment = new Comment();
        comment.setUserId(Integer.parseInt(userId));

        comment.setType(in);
        if (in == 0) {
            comment.setSongId(Integer.parseInt(songId));
        } else {
            comment.setSongListId(Integer.parseInt(songListId));
        }
    }

有什么问题吗,只是个字典类型ID,为了节约存储使用了byte

type是Bean中定义的一个属性,该实体类属性type的类型为Byte,所以在传递参数(set方法)的时候需要传递一个Type类型的值,所以将通过 request.getParameter("type")的值用new Byte(type)方法,将String类型的值转换为Byte。