标签:tin creat 数据 别名 查询语句 des cts 其他 参数绑定
List<Customer> list = session.createQuery("from Customer").list();
for (Customer customer : list) {
System.out.println(customer);
}
// 使用别名
// 别名as可以省略
/* List<Customer> list =
session.createQuery("from Customer c").list();
System.out.println(list);*/
// 使用别名:带参数
/*List<Customer> list = session
.createQuery("from Customer as c where c.cname = ?")
.setString(0, "小沈").list();
System.out.println(list);*/
// 不支持 select * from Customer写法.可以写成 select 别名 from Customer as 别名;
List<Customer> list = session.createQuery("select c from Customer c").list();
System.out.println(list);
List<Customer> list = session.createQuery(
"from Customer c order by c.id desc").list();
for (Customer customer : list) {
System.out.println(customer);
}
Query query = session.createQuery("from Order");
query.setFirstResult(20);
query.setMaxResults(10);
List<Order> list = query.list();
for (Order order : list) {
System.out.println(order);
}
Customer customer = (Customer) session
.createQuery("from Customer where cname = ?")
.setString(0, "小明").uniqueResult();
System.out.println(customer);
// 1.使用?号方式绑定
/*Query query = session.createQuery("from Customer where cname = ?");
query.setString(0, "小沈");
List<Customer> list = query.list();
System.out.println(list);*/
/*Query query = session.createQuery("from Customer where cname = ? and cid =?");
query.setString(0, "小沈");
query.setInteger(1,3);
List<Customer> list = query.list();
System.out.println(list);*/
// 2.使用名称的方式绑定
Query query = session.createQuery("from Customer
where cname=:name and cid=:id");
query.setString("name", "小沈");
query.setInteger("id", 3);
List<Customer> list = query.list();
System.out.println(list);
// 3.绑定实体
List<Order> list = session
.createQuery("from Order o where o.customer = ?")
.setEntity(0, customer).list();
for (Order order : list) {
System.out.println(order);
}
// 查询客户的名称:
List<Object> list = session.createQuery(
"select c.cname from Customer c").list(); System.out.println(list);
List<Object[]> list = session.createQuery(
"select c.cid,c.cname from Customer c").list();
for (Object[] objects : list){
System.out.println(Arrays.toString(objects));
}
List<Customer> list = session.createQuery(
"select new Customer(cname) from Customer").list();
System.out.println(list);
Query query = session.createQuery("
from Customer where cname like ?");
query.setParameter(0, "小%");
List<Customer> list = query.list();
System.out.println(list);
List<Customer> list =session.createCriteria(Customer.class).list();
for (Customer customer : list) {
System.out.println(customer);
}
List<Customer> list = session.createCriteria(Customer.class)
.addOrder(org.hibernate.criterion.Order.desc("id")).list();
for (Customer customer : list) {
System.out.println(customer);
}
Criteria criteria = session.createCriteria(Order.class);
criteria.setFirstResult(10);
criteria.setMaxResults(10);
List<Order> list = criteria.list();
for (Order order : list) {
System.out.println(order);
}
Customer customer = (Customer) session.createCriteria(Customer.class)
.add(Restrictions.eq("cname", "小明")).uniqueResult();
System.out.println(customer);
/*
List<Customer> list = session.createCriteria(Customer.class)
.add(Restrictions.eq("cname", "小明")).list();
System.out.println(list);
*/
List<Customer> list = session.createCriteria(Customer.class)
.add(Restrictions.eq("cname", "小明"))
.add(Restrictions.eq("cid", 2)).list();
System.out.println(list);
Criteria criteria = session.createCriteria(Customer.class);
criteria.add(Restrictions.like("cname", "大%"));
List<Customer> list = criteria.list();
System.out.println(list);
List<Object[]> list = session.createSQLQuery("select * from
customer").list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}
List<Customer> list = session.createSQLQuery("select * from
customer").addEntity(Customer.class).list();
for (Customer customer : list) {
System.out.println(customer);
}
标签:tin creat 数据 别名 查询语句 des cts 其他 参数绑定
原文地址:https://www.cnblogs.com/haoworld/p/hibernate-jian-suo-fang-shi.html