1 基于hibernate, 获得所有列。
Query query = session.createQuery("from mytable as ta where ta.name like :name and a.email=:email");
query.setParameter("name","%"+user_name+"%");
query.setParameter("email",user_email);
/*
也可以用,注意从1开始。
query.setParameter(1,"%"+user_name+"%");
query.setParameter(2,user_email);
*/
//Join
from mytable as ta join content as co where 1=1 and ta.user_id=co.user_id and ...
2 获得部分列
三种方法:
*返回元素为Object[]的列表。
Query query = session.createQuery("select name,pass from User as u");
遍历列表:
List<Map<String,String> result =new ArrayList<Map<String,String>();
for(Object[] obj :query.getResultList())
{
Map map=new HashMap<String,String>();
map.put("name",obj[0]);
map.put("pass",obj[1]);
result.add(map);
}
*返回List
Query query = session.createQuery("select new List(u.name,u.pass) from User as u");
返回结果[["test1","123"],["test2","345"]...]
*返回Map
Query query = session.createQuery("select new Map(u.name as name,u.pass as pass) from User as u");
返回结果[{"name"="test1","pass"="123"},{"name"="test2","pass"="345"}...]