1.导入JAR包
UTF-8
5.0.7.Final
junit
junit
4.12
test
org.hibernate
hibernate-entitymanager
${project.hibernate.version}
org.hibernate
hibernate-c3p0
${project.hibernate.version}
log4j
log4j
1.2.17
mysql
mysql-connector-java
5.1.6
2.配置JPA核心配置文件
org.hibernate.jpa.HibernatePersistenceProvider
3.创建实体类 并加注解
package com.itheima.domain;
import javax.persistence.*;
/**
* 客户的实体类
* 配置映射关系
*
*
* 1.实体类和表的映射关系
* @Entity:声明实体类
* @Table : 配置实体类和表的映射关系
* name : 配置数据库表的名称
* 2.实体类中属性和表中字段的映射关系
*
*
*/
@Entity
@Table(name = "cst_customer")
public class Customer {
/**
* @Id:声明主键的配置
* @GeneratedValue:配置主键的生成策略
* strategy
* GenerationType.IDENTITY :自增,mysql
* * 底层数据库必须支持自动增长(底层数据库支持的自动增长方式,对id自增)
* GenerationType.SEQUENCE : 序列,oracle
* * 底层数据库必须支持序列
* GenerationType.TABLE : jpa提供的一种机制,通过一张数据库表的形式帮助我们完成主键自增
* GenerationType.AUTO : 由程序自动的帮助我们选择主键生成策略
* @Column:配置属性和字段的映射关系
* name:数据库表中字段的名称
*/
@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_level")
private String custLevel;//客户级别
@Column(name="cust_industry")
private String custIndustry;//客户所属行业
@Column(name="cust_phone")
private String custPhone;//客户的联系方式
@Column(name="cust_address")
private String custAddress;//客户地址
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 getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
@Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custSource='" + custSource + '\'' +
", custLevel='" + custLevel + '\'' +
", custIndustry='" + custIndustry + '\'' +
", custPhone='" + custPhone + '\'' +
", custAddress='" + custAddress + '\'' +
'}';
}
}
4.编写工具类 ,方便后面测试
package com.itheima.utils;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
* 工具类得到
*/
public class JpaUtil {
private static EntityManagerFactory factory;
static {
factory = Persistence.createEntityManagerFactory("myJpa");
}
/**
* 获取实体管理器
* @return
*/
public static EntityManager getEntityManager(){
return factory.createEntityManager();
}
}
5.测试(增删改)
package com.itheima.test;
import com.itheima.domain.Customer;
import com.itheima.utils.JpaUtil;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaTest {
/**
* 测试jpa的保存
* 案例:保存一个客户到数据库中
* Jpa的操作步骤
* 1.加载配置文件创建工厂(实体管理器工厂)对象
* 2.通过实体管理器工厂获取实体管理器
* 3.获取事务对象,开启事务
* 4.完成增删改查操作
* 5.提交事务(回滚事务)
* 6.释放资源
*/
//增加
@Test
public void JpaSava(){
//1.加载配置文件创建工厂(实体管理器工厂)对象
// EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa");
//2.通过实体管理器工厂获取实体管理器
//EntityManager em = factory.createEntityManager();
EntityManager em = JpaUtil.getEntityManager();
//3.获取事务对象,开启事务
EntityTransaction transaction = em.getTransaction();
transaction.begin();
// 4.完成增删改查操作
Customer ct=new Customer();
ct.setCustName("哈登");
ct.setCustAddress("美国休斯敦-火箭队");
//保存操作
em.persist(ct);
//5.提交事务(回滚事务)
transaction.commit();
// 6.释放资源 从后到前
em.close();
}
/**
* 根据id查找
*/
@Test
public void Jpafind(){
//得到实体管理器
EntityManager entityManager = JpaUtil.getEntityManager();
//获取事务对象 开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//查找
Customer customer = entityManager.find(Customer.class, 2L);
System.out.println(customer);
entityManager.close();
}
/**
* 修改 先查再修改
*/
@Test
public void JpaUpdate(){
//得到实体管理器
EntityManager entityManager = JpaUtil.getEntityManager();
//获取事务对象 开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//查找
Customer customer = entityManager.find(Customer.class, 2L);
customer.setCustName("保罗");
entityManager.merge(customer);
System.out.println(customer);
entityManager.close();
}
/**
* 删除
*/
@Test
public void JpaDel(){
//得到实体管理器
EntityManager entityManager = JpaUtil.getEntityManager();
//获取事务对象 开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//查找
Customer customer = entityManager.find(Customer.class, 2L);
entityManager.remove(customer);
transaction.commit();
entityManager.close();
}
}
5.2 查询
package com.itheima.test;
import com.itheima.utils.JpaUtil;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.List;
public class JPQLTest {
/**
* 查询全部
*/
@Test
public void findAllTest(){
EntityManager entityManager = JpaUtil.getEntityManager();
//获取事务对象 ,开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
String pql="from Customer";
Query query = entityManager.createQuery(pql);
List resultList = query.getResultList();
for (Object o : resultList) {
System.out.println(o);
}
transaction.commit();
entityManager.close();
}
//分页查询
@Test
public void findPage(){
EntityManager entityManager = JpaUtil.getEntityManager();
//获取事务对象 ,开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
String pql="from Customer ";
Query query = entityManager.createQuery(pql);
query.setFirstResult(1);
query.setMaxResults(2);
List resultList = query.getResultList();
for (Object o : resultList) {
System.out.println(o);
}
transaction.commit();
entityManager.close();
}
//条件查询
@Test
public void findByTj(){
EntityManager entityManager = JpaUtil.getEntityManager();
//获取事务对象 ,开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
String pql="from Customer where custName like ? ";
Query query = entityManager.createQuery(pql);
query.setParameter(1,"%哈登%");
List resultList = query.getResultList();
for (Object o : resultList) {
System.out.println(o);
}
transaction.commit();
entityManager.close();
}
//统计查询
@Test
public void CountTest(){
EntityManager entityManager = JpaUtil.getEntityManager();
//获取事务对象 ,开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
String pql="select count (custName) from Customer where custName like ? ";
Query query = entityManager.createQuery(pql);
query.setParameter(1,"%哈登%");
List resultList = query.getResultList();
for (Object o : resultList) {
System.out.println(o);
}
transaction.commit();
entityManager.close();
}
//排序查询
// SELECT * FROM cst_customer WHERE cust_name LIKE "%哈登%" ORDER BY cust_id DESC;
@Test
public void ordesTest(){
EntityManager entityManager = JpaUtil.getEntityManager();
//获取事务对象 ,开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
String pql=" from Customer where custName like ? order by custId DESC";
Query query = entityManager.createQuery(pql);
query.setParameter(1,"%哈登%");
List resultList = query.getResultList();
for (Object o : resultList) {
System.out.println(o);
}
transaction.commit();
entityManager.close();
}
}