Criteria是面向对象的无语句查询

Demo.java

package com.legend.b_criteria;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.jupiter.api.Test; import com.legend.domain.Customer;
import com.legend.utils.HibernateUtils; /**
* 学习Criteria语法
* @author qichunlin
*
*/
public class Demo {
//基本语法
@Test
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//--------------
Criteria criteria = session.createCriteria(Customer.class); List<Customer> list = criteria.list();
System.out.println(list);
//-------------- tx.commit();
session.close(); } //条件查询
@Test
public void fun2(){
//
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//--------------
//创建Criteria 对象
Criteria criteria = session.createCriteria(Customer.class); //添加Criteria的查询参数
criteria.add(Restrictions.eq("cust_id", 1l)); //获取查询的结果
Customer c = (Customer) criteria.uniqueResult();
System.out.println(c);
//-------------- //事务的提交
tx.commit();
session.close(); } //分页查询
//(当前页数-1)*每页条数
@Test
public void fun3() {
//
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//--------------
//编写HQL语句
Criteria criteria = session.createCriteria(Customer.class); criteria.setFirstResult(1);
criteria.setMaxResults(2); List<Customer> list = criteria.list();
System.out.println(list);
//-------------- tx.commit();
session.close(); } //排序检索
@Test
public void fun5() {
//
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//--------------
Criteria criteria = session.createCriteria(Customer.class); criteria.addOrder(Order.desc("cust_id")); List<Customer> list = criteria.list();
System.out.println(list);
//-------------- tx.commit();
session.close(); } //统计查询
@Test
public void fun6() {
//
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//--------------
//编写HQL语句
Criteria criteria = session.createCriteria(Customer.class); //设置查询目标
criteria.setProjection(Projections.rowCount());
//List list = query.list();
List<Long> list = criteria.list();
System.out.println(list);
//-------------- tx.commit();
session.close(); }
}

基本查询

统计查询

附上hibernate的额核心配置文件xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- 配置参数文件 -->
<hibernate-configuration>
<session-factory>
<!--
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
配置mysql相关参数
-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///crm</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property> <!-- 配置mysql方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!--
sql语句显示
#hibernate.show_sql true
# format SQL in log and console
#hibernate.format_sql true -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">true</property> <!--
## auto schema export
#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update
#hibernate.hbm2ddl.auto validate
-->
<property name="hibernate.hbm2ddl.auto">update</property> <!--
设置数据库隔离级别
## specify a JDBC isolation level
#hibernate.connection.isolation 4
-->
<property name="hibernate.connection.isolation">4</property> <!-- 指定session与当前线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property> <!-- 配置映射文件加载 orm元数据 -->
<mapping resource="com/legend/domain/Customer.hbm.xml"/>
<mapping resource="com/legend/domain/LinkMan.hbm.xml" />
<mapping resource="com/legend/domain/User.hbm.xml" />
<mapping resource="com/legend/domain/Role.hbm.xml" /> </session-factory>
</hibernate-configuration>

传统的Criteria

离线的Criteria

2018.11.13 Hibernate 中数据库查询中的Criteria查询实例的更多相关文章

  1. 2018.11.1 Hibernate中的Mapper关系映射文件

    Customer.hbm.xml 基本的参数都在里面了 <?xml version="1.0" encoding="UTF-8"?> <!DO ...

  2. 2018.11.14 hibernate中的查询优化---关联级别查询

    查询优化------关联级别查询 集合策略 在Mapper映射文件中添加属性 测试数据 lazy:true 延时加载数据 fetch:select 单表查询 控制台显示输出 结论:单表查询,使用到在加 ...

  3. 2018.11.4 Hibernate中一对、多对多的关系

    简单总结一下 多表关系 一对多/多对一 O 对象 一的一方使用集合. 多的一方直接引用一的一方. R 关系型数据库 多的一方使用外键引用一的一方主键. M 映射文件 一: 多: 操作: 操作管理级别属 ...

  4. 2018.11.13 N4010A 通信设置

    设置电脑之IP地址及Subnet mask.      IP address: 192.168.1.2      Subnet mask: 255.255.255.0, 其它选项为默认. 然后点击OK ...

  5. hibernate里的generator中class =value介绍

    在*.hbm.xml必须声明的<generator>子元素是一个Java类的名字,用来为该持久化类的实例生成唯一的标识.<generator class="sequence ...

  6. Criteria 查询

    Criteria.Criterion接口和Expression类组成,他支持在运行时动态生成查询语句. Criteria查询是Hibernate提供的一种查询方式 Hibernate检索方式:  PO ...

  7. JPA criteria 查询:类型安全与面向对象

    参考:https://my.oschina.net/zhaoqian/blog/133500 一.JPA元模型概念,及使用 在JPA中,标准查询是以元模型的概念为基础的.元模型是为具体持久化单元的受管 ...

  8. NHibernate系列文章二十三:NHibernate查询之Criteria查询(附程序下载)

    摘要 上一篇文章介绍了NHibernate HQL,他的缺点是不能够在编译时发现问题.如果数据库表结构有改动引起了实体关系映射的类有改动,要同时修改这些HQL字符串.这篇文章介绍NHibernate面 ...

  9. Hibernate-ORM:15.Hibernate中的Criteria查询

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲师Hibernate中的Criteria查询! 一,Criteria简介: 刚接触Hibernate ...

随机推荐

  1. 2019.03.26 读书笔记 关于event

    event 主要是给委托加了一层保护,不能任意的 class.delegate=null,class.delegate=fun1,不能由调用者去任意支配,而是由class自己去增加或减少,用+=.-= ...

  2. python 爬虫系列03--职位爬虫

    职位爬虫 import requests from lxml import etree cookie = { 'Cookie':'user_trace_token=20181015184304-692 ...

  3. HexChat访问Docker频道

    1.使用HexChat登录Freenode.net 2.在Freenode下输入并回车: /msg NickServ REGISTER yourpassword youremail@example.c ...

  4. (转)expfilt 命令

    expfilt 命令 原文:https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72/com.ibm.aix.cmds2/expfilt.ht ...

  5. vue之理解异步更新 --- nextTick

    默认情况下,vue中DOM的更新是异步执行的,理解这一点非常重要. 当侦测到数据变化时,Vue会打开一个队列,然后把在同一个事件循环(event loop)当中观察到的数据变化的watcher推送进入 ...

  6. Ubuntu(Linux) 下 zip 命令使用详解

    1.功能作用:压缩文件或者目录 2.位置:/usr/bin/zip 3.格式用法:zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfi ...

  7. Springboot - 集成 JPA

    1.什么是 JPA? JPA就是Java Persistence API的意思,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. 2. JPA 具有什么优 ...

  8. vim创建新的命令

    转自:http://man.chinaunix.net/newsoft/vi/doc/usr_5F40.html#usr_40.txt *40.1* 键映射 简单的映射已经在 |05.3| 介绍过了. ...

  9. Linux VFS机制简析(二)

    Linux VFS机制简析(二) 接上一篇Linux VFS机制简析(一),本篇继续介绍有关Address space和address operations.file和file operations. ...

  10. asp ajax

    //[AjaxPro.AjaxMethod()] //public DataTable loadChecked() //{ // return BDAContext.GetObject<ICNP ...