求救。。查数据库的值显示为NULL,但是转JSON的时候显示出来了值

public String findCommentByNewsId(String id) {
		Integer i = Integer.parseInt(id);
		System.out.println("service = " + new CommentDao().findCommentByNewsId(i).toString());
		System.out.println("serviceGson = " + new Gson().toJson(new CommentDao().findCommentByNewsId(i)));
		return new Gson().toJson(new CommentDao().findCommentByNewsId(i));
	}
public List<Comment> findCommentByNewsId(int id){
		System.out.println("dao = " + Comment.dao.find("select * from comment_info where comment_belone = ?",id).toString());
		return Comment.dao.find("select * from comment_info where comment_belone = " + id);
	}
dao = [Comment{comment_id=null, comment_author=null, comment_class=null, comment_content='null', comment_likeNum=null, comment_time=null, comment_belone=null}, Comment{comment_id=null, comment_author=null, comment_class=null, comment_content='null', comment_likeNum=null, comment_time=null, comment_belone=null}]
service = [Comment{comment_id=null, comment_author=null, comment_class=null, comment_content='null', comment_likeNum=null, comment_time=null, comment_belone=null}, Comment{comment_id=null, comment_author=null, comment_class=null, comment_content='null', comment_likeNum=null, comment_time=null, comment_belone=null}]
serviceGson = [{"attrs":{"comment_likenum":12,"comment_time":"十一月 27, 2019","comment_belone":1,"comment_author":1,"comment_class":3,"commentId":1,"comment_content":"涌起的情绪一言两语说不清楚,惊喜中带着不置信,随之而来的感动。"}},{"attrs":{"comment_likenum":23,"comment_time":"十一月 28, 2019","comment_belone":1,"comment_author":1,"comment_class":3,"commentId":2,"comment_content":"涌起的情绪一言两语说不清楚,惊喜中带着不置信,随之而来的感动。"}}]

转JSON的时候他居然能在控制台输出出来数据,但是两个调用toString方法打印出来的却是null

public class Comment extends Model<Comment>{
    private Integer commentId;
    private Integer comment_author;
    private Integer comment_class;
    private String comment_content;
    private Integer comment_likeNum;
    private Date comment_time;
    private Integer comment_belone;
    
    public static final Comment dao = new Comment().dao();
    
    public Comment(){}

    public Integer getCommentId() {
        return commentId;
    }

    public void setCommentId(Integer comment_id) {
        this.commentId = comment_id;
    }


评论区

JFinal

2019-11-28 22:12

单步调试跟踪一下

flyinkeke

2019-11-28 22:14

下载官方demo看一下,用Model生成工具类生成,不要手写。Comment应该继承BaseComment,BaseComment里只有setter getter。至于为什么转成json还能能看到数据,这就是jfinal的独特之处。不要用Gson,要用自带的JsonKit或是MixedJsonFactory。转成json后看不到attrs里的数据才正常

LFF

2019-12-01 21:24

@JFinal 您好,感谢帮助;调试之后发现了JFinal的查数据库的缓存方法了,会有一个attrs的集合来用键值来存放,不是直接赋值,所以调用自己重写的toString方法的时候打印出来了null

LFF

2019-12-01 21:26

@flyinkeke 您好,按照这个Demo改写了,现在可以正常的拿到数据了,非常感谢

热门反馈

扫码入社