标签:var 字符串 color sql查询 调用 hql back 位置 print
Select/update/delete…… from …… where …… group by …… having …… order by …… asc/desc
1、 实体查询:
有关实体查询技术
1 String hql=”from User user ”; 2 List<User> list=session.CreateQuery(hql).list(); 上面的代码执行结果是,查询出User实体对象所对应的所有数据,而且将数据封装成User实体对象,并且放入List<User>中返回。这里需要注意的是,Hibernate的实体查
from User user where user.age=20; from User user where user.age between 20 and 30; from User user where user.age in(20,30); from User user where user.name is null; from User user where user.name like ‘%zx%’; from User user where (user.age%2)=1; from User user where user.age=20 and user.name like ‘%zx%’;
1 Transaction trans=session.beginTransaction(); 2 String hql=”update User user set user.age=20 where user.age=18”; 3 Query queryupdate=session.createQuery(hql); 4 int ret=queryupdate.executeUpdate(); 5 trans.commit();
通过这种方式我们可以在Hibernate3中,一次性完成批量数据的更新,对性能的提高是相当的可观。同样也可以通过类似的方式来完成delete操作,如下面的代码
1 Transaction trans=session.beginTransaction(); 2 String hql=”delete from User user where user.age=18”; 3 Query queryupdate=session.createQuery(hql); 4 int ret=queryupdate.executeUpdate(); 5 trans.commit();
1 List list=session.createQuery(“select user.name from User user ”).list(); 2 for(int i=0;i<list.size();i++){ 3 System.out.println(list.get(i)); 4 }
1 List list=session.createQuery(“select user.name,user.age from User user ”).list(); 2 for(int i=0;i<list.size();i++){ 3 Object[] obj=(Object[])list.get(i); 4 System.out.println(obj[0]); 5 System.out.println(obj[1]); 6 }
List list=session.createQuery(“select new User(user.name,user.age) from User user ”).list(); for(int i=0;i<list.size();i++){ User user=(User)list.get(i); System.out.println(user.getName()); System.out.println(user.getAge()); }
1 List list=session.createQuery(“select new User(user.name,user.age) from User user ”).list(); 2 for(int i=0;i<list.size();i++){ 3 User user=(User)list.get(i); 4 user.setName(“gam”); 5 session.saveOrUpdate(user); 6 /*这里将会实际执行一个save操作,而不会执行update操作,因这User对象的id属性为null,Hibernate会把它作为一个自由对 7 象(请参考持久化对象状态部分的论述),因此会对它执行save操作。*/ 8 }
4、 分组与排序
A、Order by子句:
与SQL语句相似,HQL查询也可以通过order by子句对查询结果集进行排序,并且可以通过asc或者desc关键字指定排序方式,如下面的代码:
from User user order by user.name asc,user.age desc;
1 String hql=”select count(user),user.age from User user group by user.age having count(user)>10 ”; 2 List list=session.createQuery(hql).list();
customer ID varchar2(14) age number(10) name varchar2(20) order ID varchar2(14) order_number number(10) customer_ID varchar2(14)
from Customer c inner join c.orders o group by c.age;(1) select c.ID,c.name,c.age,o.ID,o.order_number,o.customer_ID from Customer c inner join c.orders c group by c.age;(2)
1 Query query=session.createQuery(“from User user where user.name=:customername and user.customerage=:age ”); 2 query.setString(“customername”,name); 3 query.setInteger(“customerage”,age);
1 Query query=session.createQuery(“from User user where user.name=? and user.age =? ”); 2 query.setString(0,name); 3 query.setInteger(1,age);
1 String hql=”from User user where user.name=:customername ”; 2 Query query=session.createQuery(hql); 3 query.setParameter(“customername”,name,Hibernate.STRING);
1 Customer customer=new Customer(); 2 customer.setName(“pansl”); 3 customer.setAge(80); 4 Query query=session.createQuery(“from Customer c where c.name=:name and c.age=:age ”); 5 query.setProperties(customer);
1 Customer customer=(Customer)session.load(Customer.class,”1”); 2 Query query=session.createQuery(“from Order order where order.customer=:customer ”); 3 query. setProperties(“customer”,customer); 4 List list=query.list();
Select * from order where customer_ID=’1’;
标签:var 字符串 color sql查询 调用 hql back 位置 print
原文地址:https://www.cnblogs.com/zl520/p/9973767.html