62,616
社区成员
发帖
与我相关
我的任务
分享
private static QueryRunner qr;
static {
qr = new QueryRunner(JdbcUtils.getDataSource());
}
/*
* 查找评论
*/
public List<Comment> find(String blogID) {
try {
String sql = "select child.commentID,u.userName,child.commentContent,count(child.commentID) depth,child.commentParentID,child.commentTime from comment2 parent,comment2 child,user u where child.blogID = ? and child.blogID = parent.blogID and parent.left<=child.left and parent.right>=child.right and child.userID = u.userID group by child.commentID order by child.left";
Object params[] = { blogID };
List<Comment> list = qr.query(sql, new BeanListHandler<Comment>(
Comment.class), params);
sql = "select u.userName from user u,comment c where c.commentID = ? and u.userID = c.userID";
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getCommentParentID() != null) {
Object params1[] = { list.get(i).getCommentParentID() };
String userName = qr.query(sql,new ScalarHandler<String>(), params1);
Comment commentParent = qr.query(sql, new BeanHandler<Comment>(Comment.class), params1);
System.out.println(sql+ commentParent.getUserName() );
list.get(i).setCommentParent(commentParent);
}
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/*
* 查找评论
*/
public List<Comment> find(String blogID) {
try {
String sql = "select child.commentID,u.userName,child.commentContent,count(child.commentID) depth,child.commentParentID,child.commentTime from comment2 parent,comment2 child,user u where child.blogID = ? and child.blogID = parent.blogID and parent.left<=child.left and parent.right>=child.right and child.userID = u.userID group by child.commentID order by child.left";
Object params[] = { blogID };
List<Comment> list = qr.query(sql, new BeanListHandler<Comment>(
Comment.class), params);
sql = "select u.userName from user u,comment c where c.commentID = ? and u.userID = c.userID";
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getCommentParentID() != null) {
//增加的获取queryrunner
qr = new QueryRunner(JdbcUtils.getDataSource());
Object params1[] = { list.get(i).getCommentParentID() };
String userName = qr.query(sql,new ScalarHandler<String>(1), params1);
//增加的后台打印结果
System.out.println(list.get(i).getCommentParentID() +":"+ userName);
}
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}