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>