数据准备
/** * 本地sql查询,原生sql */ public void sqlQuery(){ //返回Object数组 @SuppressWarnings("unchecked") List<Object> list = session.createSQLQuery("select uname,upwd from user").list(); System.out.println("[uname:"+((Object[])list.get(0))[0]+" upwd:"+((Object[])list.get(0))[1]+"]"); //addEntity(Class clazz) 转化为java bean @SuppressWarnings("unchecked") List<User> userList= session.createSQLQuery("select * from user").addEntity(User.class).list(); System.out.println(userList.get(0)); //setParameterList 多参数 in not in Integer[] ids = {1,2}; @SuppressWarnings("unchecked") List<User> userlist2 = session.createSQLQuery("select * from user where id in :id"). addEntity(User.class).setParameterList("id",ids).list(); System.out.println(userlist2); }
/** * 命名查询 采用 @NamedQueries 和@NamedQuery注解 * @NamedQueries({ * @NamedQuery(name="user.findUserByName",query="select u from User u where u.uname=:uname") * }) */ public void namedQuery(){ //返回单挑数据 uniqueResult(); User u = (User)session.getNamedQuery("user.findUserByName").setString("uname", "admin").uniqueResult(); System.out.println(u); }
/** * 查询指定字段,过滤不需要的数据,节省空间。 * 还有就是hibernate关联太多 查询对象全部信息容易堆栈溢出 * */ public void toMapOrBeanQuery(){ //查询指定字段不经过特殊处理 返回的是Object[]类型 Object[] o = (Object[])session.createQuery("select u.uname,u.age,u.email from User u whereu.uname='admin'").uniqueResult(); System.out.println("[uname:"+o[0]+" age:"+o[1] +" email:"+o[2]+"]"); //封装为 Map List<Map<String, Object>> mapList = (List<Map<String, Object>>)session.createQuery("select new Map(u.uname as uname,u.email as email,u.age as age) from User u").list(); for(Map map : mapList){ System.out.println(map.get("uname")); //uname 是 as 后面的别名 System.out.println(map.get("email")); } //封装成Java Bean --> new User(u.uname,u.email,u.age) 必须有指定的构造方法 List<User> list = (List<User>)session.createQuery("select new User(u.uname,u.email,u.age) from User u").list(); System.out.println(list); }
/** * 带关联关系的 指定字段查询 */ public void toMapOrBeanQuery2(){ //这个封装成Map 比较简单,和上面的一样 String hql = "select new Map(u.uname as uname,c.type as type,c.cardId as cardid) from User u,Card c where (u.id = c.user.id)"; List<Map<String, Object>> mapList = (List<Map<String, Object>>)session.createQuery(hql).list(); System.out.println(mapList); //[{type=身份证, cardid=511325xxx, uname=admin}, {type=银行卡, cardid=62122xxx, uname=admin}] //封装javabean 下面的构造方法有点难以理解 ,只可意会 //多对一方 查询方式 /** 构造方法如下 * public Card(String cardId, String type, String uname) { super(); this.cardId = cardId; this.type = type; User u = new User(); u.setUname(uname); this.user = u; } */ String hql2 = "select new Card(c.type,c.cardId,c.user.uname) from User u,Card c where (u.id = c.user.id)"; List<Card> list = session.createQuery(hql2).list(); System.out.println(list); //[Card [id=0, cardId=身份证, type=511325xxx, username=admin], Card [id=0, cardId=银行卡, type=62122xxx, username=admin]] //一对多的方式 /** * 这里有 多的一方的两个字段 * public User(String uname, String type,String card) { super(); this.uname = uname; List<Card> li = new ArrayList<Card>(); Card c = new Card(); c.setCardId(card); c.setType(type); li.add(c); this.list = li; } */ String hql3 = "select new User(u.uname,c.type,c.cardId) from User u,Card c where (u.id = c.user.id)"; List<User> list2 = session.createQuery(hql3).list(); System.out.println(list2.get(0).getList().get(0).getCardId()); System.out.println(list2.get(1).getList().get(0).getCardId()); for(User u:list2){ System.out.println(u.getUname()); for(Card c :u.getList()){ System.out.println(c.getType() +" " + c.getCardId()); } } }