查询单个对象和集合:
public PostInfo getMolde(Object objId) throws SQLException,
NoSuchMethodException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
PostInfo p=new PostInfo();
p.setUserId((Integer)objId);//得到用户编号
String sql=BeanUitl.getSelectSQL(p);
Connection con=C3p0ConnectionHelper.getConnection();
//查询得到一个实体对象
PostInfo u1= queryRunner.query(con,sql,new BeanHandler<PostInfo>(PostInfo.class));
Integer userId=u1.getUserId();//得到用户ID
UserInfoDao userDao=new UserInfoDao();
UserInfo userInfo=userDao.getMolde(userId);
u1.setPostUserInfo(userInfo);
C3p0ConnectionHelper.closeConnection();
return u1;
}
@Override
public List<PostInfo> getList(PostInfo t) throws SQLException,
NoSuchMethodException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
String sql=BeanUitl.getSelectSQL(t);
Connection con=C3p0ConnectionHelper.getConnection();
//查询得到集合对象
List<PostInfo> list=queryRunner.query(con, sql,new BeanListHandler<PostInfo>(PostInfo.class));
UserInfoDao userDao=new UserInfoDao();
for(PostInfo postInfo:list){
Integer userId=postInfo.getUserId();//得到用户ID
UserInfo userInfo=userDao.getMolde(userId);
postInfo.setPostUserInfo(userInfo);
}
C3p0ConnectionHelper.closeConnection();
return list;
}
}