hql
(hibernate query language )
是一种 除了 orm 操作对象的另一种操作 对象的技术(基于orm)。
主要用于查询(支持增删改)。
a.=======================================
String hql = "from UserInfo";
Query query = session.createQuery(hql);
返回值 List<UserInfo> =( List<UserInfo>) query.list();
=======================================
b.=======================================
String hql = "select user_id,user_pwd from UserInfo";
Query query = session.createQuery(hql);
返回值 List<Object[]> =( List<Object[]>) query.list();
a b object[0] object[1]
1 2
=======================================
c.=======================================
com.it.vo.UserVo
String hql = "select new com.it.vo.UserVo(user_id,user_pwd) from UserInfo";
Query query = session.createQuery(hql);
返回值 List<UserVo> users =( List<UserVo>) query.list();
=======================================
d.=======================================
String hql = "select p.prov_name,c.city_name from City c join
c.prov p";
"select p.prov_name,c.city_name from Prov p right join
p.citys c";
Query query = session.createQuery(hql);
=======================================
e.=======================================
String hql = "from UserInfo where user_id="1001";
Query query = session.createQuery(hql);
UserInfo o =(UserInfo)query.uniqueResult();
=======================================
f.=======================================
String hql = "select new map(count(u)as ct,max(u.user_id)as mx) from UserInfo u";
Query query = session.createQuery(hql);
Map map =(Map)query.uniqueResult();
map.get("ct");
map.get("mx");
=======================================
g.=======================================
String hql = "from UserInfo";
Query query = session.createQuery(hql);
Iterator it =query.iterator();
=======================================
h.=======================================
String hql = "from UserInfo where user_id=? and user_pwd = ?";
Query query = session.createQuery(hql);
//为参数赋值
query.setString(index,value);
query.list();
=======================================
i.=======================================
String hql = "from UserInfo where user_id=:uid and user_pwd = :upwd";
Map params =new HashMap();
params.put("upwd",u.getUser_pwd());
params.put("uid",u.getUser_id());
super.query(hql,params);
public List query(String hql ,Map params)
Query query = session.createQuery(hql);
//为参数赋值
for(String key:params.keySet()){
query.setString(key,params.get(key));
}
query.list();