hibernate
hibernate 查询
hibernate操作的是对象 orm对象关系模型
1.查询一
/** * select street.sname,street.sid from Street as street * 查询得到的是一个 Object[] */ /*List<Object[]> list = session.createQuery("select street.sname,street.sid from Street as street").list(); for (Object[] objects : list) { System.out.println(objects[0]+"+"+objects[1]+"+"); }*/ /** *在Street类里面重写了一个构造方法new Street(street.sname,street.sid) * select new Street(street.sname,street.sid) from Street as street * 查询得到的是一个Street对象 */ List<Street> list = session.createQuery("select new Street(street.sname,street.sid) from Street as street").list();
2. 模糊查询
String hql = "from Street as street where street.sname like ?";//占位符 List<Street> list = session.createQuery(hql).setString(0, "%海%").list();
3.参数查询
String hql = "from Street as street where street.sid>3"; List<Street> list = session.createQuery(hql).list();
4.对象关联查询
String hql = "select provinces from Street street,Provinces provinces where street.provinces=provinces and street.sname='山路'"; List<Provinces> list = session.createQuery(hql).list();
5.分页查询
String hql = "from Street"; Query query = session.createQuery(hql); query.setFirstResult(0);//开始 query.setMaxResults(3);//页面大小 List<Street> list = query.list();
6.函数统计
String hql = "select count(street) from Street as street"; Query query = session.createQuery(hql);
7.排序查询
String hql = "select street from Street as street order by street.sid"; Query query = session.createQuery(hql); List<Street> list = query.list();
8.对象查询
Criteria c = session.createCriteria(Street.class); if(null !=street){ if(null!=street.getSname()){ c.add(Restrictions.like("sname", street.getSname(),MatchMode.ANYWHERE));//模糊查询。查询 sname 中像 street.getSname()的 } if(0!=street.getSid()){ c.add(Restrictions.ge("sid",street.getSid()));//street.getSid()大于等于sid } // if(0!=street.getSid()){ // c.add(Restrictions.le("sid",street.getSid()));//street.getSid()小于等于sid // } } c.addOrder(Order.asc("sid"));//可以增加多个排序规则 // c.addOrder(Order.desc("sid"));//可以增加多个排序规则 hUtil.sessionClose(session); return c.list();
9.查询
Street street = (Street)session.get(Street.class, sid);
10.添加
Provinces provinces = new Provinces(); provinces.setPid(pid); provinces.setPname(pname); session.save(provinces);
11.更新
street = (Street)session.load(Street.class, sid); street.setSname(sname); session.update(street);
12.删除
Street street = (Street)session.get(Street.class, id); session.delete(street);
相关推荐
LetonLIU 2020-03-28
tugangkai 2020-07-04
taisenki 2020-06-02
zzjmay 2020-05-17
shuiluobu 2020-03-20
solarspot 2014-06-27
LetonLIU 2019-12-23
东方咖啡屋 2019-12-08
浮生 2011-12-12
surpassdream 2011-12-11
keruis 2015-02-03
小黄鸡kimhuhg 2015-02-16
Lingforme 2015-07-23
bigcactus 2012-05-21
张浩BIT 2012-04-22
虽然我很菜 2016-01-08
WJI 2014-11-06
crazzylp 2014-01-08
netHibernate 2014-01-04