发表于: 2019-11-11 23:25:50
1 972
今天完成的事情:
总算把昨天的问题解决了,我对于DAO类,实体类,映射文件XML都是用插件generator自动生成的,他生成的实体类默认是包装类。而我昨天要使用的是复盘组员他自己手工敲的类,用的是基本类型。
而数值类的基本类型在不传参的情况下默认为0,我没有做判0的校验。所以一直获取不到正确的参数。后面在映射文件里加上判断就好了
他的属性
/**
* @Description: 用户年级
*/
private byte userClass;
我的映射文件
<!--列表查询留言表的留言与对应的用户信息-->
<select id="selectSelective" parameterType="com.happynewyear.admin.pojo.Comment" resultMap="BaseResultMap">
SELECT c.id,u.user_name,u.user_img,c.content,c.star,c.create_at,c.like_count,c.create_by,o.type,o.course
FROM
`comment` as c ,`user` as u ,study_object as o
<trim prefix="WHERE" prefixOverrides="AND|OR ">
c.create_by = u.user_id
<if test="id!=null">and c.id=#{id}</if>
<if test="user != null ">
<if test="user.userName != null and user.userName !=''">and u.user_name LIKE CONCAT('%',#{user.userName},'%')</if>
<if test="user.userClass != null and user.userClass !=0">and u.user_class= #{user.userClass}</if>
<if test="startTime != null and endTime != null ">and c.create_at between #{startTime} and #{endTime}</if>
</if>
</trim>
</select>
当然,这种问题其实不应该出现的,我们写实体类的时候,最好,说严重点,只能用包装类。包括在返回这样类的对象的时候,如果你对应属性不传参,或者没参数,我们获取的时候,默认会获取到0,这样其实是不好的,毕竟我们不需要这个0,特别有的地方,比如我们上下架的状态码是0和1,这样很容易出BUG的。
剩下的详细查看和删除
/**评论查看*/
@GetMapping("/a/u/comment")
public Map<String, Object> commentSelect(Long id) {
HashMap<String, Object> result = new HashMap<>(16);
log.info("获取留言{}", id);
try {
Comment comment = new Comment();
comment.setId(id);
List commentList = commentServer.selectSelective(comment);
result.put("code", SUCCESS.getCode());
result.put("msg", SUCCESS.getMsg());
result.put("data", commentList);
return result;
} catch (Exception e) {
e.printStackTrace();
result.put("code", REQUEST_FAILED.getCode());
result.put("msg", REQUEST_FAILED.getMsg());
return result;
}
}
/**评论删除*/
@DeleteMapping("/a/u/comment")
public Map<String, Object> commentDelete(Long id) {
HashMap<String, Object> result = new HashMap<>(16);
log.info("要删除的评论记录{}",id);
try {
/*查询要删除评论的对应学习对象,进行统计删除准备*/
StudyObject studyObject=new StudyObject();
Comment comment=new Comment();
comment.setId(id);
List<Comment> commentList=commentServer.selectSelective(comment);
studyObject.setId(commentList.get(0).getStudyObjectId());
studyObject.setCommentCount(-1);
int commentDeleteSuccess=commentServer.deleteByPrimaryKey(id);
/*删除成功后,学习对象对应统计减1*/
if (commentDeleteSuccess==1) {
studyObjectServer.updateCount(studyObject);
}
result.put("code", SUCCESS.getCode());
result.put("msg", SUCCESS.getMsg());
return result;
}catch (Exception e) {
e.printStackTrace();
result.put("code", REQUEST_FAILED.getCode());
result.put("msg", REQUEST_FAILED.getMsg());
return result;
}
}
今天因为统计留言评星级的问题,把表改了下,增加了统计每种星级的字段,当然,这样很不好,我的学习对象表已经很大了,
以后如果遇到统计功能的表,我会把统计参数单独放一张表,用类型区分,这样扩展性会比较好。而且统计这东西。
明天计划的事情:
和前端对接口,看微信支付
遇到的问题:
实体类用包装类,不要用基本类型
收获:
知道基本类型会有这个0的问题,一直没重视,在这里也没想到,现在算是涨教训了。。
评论