标签:java dao层 test 对象 slist pen 离线 customer package
package com.itheima.qbc;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.utils.HibernateUtils;
public class QBCTest {
/*
* 查询全部字段
* 查询部分字段
* 去重查询
* 聚合查询
* 条件查询
* 排序查询
* 分组查询
* 分页查询
* 离线查询(重点)---DetachedCriteria
*
条件查询
criteria.add(Restrictions.gt/eq/lt/le/ge/like(propertyName,value));
投影查询
criteria.setProject(Projections.property(propertyName));
什么是离线查询?
需求:业务:页面筛选条件查询操作
--->页面提交多个条件
--->在Action中获得提交数据 将数据封装一个条件实体Condition
--->将condition传递给service层
--->将condition传递给dao层
--->通过session获得criteria对象
--->criteria设置条件(将condition对象拆开)
--->想要数据 criteria.list()
是否可以在Action中不要Condition 在Action中直接将条件封装到criteria中
将criteria传递service 传递dao 在dao执行查询操作 将condition对象省略
criteria获取方式:session.createCriteria(); session是属于dao层
DetachedCriteria离线的 获取可以不通过session
DetachedCriteria封装条件
*/
@Test
//* sql方式
public void test10(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
SQLQuery sqlQuery = session.createSQLQuery("select * from cst_customer");//sql语句
List<Object[]> list = sqlQuery.list();
for(Object[] objs:list){
System.out.println(Arrays.asList(objs));
}
transaction.commit();
session.close();
}
@Test
//* 离线查询(重点)---DetachedCriteria
public void test9(){
//模拟web层
String pname = "小米";
double minPrice = 20D;
double maxPrice = 5000D;
//直接封装DetachedCriteria
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
detachedCriteria.add(Restrictions.like("xxx", "%"+pname+"%"));
//...........
//模拟service
//将detachedCriteria传递给service
//模拟dao层
Session session = HibernateUtils.openSession();
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
List<Customer> list = criteria.list();
}
@Test
//* 分页查询
public void test8(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
//完全面向对象的
Criteria criteria = session.createCriteria(Customer.class);//from Customer
criteria.setFirstResult(3);
criteria.setMaxResults(3);
List<Customer> list = criteria.list();
for(Customer customer:list){
System.out.println(customer);
}
transaction.commit();
session.close();
}
@Test
//* 分组查询 默认将分组的字段作为投影字段 进行查询
public void test7(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
//完全面向对象的
Criteria criteria = session.createCriteria(Customer.class);//from Customer
//criteria.setProjection(Projections.groupProperty("cust_name"));//select cust_name from cst_customer group by cust_name
//select cust_name,sum(cust_id) from cst_customer group by cust_name
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("cust_name"));
projectionList.add(Projections.sum("cust_id"));
criteria.setProjection(projectionList);
List<Object[]> list = criteria.list();
for(Object[] obj:list){
System.out.println(Arrays.asList(obj));
}
transaction.commit();
session.close();
}
@Test
//* 排序查询
public void test6(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
//完全面向对象的
Criteria criteria = session.createCriteria(Customer.class);//from Customer
criteria.addOrder(Order.desc("cust_id")); // order by cust_id desc
List<Customer> list = criteria.list();
for(Customer customer:list){
System.out.println(customer);
}
transaction.commit();
session.close();
}
@Test
//* 条件查询
public void test5(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
//完全面向对象的
Criteria criteria = session.createCriteria(Customer.class);
criteria.add(Restrictions.gt("cust_id", 2L));
criteria.add(Restrictions.like("cust_name", "%百%"));//and --- select * from cst_customer where cust_id>2 and cust_name like %百%
//select * from cst_customer where cust_id>? or cust_id<? and cust_name like ?
//select * from cst_customer where (cust_id>?) or (cust_id<? and cust_name like ?)
/*criteria.add(Restrictions.or(
Restrictions.gt("cust_id", 2L),
Restrictions.and(
Restrictions.lt("cust_id", 8L),
Restrictions.like("cust_name", "%学谷%")
)
));
*/
List<Customer> list = criteria.list();
for(Customer customer:list){
System.out.println(customer);
}
transaction.commit();
session.close();
}
@Test
//* 聚合查询
public void test4(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
//完全面向对象的
Criteria criteria = session.createCriteria(Customer.class);
//criteria.setProjection(Projections.count("cust_id"));
criteria.setProjection(Projections.rowCount()); //select count(*) from cst_customer;
//criteria.setProjection(Projections.sum("cust_id"));
Object uniqueResult = criteria.uniqueResult();
System.out.println(uniqueResult);
transaction.commit();
session.close();
}
@Test
//* 去重查询
public void test3(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
//完全面向对象的
Criteria criteria = session.createCriteria(Customer.class);
criteria.setProjection(Projections.distinct(Projections.property("cust_name")));
List<Object> list = criteria.list();
for(Object obj:list){
System.out.println(Arrays.asList(obj));
}
transaction.commit();
session.close();
}
@Test
//* 查询部分字段 投影
public void test2(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
//完全面向对象的
Criteria criteria = session.createCriteria(Customer.class);
//设置要投影的字段
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("cust_id"));
projectionList.add(Projections.property("cust_name"));
criteria.setProjection(projectionList); //select cust_id,cust_name from cst_customer;
List<Object[]> list = criteria.list();
for(Object[] objs:list){
System.out.println(Arrays.asList(objs));
}
transaction.commit();
session.close();
}
@Test
//* 查询全部字段
public void test1(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
//完全面向对象的
Criteria criteria = session.createCriteria(Customer.class);//from Customer --- sql:select * from cst_customer
List<Customer> list = criteria.list();
for(Customer customer:list){
System.out.println(customer);
}
transaction.commit();
session.close();
}
}
标签:java dao层 test 对象 slist pen 离线 customer package
原文地址:http://www.cnblogs.com/zjqblog/p/7392196.html