hibernate框架学习笔记10:HQL查询详解
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(); } }
补上内连接与外连接区别:
hibernate框架学习笔记10:HQL查询详解的更多相关文章
- hibernate框架学习笔记3:API详解
Configuration对象: package api; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configur ...
- hibernate框架学习笔记2:配置文件详解
实体类: package domain; public class Customer { private Long cust_id; private String cust_name; private ...
- spring框架学习笔记2:配置详解
简单写一个类做示例: package bean; public class User { private String name; private Integer age; public String ...
- struts2框架学习笔记2:配置详解
核心配置文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC ...
- mybatis学习笔记(10)-一对一查询
mybatis学习笔记(10)-一对一查询 标签: mybatis mybatis学习笔记10-一对一查询 resultType实现 resultMap实现 resultType和resultMap实 ...
- IP2——IP地址和子网划分学习笔记之《子网掩码详解》
2018-05-04 16:21:21 在学习掌握了前面的<进制计数><IP地址详解>这两部分知识后,要学习子网划分,首先就要必须知道子网掩码,只有掌握了子网掩码这部分内容 ...
- hibernate框架学习笔记7:HQL查询、Criteria查询简介
HQL查询:hibernate独有的查询语言 适用于不复杂的多表查询 示例: 实体类: package domain; public class Customer { private Long cus ...
- j2ee开发之hibernate框架学习笔记
hibernate框架技术重点学习笔记 1.针对不同的数据库,有不同的数据库实现类,使其符号对应的数据库? mysqlDaoImpl oracleDaoImpl ... ... 2.对象和表记录的转换 ...
- Hibernate框架学习笔记
Hibernate 是一个 JDO( Java Data Objects)工具.它的工作原理是通过文件把值对象(Java对象)和 数据库表之间建立起一个映射关系,还提供数据查询和获取数据的方法. ...
随机推荐
- asp.net URL重新实例
void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 RegisterRoutes(); } // 页面存放目录 ...
- emWin 2天速成实例教程000_如何快速入门ucGUI/emWin
备注:(1)打开工程目录下的"Exe\GUISimulationDebug.exe"即可看到效果.(2)看完教程000-005就基本会用emWin做项目,其他章节可以需要时再参考. ...
- 在visual studio 2017中配置Qt
简述 这两天因为软件工程课要用vs2017写一个C++的GUI界面,就打算学习Qt,但是vs2017配置起Qt来不像vs2013,15那么简单,而且现在网上对于vs2017配置Qt的教程很少,也不详细 ...
- 更改Qt Application为 Qt Console Application
工程属性 -> 链接器 -> 系统 -> 子系统 : 更改为 控制台 (/SUBSYSTEM:CONSOLE)
- 页面jquery调试的一个宝贵经验(类似于Eclipse中的写出一个对象点它的方法时候用alt加/可以跳出来它所有的方法)
案例讲解 一,html片段 <div class="page-upload"> <div class="tab-wrapper2"> & ...
- iOS开发--XMPPFramework--环境的配置(一)
一.XMPPFramework概述 XMPPFramework是一个OS X/iOS平台的开源项目,使用Objective-C实现了XMPP协议(RFC-3920),同时还提供了用于读写XML的工具, ...
- WEB前端中rem单位的应用(一)
在最近的开发和之前的的使用中,我们一般面对需要适应多端的项目或者需要移动端多设备的适应,都可能会引入rem,em这样的单位. 如果你要着手一个以上类似的项目,我们也同样建议使用rem或者干脆引入框架, ...
- 安装anaconda与tensorflow
在安装Anaconda之前,有的已经安装过一个Python版本了,但是又不想删除这个Python版本,该怎么办呢? 安装anaconda与tensorflow一条龙 jupyter notebook ...
- windows下 python3.5+tensorflow 安装
个人随笔,备忘参考 首先最近的tensorflow 对python3.5.x友好,我先装了Python3.6,查其他的一些博客说出现问题,后来重装3.5.0.下载用迅雷,超快. 安装比较简单,官网下载 ...
- Google Chrome Plus——绿色便携多功能谷歌浏览器
我更新浏览器的时候一般没有时间更新这个帖子,所以具体请看我网盘下载链接里面的更新日志,请自行查看最新版本下载,谢谢. 近期更新日期:2016.8.15(此时间可能不是最新,请看我网盘里面的更新日志) ...