码迷,mamicode.com
首页 > 其他好文 > 详细

jpql简单l查询

时间:2019-12-06 10:03:46      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:import   oid   junit   void   tran   public   释放   提交   gen   

JPQL全称Java Persistence Query Language

 

 

package com.ytkj.test;

import com.ytk.utils.JpaUtils;
import org.junit.Test;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.List;

/**
 * jpql测试
 */
public class JpqlTest {
    /**
     * 查询全部
     */
    @Test
    public void findAll(){
        EntityManager entityManager = JpaUtils.getEntityManager();
        //开启事务
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        //创建query对象 操作实体类而不是直接取操作数据库
        String jpql="from Customer";
        Query query = entityManager.createQuery(jpql);

        //查询
        List list = query.getResultList();
        for (Object o : list) {
            System.out.println(o);
        }

        //5.提交事务
        transaction.commit();
        //6.释放资源
        entityManager.close();
    }

    /**
     * 排序查询
     */
    @Test
    public void findAllOrderBy(){
        EntityManager entityManager = JpaUtils.getEntityManager();
        //开启事务
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        //创建query对象 操作实体类而不是直接取操作数据库
        String jpql="from Customer order by custId desc";
        Query query = entityManager.createQuery(jpql);

        //查询
        List list = query.getResultList();
        for (Object o : list) {
            System.out.println(o);
        }

        //5.提交事务
        transaction.commit();
        //6.释放资源
        entityManager.close();
    }

    /**
     * 查询总数
     */
    @Test
    public void getCount(){
        EntityManager entityManager = JpaUtils.getEntityManager();
        //开启事务
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        //创建query对象 操作实体类而不是直接取操作数据库
        String jpql="select count(custId) from Customer ";
        Query query = entityManager.createQuery(jpql);
        //查询
        Object result = query.getSingleResult();
        System.out.println(result);
        //5.提交事务
        transaction.commit();
        //6.释放资源
        entityManager.close();
    }

    /**
     * 分页查询
     */
    @Test
    public void getByPage(){
        EntityManager entityManager = JpaUtils.getEntityManager();
        //开启事务
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        //创建query对象 操作实体类而不是直接取操作数据库
        int pageNum=0;
        int pageSize=2;
        String jpql=" from Customer";
        Query query = entityManager.createQuery(jpql);
        //对分页参数赋值
        query.setFirstResult(pageNum);
        query.setMaxResults(pageSize);
        //查询
        List list = query.getResultList();
        for (Object o : list) {
            System.out.println(o);
        }
        //5.提交事务
        transaction.commit();
        //6.释放资源
        entityManager.close();
    }


    /**
     * 条件查询
     */
    @Test
    public void getByCondition(){
        EntityManager entityManager = JpaUtils.getEntityManager();
        //开启事务
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        //创建query对象 操作实体类而不是直接取操作数据库
        String jpql=" from Customer where custName like ?";
        Query query = entityManager.createQuery(jpql);
        //对占位符参数赋值
        query.setParameter(1,"zhe%");
        //查询
        List list = query.getResultList();
        for (Object o : list) {
            System.out.println(o);
        }
        //5.提交事务
        transaction.commit();
        //6.释放资源
        entityManager.close();
    }


}

 

  

 

jpql简单l查询

标签:import   oid   junit   void   tran   public   释放   提交   gen   

原文地址:https://www.cnblogs.com/yscec/p/11993006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!