Hibernate Query Language
select ... from ... where ... group by ... having ... order by ...
session.createQuery()//创建Query实例
createQuery(hql)//方法2
query.list();//执行查询
//list()方法返回结果数据类型为java.util.List,List集合存放符合查询条件的持久化类对象。
private Session session = null;
@Before
public void setUp() throws Exception {
session = HibernateSessionFactory.getSession();
}
@After
public void tearDown() throws Exception {
session.close();
}
@Test
public void testSeller() {
// junit测试 1.导包然后buildpath 2.在项目根目录下新建source folder(test)
// 3.新建junit test case
// HQL语法,用session对象来得到query对象,然后query对象直接执行就可以了,就是这么简单
String hql = "from Seller as seller";// 设置别名,对查询完全不影响
// String hql = "from com.imooc.model.Seller";//这样也可以
Query query = session.createQuery(hql);
List sellers = query.list();
for (Seller seller : sellers) {
System.out.println(seller);
}
}
//测试关联表
@Test
public void test() {
String hql = "from Commodity";
Query query = session.createQuery(hql);
List commodities = query.list();
for (Commodity c : commodities) {
System.out.println("name:"+c.getName());
//可以直接查询其对应外键表的信息(先在数据库定义好外键,然后再反向生成持久化对象和映射文件)
System.out.println("seller's name:"+c.getSeller().getName());
}
}
select子句中未指定返回数据类型,默认为Object[]
// 查询商家的名称、电话、地址、星级 ***当只查询一个属性信息时,返回的是object而不是数组,用数组接收会报错
@Test
// 默认返回object[]
public void testSelectReturnObjectArray() {
String hql = "select s.name,s.tel,s.address,s.star from Seller s";
Query query = session.createQuery(hql);
List
// 返回的一条信息就是一个list集合,返回的是多条消息,所以用集合的集合接收
@Test
public void testSelectReturnList() {
String hql = "select new list(s.name as name,s.tel,s.address)from Seller s";
Query query = session.createQuery(hql);
List lists = query.list();
for (List list : lists) {
System.out.println("name:" + list.get(0));
System.out.println("tel:" + list.get(1));
System.out.println("address:" + list.get(2));
}
}
select子句中使用new map指定
key值为索引值,字符串类型
@Test//返回map ***Map中key值是string类型 可以通过别名来获取属性
public void testSelectReturnMap() {
String hql = "select new map(s.name as name,s.tel as tel,s.address)from Seller s";
Query query = session.createQuery(hql);
List
持久化类中定义对应的构造器
select子句中调用定义的构造器
//返回自定义类型,先在持久化类中创建相应的构造方法
@Test
public void testSelectReturnSelf(){
String hql = "select new Seller(s.name,s.tel,s.address) from Seller s";
Query query = session.createQuery(hql);
List sellers = query.list();
for (Seller seller : sellers) {
System.out.println("name:"+seller.getName());
System.out.println("tel:"+seller.getTel());
System.out.println("address:"+seller.getAddress());
}
}
去除查询结果中的重复元素
@Test
public void testDistinct(){
String hql ="select distinct c.sex from Customer c";
Query query= session.createQuery(hql);
List
where子句是一个逻辑表达式,设置查询条件,从而限制查询的返回结果
@Test
public void testWhere1(){
String hql = "from Commodity c where c.price>400";
Query query = session.createQuery(hql);
List commodities = query.list();
for (Commodity commodity : commodities) {
System.out.println("name:"+commodity.getName());
System.out.println("price:"+commodity.getPrice());
}
}
@Test
public void testWhereNull(){
String hql = "from Commodity c where c.description is null";
//可以的
//String hql = "from Commodity c where c.description = null";
Query query = session.createQuery(hql);
List commodities = query.list();
for (Commodity commodity : commodities) {
System.out.println("name:"+commodity.getName());
System.out.println("description:"+commodity.getDescription());
}
}
@Test
public void testWhereUnique(){
String hql = "from Customer c where c.name = '张三'";
Query query = session.createQuery(hql);
//如果结果不是一个单独的结果或者没有结果,就会报错
Customer c = (Customer) query.uniqueResult();
System.out.println(c.getName());
}
@Test
public void practice1(){
String hql = "select c.name,c.price,s.name,c.category" +
" from Commodity c,Seller s " +
"where c.seller=s.id and c.category like '%书%' and c.price>=40" +
"order by c.seller desc,c.name asc";
Query query = session.createQuery(hql);
List
@Test
public void practice2(){
String hql = "select new Map(o.customer.name,o.tradedate,o.status,o.amount) from Orderform o" +
" where o.tradedate between '2015-05-01' and '2015-06-01' and o.status in ('已发货' ,'已付款') and " +
"o.amount>1000 order by o.status asc,o.tradedate desc,o.amount desc";
Query query = session.createQuery(hql);
List