思路,先select from User 出所有的List<User> allUser 然后用for循环 for(int i=0 i<allUser.size,i++){ userId=allUser[i].getUserId List<UserPic> aa = String hql="from UserPic p where p.user.userId = ?userId order by p.biaozu desc,p.picId desc"; List<UserPic> bb .add(aa.get[0]) } 这样的思路应该可以,就是效率一定很低,请高手说说有什么好办法。
分两次查呗,反正在一个事物里,影响不大; 最好biaoZu上建个索引,查找更快 1,select * from UserPic p where biaoZu=1 and userid=? 2,select * from UserPic p where userid=? and id= max(id); sql语句是错的,思路就这样了
想到个好玩的写法:用order by + 分页找出来; select * from UserPic pwhere userid=? order by biaoZu,picId desc limit(0,1)
楼上的高手,我要每人一张图片,所以要select所有的人,再找出每个人的一张图片,人与图片是一对多的,就是select人后怎么有效率的找出对应的图片。难道要像下面这样????,天啊 思路,先select from User 出所有的List<User> allUser 然后用for循环 for(int i=0 i<allUser.size,i++){ userId=allUser[i].getUserId List<UserPic> aa = String hql="from UserPic p where p.user.userId = ?userId order by p.biaozu desc,p.picId desc"; List<UserPic> bb .add(aa.get[0]) } 这样的思路应该可以,就是效率一定很低,请高手说说有什么好办法。
如果你用纯粹sql的,首先你需要的是一个列表 ,每列是 {user信息,正确的一张pic}。所以你就不用from user了。直接按照 select * from UserPic p where userid=? order by biaoZu,picId desc limit(0,1)来执行。 如果查询的信息不够,就关联user表查询 select * from (select a.username,b.picId from user a,UserPic b where a.userid=b.userid order by b.biaozu,b.picId desc limit 0,1) t order by t.username;之类的~~ 数据库不支持limit就换join或者其他的方法。前台分页需要的什么类型的list,就按照那个样式add 进去。 hql具体实现是可以的,但是会查询无用的信息,效率也是个问题,就比如你给的那个,hibernate不是万能的,这也是为什么mybatis能存活下来的一个原因
写出高效率的hql,这个确实难度大,先确保能正确性吧
思路是分两次向数据库查
第一次查biaoZu为1
第二次差biaoZu为0的
dao层合并
出所有的List<User> allUser
然后用for循环
for(int i=0 i<allUser.size,i++){
userId=allUser[i].getUserId
List<UserPic> aa = String hql="from UserPic p where p.user.userId = ?userId order by p.biaozu desc,p.picId desc";
List<UserPic> bb .add(aa.get[0])
}
这样的思路应该可以,就是效率一定很低,请高手说说有什么好办法。
最好biaoZu上建个索引,查找更快
1,select * from UserPic p where biaoZu=1 and userid=?
2,select * from UserPic p where userid=? and id= max(id);
sql语句是错的,思路就这样了
select * from UserPic pwhere userid=? order by biaoZu,picId desc limit(0,1)
思路,先select from User
出所有的List<User> allUser
然后用for循环
for(int i=0 i<allUser.size,i++){
userId=allUser[i].getUserId
List<UserPic> aa = String hql="from UserPic p where p.user.userId = ?userId order by p.biaozu desc,p.picId desc";
List<UserPic> bb .add(aa.get[0])
}
这样的思路应该可以,就是效率一定很低,请高手说说有什么好办法。
select * from UserPic p where userid=? order by biaoZu,picId desc limit(0,1)来执行。
如果查询的信息不够,就关联user表查询
select * from (select a.username,b.picId from user a,UserPic b where a.userid=b.userid order by b.biaozu,b.picId desc limit 0,1) t order by t.username;之类的~~
数据库不支持limit就换join或者其他的方法。前台分页需要的什么类型的list,就按照那个样式add 进去。
hql具体实现是可以的,但是会查询无用的信息,效率也是个问题,就比如你给的那个,hibernate不是万能的,这也是为什么mybatis能存活下来的一个原因
/**
* 是否已过期
*
* @return 是否已过期
*/
@Transient
public boolean isExpired() {
return getExpire() != null && new Date().after(getExpire());
}
isExpired 这个字段在数据库是不存在的,但是他是类的成员变量,然后里面加一些逻辑;
@Transient 该注解就表示他不关联数据库。