现在的位置: 首页 > 综合 > 正文

hibernate 只查询某个字段

2015年07月25日 ⁄ 综合 ⁄ 共 545字 ⁄ 字号 评论关闭

第一种查询方法:

String HQL = "select new Usertab(a.userpwd) from Usertab a";

这样以后,List list = q.list;

for(int i=0;i<list.size;i++){
   Usertab user = list.get(i);
   //以下操作省略
}

这样你就能得到只封装了userpwd的Usertab对象了..

至于你的那个Usertab类,你这样写:

//属性省略
public Usertab(){}//不带参的构造方法

public Usertab(String userpwd){  //带参的构造方法
   this.userpwd = userpwd;
}

第二种

String HQL = "select new Admin(id,account,password) from Admin a where a.account=? and a.password=?";
其他方式相同

Admin.java:

    public Admin(){}
    
    public Admin(Long id,String account,String password){
        this.id=id;
        this.account=account;
        this.password=password;
    }

抱歉!评论已关闭.