HQL(Hibernate Query Language)
面向对象的查询语言,与sql不同,hql中的对象名是区分大小写的(除了java和属性其它部分不区分大小写);hql中查的是对象而不是表,并且支持多态;hql主要通过Query来操作。
使用步骤:
1获取session对象
2.编写HQL语句
3.创建Query对象
4.执行查询,得到查询结果
package cn.text.java.cn.happy.test;
import cn.happy.entity.Dept;
import cn.happy.entity.Dog;
import cn.hql.entity.demp;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import java.util.List;
/**
* Created by Happy on 2017-09-18.
* 勿忘国耻 为中华之崛起而读书
*/
public class Test20170920 {
@Test
public void testdel(){
//Configuration
Configuration cfg=new Configuration().configure();//1
//session对象
SessionFactory factory=cfg.buildSessionFactory(); //2
Session session = factory.openSession(); //3
Transaction tx = session.beginTransaction();//4
Dog dog=new Dog();
dog.setDogid(2);
session.delete(dog);
tx.commit();//5
System.out.println("deleate ok!");
session.close();
}
@Test
public void loadtext() {
Configuration cfg = new Configuration().configure();//1
//session对象
SessionFactory factory = cfg.buildSessionFactory(); //2
Session session = factory.openSession(); //3
Transaction tx = session.beginTransaction();//4
String hql = "from demp";
Query query = session.createQuery(hql);
List<demp> list = query.list();
for (demp depts : list) {
System.out.println(depts.getDempname());
}
}
//02.案例:我想检索 名称为"开发部" 部门集合
@Test
public void selectSomeRecordsFromTable(){
//你找方案
Configuration cfg = new Configuration().configure();//1
//session对象
SessionFactory factory = cfg.buildSessionFactory(); //2
Session session = factory.openSession(); //3
Transaction tx = session.beginTransaction();//4
String hql="from demp demp where demp.dempname='财务部'";
Query query = session.createQuery(hql);
List<demp> list = query.list();
for (demp dept:list){
System.out.println(dept.getDempname());
}
}
//02.案例:参数查询 :方案一:?匿名占位符
@Test
public void selectByConditionNiming(){
Configuration cfg = new Configuration().configure();//1
//session对象
SessionFactory factory = cfg.buildSessionFactory(); //2
Session session = factory.openSession(); //3
Transaction tx = session.beginTransaction();//4
String hql="from demp where dempname=?";//为?号赋值
Query query = session.createQuery(hql); //q.setString(0,name);//下标从0开始,对应设置“?”的参数
query.setParameter(0,"45");
List<demp> list = query.list();
for (demp dept1:list){
System.out.println(dept1.getDempname());
}
}
//02.案例:参数查询 :方案二::name 参数名称绑定
// 使用“:起个名字” 表示未知量,然后是对这个“起个名字赋值”
@Test
public void selectByConditionParametername(){
Configuration cfg = new Configuration().configure();//1
//session对象
SessionFactory factory = cfg.buildSessionFactory(); //2
Session session = factory.openSession(); //3
Transaction tx = session.beginTransaction();//4
String hql="from demp where dempname=:dempname";
Query query = session.createQuery(hql);
query.setParameter("dempname","2");
List<demp> list = query.list();
for (demp depts:list){
System.out.println(depts.getDempname());
}
}
//02.案例:参数查询 :方案三::name 参数名称绑定++++对象属性
@Test
public void selectByConditionParameternameAndObjectAttribute(){
Configuration cfg = new Configuration().configure();//1
//session对象
SessionFactory factory = cfg.buildSessionFactory(); //2
Session session = factory.openSession(); //3
Transaction tx = session.beginTransaction();//4
String hql="from demp where dempname=:dempname";
demp dd=new demp();
dd.setDempname("开发部");
Query query = session.createQuery(hql);
query.setProperties(dd);
List<demp> list = query.list();
for (demp dept2:list){
System.out.println(dept2.getDempname());
}
}
}