JPQL全称Java Persistence Query Language
基于首次在EJB2.0中引入的EJB查询语言(EJB QL),Java持久化查询语言(JPQL)是一种可移植的查询语言,旨在以面向对象表达式语言的表达式,将SQL语法和简单查询语义绑定在一起·使用这种语言编写的查询是可移植的,可以被编译成所有主流数据库服务器上的SQL。
其特征与原生SQL语句类似,并且完全面向对象,通过类名和属性访问,而不是表名和表的属性。
Hibernate框架查询语言: HQL
JPA查询语言: JPQL
/**
* @author bruceliu
* @create 2019-07-30 14:10
* @description JPA的工具类(事务处理没有封装好)
*/
public class JPAUtil {
private static EntityManagerFactory factory=null;
static{
factory = Persistence.createEntityManagerFactory("myJpa");
}
/**
* 获取EntityManager
* @return
*/
public static EntityManager getEntityManager(){
return factory.createEntityManager();
}
/**
* 关闭资源
* @param factory
* @param em
*/
public static void clodeResources(EntityManagerFactory factory,EntityManager em){
em.close();
factory.close();
}
}
###2.查询全部
//查询所有客户
@Test
public void findAll() {
EntityManager em = null;
EntityTransaction tx = null;
try {
//获取实体管理对象
em = JPAUtil.getEntityManager();
//获取事务对象
tx = em.getTransaction();
tx.begin();
// 创建query对象
String jpql = "from Customer";
Query query = em.createQuery(jpql);
// 查询并得到返回结果
List list = query.getResultList(); // 得到集合返回类型
for (Object object : list) {
System.out.println(object);
}
tx.commit();
} catch (Exception e) {
// 回滚事务
tx.rollback();
e.printStackTrace();
} finally {
// 释放资源
em.close();
}
}
/**
* 测试分页查询
*/
@Test
public void testPage(){
EntityManager em = JPAUtil.getEntityManager();
Query query = em.createQuery("select count(id) from Customer");
Integer totalCount = Integer.parseInt(query.getSingleResult().toString()); //查询一个结果
System.out.println("数据库的总数是:"+totalCount);
int pageIndex=4;
int pageSize=3;
int pageCount=totalCount%pageSize==0?(totalCount/pageSize):(totalCount/pageSize+1);
Query query1 = em.createQuery("from Customer ");
query1.setFirstResult((pageIndex-1)*pageSize);//设置开始查询的位置
query1.setMaxResults(pageSize); //设置每页查询的大小
System.out.println("当前页码:"+pageIndex);
System.out.println("每页大小:"+pageSize);
System.out.println("总页数:"+pageCount);
System.out.println("总条数:"+totalCount);
List<Customer> list = query1.getResultList();
for (Customer customer : list) {
System.out.println(customer);
}
}
/**
* 条件查询
* 假设需要查询 年龄大于等于18 并小于等于40
*/
@Test
public void testQuery1(){
EntityManager em = JPAUtil.getEntityManager();
Query query = em.createQuery("from Customer where age>=? and age<=?");//JPSQL语句 此处非SQL语句!
query.setParameter(1,18);
query.setParameter(2,40);
List<Customer> list = query.getResultList();
for (Customer customer : list) {
System.out.println(customer);
}
JPAUtil.clodeResources(em);
}
/**
* 条件查询
* 假设需要查询 年龄大于等于18 并小于等于40
* 按照下标占位
*/
@Test
public void testQuery2(){
EntityManager em = JPAUtil.getEntityManager();
Query query = em.createQuery("from Customer where age>=?1 and age<=?2");//JPSQL语句 此处非SQL语句!
query.setParameter(1,18);
query.setParameter(2,40);
List<Customer> list = query.getResultList();
for (Customer customer : list) {
System.out.println(customer);
}
JPAUtil.clodeResources(em);
}
/**
* 条件查询
* 假设需要查询 年龄大于等于18 并小于等于40
* 按照位置占位符
*/
@Test
public void testQuery3(){
EntityManager em = JPAUtil.getEntityManager();
Query query = em.createQuery("from Customer where age>=:XXX and age<=:OOO");//JPSQL语句 此处非SQL语句!
query.setParameter("OOO",40);
query.setParameter("XXX",18);
List<Customer> list = query.getResultList();
for (Customer customer : list) {
System.out.println(customer);
}
JPAUtil.clodeResources(em);
}
/**
* 模糊查询
*/
@Test
public void testQuery4(){
EntityManager em = JPAUtil.getEntityManager();
Query query = em.createQuery("from Customer where lastName like ?1");//JPSQL语句 此处非SQL语句!
query.setParameter(1,"%小%");
List<Customer> list = query.getResultList();
for (Customer customer : list) {
System.out.println(customer);
}
JPAUtil.clodeResources(em);
}
//根据客户id倒序查询所有客户
//查询所有客户
@Test
public void testOrder() {
EntityManager em = null;
EntityTransaction tx = null;
try {
//获取实体管理对象
em = JPAUtil.getEntityManager();
//获取事务对象
tx = em.getTransaction();
tx.begin();
// 创建query对象
String jpql = "from Customer order by id desc,age asc";
Query query = em.createQuery(jpql);
// 查询并得到返回结果
List list = query.getResultList(); // 得到集合返回类型
for (Object object : list) {
System.out.println(object);
}
tx.commit();
} catch (Exception e) {
// 回滚事务
tx.rollback();
e.printStackTrace();
} finally {
// 释放资源
em.close();
}
}
/**
* 查询年龄最大的前三个人
*/
@Test
public void testMax(){
EntityManager em = JPAUtil.getEntityManager();
Query query = em.createQuery("from Customer order by age desc");//JPSQL语句 此处非SQL语句!
query.setMaxResults(3); //查询3条
List<Customer> list = query.getResultList();
for (Customer customer : list) {
System.out.println(customer);
}
JPAUtil.clodeResources(em);
}
//统计查询
@Test
public void findCount() {
EntityManager em = null;
EntityTransaction tx = null;
try {
//获取实体管理对象
em = JPAUtil.getEntityManager();
//获取事务对象
tx = em.getTransaction();
tx.begin();
// 查询全部客户
// 1.创建query对象
String jpql = "select count(custId) from Customer";
Query query = em.createQuery(jpql);
// 2.查询并得到返回结果
Object count = query.getSingleResult(); // 得到集合返回类型
System.out.println(count);
tx.commit();
} catch (Exception e) {
// 回滚事务
tx.rollback();
e.printStackTrace();
} finally {
// 释放资源
em.close();
}
}
/**
* 查询表中年龄最大值和最小值
*/
@Test
public void testQueryByAge(){
EntityManager em = JPAUtil.getEntityManager();
Query query = em.createQuery("select max(age),min(age) from Customer");
Object[] obj = (Object[]) query.getSingleResult();//结果一条
System.out.println("最大年龄:"+obj[0]);
System.out.println("最小年龄:"+obj[1]);
}
/**
* 查询所有人的姓名和年龄
*/
@Test
public void testQueryName(){
EntityManager em = JPAUtil.getEntityManager();
Query query = em.createQuery("select lastName,age from Customer");
List<Object[]> list = query.getResultList();
for (Object[] obj : list) {
System.out.println(obj[0]+"----"+obj[1]);
}
}