HQL语句中不可以出现与表有关的内容,而是对象的属性
实体类(注意配置文件):
package domain; import java.util.HashSet;
import java.util.Set; //客户实体
public class Customer { private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
//使用set集合,表达一对多关系
private Set<LinkMan> linkMens = new HashSet<LinkMan>(); public Set<LinkMan> getLinkMens() {
return linkMens;
}
public void setLinkMens(Set<LinkMan> linkMens) {
this.linkMens = linkMens;
}
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
}
}
抽取工具类:
package utils; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtils {
private static SessionFactory sf; static{
//1 创建,调用空参构造
Configuration conf = new Configuration().configure();
//2 根据配置信息,创建 SessionFactory对象
sf = conf.buildSessionFactory();
} //获得session => 获得全新session
public static Session openSession(){
//3 获得session
Session session = sf.openSession(); return session; }
//获得session => 获得与线程绑定的session
public static Session getCurrentSession(){
//3 获得session
Session session = sf.getCurrentSession(); return session;
}
}
测试类:
基本语法:
package hql; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import domain.Customer;
import utils.HibernateUtils; //学习HQL语法
public class Demo { //基本语法
@Test
public void fun1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql = " from domain.Customer ";//完整写法
String hql2 = " from Customer "; //简单写法
String hql3 = " from java.lang.Object "; //查询所有Object对象,即所有对象 Query query = session.createQuery(hql); List list = query.list(); System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//排序
public void fun2(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql1 = " from domain.Customer order by cust_id asc ";//完整写法升序
String hql2 = " from domain.Customer order by cust_id desc ";//完整写法降序 Query query = session.createQuery(hql2); List list = query.list(); System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//条件查询
public void fun3(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql1 = " from domain.Customer where cust_id =? ";//?按所有传值
String hql2 = " from domain.Customer where cust_id = :id ";//:按名称传值 Query query = session.createQuery(hql2); //query.setParameter(0, 5l);
query.setParameter("id", 5l); List list = query.list(); System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//分页查询
public void fun4(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql1 = " from domain.Customer ";//完整写法 Query query = session.createQuery(hql1); //类似SQL中的limit ?,?
// (当前页数-1)*每页条数
query.setFirstResult(0);
query.setMaxResults(2); List list = query.list(); System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//统计查询
//count 计数
//sum 求和
//avg 平均数
//max
//min
public void fun5(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql1 = " select count(*) from domain.Customer ";
String hql2 = " select sum(cust_id) from domain.Customer ";
String hql3 = " select avg(cust_id) from domain.Customer ";
String hql4 = " select max(cust_id) from domain.Customer ";
String hql5 = " select min(cust_id) from domain.Customer "; Query query = session.createQuery(hql3); Number number = (Number) query.uniqueResult(); System.out.println(number);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//投影查询
//查询对象某一个属性
public void fun6(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql1 = " select cust_name from domain.Customer ";
String hql2 = " select cust_name,cust_id from domain.Customer ";
String hql3 = " select new Customer(cust_id,cust_name) from domain.Customer ";
//如果实体类有相应构造方法,也可以执行 Query query = session.createQuery(hql2); List list = query.list(); System.out.println(list); //----------------------------------------------------
tx.commit();
session.close(); }
}
多表查询:
再创建一个实体类:
package domain; //联系人实体
public class LinkMan {
private Long lkm_id;
private Character lkm_gender;
private String lkm_name;
private String lkm_phone;
private String lkm_email;
private String lkm_qq;
private String lkm_mobile;
private String lkm_memo;
private String lkm_position; // 表达多对一关系
private Customer customer; public Customer getCustomer() {
return customer;
} public void setCustomer(Customer customer) {
this.customer = customer;
} public Long getLkm_id() {
return lkm_id;
} public void setLkm_id(Long lkm_id) {
this.lkm_id = lkm_id;
} public Character getLkm_gender() {
return lkm_gender;
} public void setLkm_gender(Character lkm_gender) {
this.lkm_gender = lkm_gender;
} public String getLkm_name() {
return lkm_name;
} public void setLkm_name(String lkm_name) {
this.lkm_name = lkm_name;
} public String getLkm_phone() {
return lkm_phone;
} public void setLkm_phone(String lkm_phone) {
this.lkm_phone = lkm_phone;
} public String getLkm_email() {
return lkm_email;
} public void setLkm_email(String lkm_email) {
this.lkm_email = lkm_email;
} public String getLkm_qq() {
return lkm_qq;
} public void setLkm_qq(String lkm_qq) {
this.lkm_qq = lkm_qq;
} public String getLkm_mobile() {
return lkm_mobile;
} public void setLkm_mobile(String lkm_mobile) {
this.lkm_mobile = lkm_mobile;
} public String getLkm_memo() {
return lkm_memo;
} public void setLkm_memo(String lkm_memo) {
this.lkm_memo = lkm_memo;
} public String getLkm_position() {
return lkm_position;
} public void setLkm_position(String lkm_position) {
this.lkm_position = lkm_position;
} }
测试类:
package hql; import java.util.Arrays;
import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import domain.Customer;
import utils.HibernateUtils; //学习HQL语法(不常用) - 多表查询语法
public class Demo2 {
//原生SQL中:
// 交叉连接-笛卡尔积(避免)
// select * from A,B
// 内连接
// |-隐式内连接
// select * from A,B where b.aid = a.id
// |-显式内连接
// select * from A inner join B on b.aid = a.id
// 外连接
// |- 左外
// select * from A left [outer] join B on b.aid = a.id
// |- 右外
// select * from A right [outer] join B on b.aid = a.id
//---------------------------------------------------------------------
//HQL的多表查询
//内连接(迫切)
//外连接
// |-左外(迫切)
// |-右外(迫切) @Test
//HQL 内连接 => 将连接的两端对象分别返回.放到数组中.
public void fun1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql = " from Customer c inner join c.linkMens ";
//c相当于别名
Query query = session.createQuery(hql); List<Object[]> list = query.list(); for(Object[] arr : list){
System.out.println(Arrays.toString(arr));
}
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//HQL 迫切内连接 => 帮我们进行封装.返回值就是一个对象
public void fun2(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql = " from Customer c inner join fetch c.linkMens "; Query query = session.createQuery(hql); List<Customer> list = query.list(); System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//HQL 左外连接 => 将连接的两端对象分别返回.放到数组中.
public void fun3(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql = " from Customer c left join c.linkMens "; Query query = session.createQuery(hql); List<Object[]> list = query.list(); for(Object[] arr : list){
System.out.println(Arrays.toString(arr));
}
//----------------------------------------------------
tx.commit();
session.close(); }
@Test
//HQL 右外连接 => 将连接的两端对象分别返回.放到数组中.
public void fun4(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql = " from Customer c right join c.linkMens "; Query query = session.createQuery(hql); List<Object[]> list = query.list(); for(Object[] arr : list){
System.out.println(Arrays.toString(arr));
}
//----------------------------------------------------
tx.commit();
session.close(); } }
补上内连接与外连接区别: