Hibernate的两种检索方式(HQL检索、QBC检索)

Hibernate框架

  javaEE开发三层结构 (高内聚、低耦合)
  MVC: jsp+Servlet+javaBean
  M:model 模型(javaBean:封装数据)
  V:view 视图(jsp:展示数据)
  C:controller 控制器(Servlet:1、获得请求参数 2、调用业务 3、分发转向)
Hibernate的概述
   Hibernate就是一个持久层的ORM的框架.
   ORM:Object Relational Mapping.对象关系映射. 
下载Hibernate5
   下载网址:http://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/
 开发包目录,如下图所示:
Hibernate的两种检索方式(HQL检索、QBC检索)_第1张图片

1.创建表
Create database hibernate;
Use hibernate;
CREATE TABLE `cst_customer` (
  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
  `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
  `cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id',
  `cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人id',
  `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
  `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
  `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
  `cust_linkman` varchar(64) DEFAULT NULL COMMENT '联系人',
  `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
  `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
  PRIMARY KEY (`cust_id`)
);
2.创建项目引入jar包

Hibernate的两种检索方式(HQL检索、QBC检索)_第2张图片


3.创建实体

public class Customer {
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
...
}

4.编写映射配置文件(类名.hbm.xml)






















5.创建Hibernate的核心配置文件(hibernate.cfg.xml)



"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">





com.mysql.jdbc.Driver
jdbc:mysql:///hibernate_day01
root
123

org.hibernate.dialect.MySQLDialect












6.HibernateUtils工具类


public class HibernateUtils {


private static SessionFactory sessionFactory;

static{
//创建Configuration对象,用于加载主配置文件(hibernate.cfg.xml)
Configuration cfg = new Configuration().configure();
//构建一个sessionFactory工厂类 (相当于一个连接池)
sessionFactory = cfg.buildSessionFactory();
}

//得到一个连接
public static Session openSession(){
return sessionFactory.openSession();
}

//从线程局部变量中获得session
public static Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}

/*public static void main(String[] args) {

}*/
}


Hibernate的检索方式


1.HQL检索: HQL,Hibernate Query Language(Hibernate查询语言,语法与SQL类似)
2.QBC检索: QBC,Query By Criteria(条件查询,更加面向对象的查询方式)


HQL检索--基本检索


@Test
/**
* HQL:基本检索
*/
public void demo2(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

// 基本查询
/*Query query = session.createQuery("from Customer");
List list = query.list();*/
// 起别名
//Query query = session.createQuery("from Customer c");
Query query = session.createQuery("select c from Customer c");
List list = query.list();

for (Customer customer : list) {
System.out.println(customer);
}

tx.commit();
}
HQL检索--排序检索


@Test
/**
* HQL:排序检索  -- 根据id降序
*/
public void demo3(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

// 基本查询
Query query = session.createQuery("from Customer order by cust_id desc");
List list = query.list();

for (Customer customer : list) {
System.out.println(customer);
}

tx.commit();
}


HQL检索--条件检索
@Test
/**
* HQL:条件查询
*/
public void demo4(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

// 按位置绑定参数
/*Query query = session.createQuery("from Customer where cust_name = ?");
// query.setString(0, "小军军");
query.setParameter(0, "小军军");
List list = query.list();*/

// 按名称绑定参数
Query query = session.createQuery("from Customer where cust_name = :name");
query.setParameter("name", "小添添");
/*List list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}*/
Customer customer = (Customer) query.uniqueResult();
System.out.println(customer);

tx.commit();
}


HQL检索--分页检索


@Test
/**
*  分页查询 联系人信息
*/
public void demo5(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

Query query = session.createQuery("from LinkMan order by lkm_id desc");
query.setFirstResult(5);
query.setMaxResults(5);
List list = query.list();
for (LinkMan linkMan : list) {
System.out.println(linkMan);
}

tx.commit();
}


HQL检索--统计检索


@Test
/**
*  统计查询
*/
public void demo6(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

Query query = session.createQuery("select count(*) from Customer");
Long num = (Long) query.uniqueResult();
System.out.println(num);

tx.commit();
}


HQL检索--投影检索


@Test
/**
* 投影查询
*/
public void demo7(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// 投影查询一列
/*List list = session.createQuery("select cust_name from Customer").list();
for (String string : list) {
System.out.println(string);
}*/
// 投影查询多列:
/*List list = session.createQuery("select cust_id,cust_name from Customer").list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}*/

// 投影的构造的方式查询:
List list = session.createQuery("select new Customer(cust_id,cust_name) from Customer").list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}


QBC检索--基本检索


@Test
/**
* 简单的查询
*/
public void demo1(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// 简单查询:
Criteria criteria = session.createCriteria(Customer.class);
List list = criteria.list();

for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}


QBC检索--条件检索


@Test
/**
* 简单的查询
*/
public void demo2(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// 条件查询:
Criteria criteria = session.createCriteria(Customer.class);
// 设置条件:
// criteria.add(Restrictions.eq("cust_name", "小军军"));
criteria.add(Restrictions.like("cust_name", "%小%"));
criteria.add(Restrictions.gt("cust_id", 1l));

List list = criteria.list();

for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}


QBC检索--分页检索


@Test
/**
* 分页查询
*/
public void demo3(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// 条件查询:
Criteria criteria = session.createCriteria(LinkMan.class);
// 设置分页查询:
criteria.setFirstResult(5);
criteria.setMaxResults(5);

List list = criteria.list();

for (LinkMan linkMan : list) {
System.out.println(linkMan);
}
tx.commit();
}


QBC检索--排序检索


@Test
/**
* 排序查询
*/
public void demo4(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

// 排序查询:
Criteria criteria = session.createCriteria(LinkMan.class);
criteria.addOrder(Order.desc("lkm_id"));

List list = criteria.list();

for (LinkMan linkMan : list) {
System.out.println(linkMan);
}

tx.commit();
}


QBC检索--统计检索


public void demo5(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

Criteria criteria = session.createCriteria(LinkMan.class);
criteria.setProjection(Projections.rowCount());
Long count = (Long) criteria.uniqueResult();

System.out.println(count);
tx.commit();
}
QBC检索--离线条件检索


@Test
/**
* 离线条件查询:DetachedCriteria(SSH整合经常使用.)
* * 可以脱离session设置参数.
*/
public void demo6(){
// 获得一个离线条件查询的对象
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);


detachedCriteria.add(Restrictions.eq("cust_name","小童童"));

Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

List list = detachedCriteria.getExecutableCriteria(session).list();// 离线条件查询对象与session绑定.
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}

你可能感兴趣的:(Hibernate的检索方式)