JPQL全称Java Persistence Query Language

 

 

package com.ytkj.entity;

import javax.persistence.*;
import java.io.Serializable;

/**
 *          @Entity
 *         	作用:指定当前类是实体类。
 *         @Table
 *         	作用:指定实体类和表之间的对应关系。
 *         	属性:
 *         		name:指定数据库表的名称
 *         @Id
 *         	作用:指定当前字段是主键。
 *         @GeneratedValue
 *         	作用:指定主键的生成方式。。
 *         	属性:
 *         		strategy :指定主键生成策略。
 *         @Column
 *         	作用:指定实体类属性和数据库表之间的对应关系
 *         	属性:
 *         		name:指定数据库表的列名称。
 *         		unique:是否唯一
 *         		nullable:是否可以为空
 *         		inserttable:是否可以插入
 *         		updateable:是否可以更新
 *         		columnDefinition: 定义建表时创建此列的DDL
 *         		secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在从表的名字搭建开发环境[重点]
 *
 * 客户实体类
 *      配置映射关系
 *          实体类和表映射
 *          实体类属性和表字段映射
 */
@Entity
@Table(name = "cst_customer")
public class Customer implements Serializable {
    /**
     * 声明主键配置
     */
    @Id
    /**
     * 配置主键的生成策略
     */
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    /**
     * 指定实体类属性和数据库表之间的对应关系
     */
    @Column(name ="cust_id")
    private Long custId;//客户主键
    @Column(name = "cust_name")
    private String custName;//客户名称
    @Column(name ="cust_source" )
    private String custSource;//客户来源
    @Column(name = "cust_industry")
    private String custIndustry;//客户行业
    @Column(name ="cust_level")
    private String custLevel;//客户级别
    @Column(name ="cust_address")
    private String custAddress;//客户地址
    @Column(name = "cust_phone")
    private String custPhone;//客户电话

    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getCustSource() {
        return custSource;
    }

    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }

    public String getCustIndustry() {
        return custIndustry;
    }

    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }

    public String getCustLevel() {
        return custLevel;
    }

    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }

    public String getCustAddress() {
        return custAddress;
    }

    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }

    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "custId=" + custId +
                ", custName='" + custName + '\'' +
                ", custSource='" + custSource + '\'' +
                ", custIndustry='" + custIndustry + '\'' +
                ", custLevel='" + custLevel + '\'' +
                ", custAddress='" + custAddress + '\'' +
                ", custPhone='" + custPhone + '\'' +
                '}';
    }
}

  

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();
    }


}

 

  

 

posted on 2019-12-06 01:40  西门夜说  阅读(311)  评论(0编辑  收藏  举报