JPA单表操作
创建表
CREATE TABLE cst_customer (
cust_id bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
cust_name varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
cust_source varchar(32) DEFAULT NULL COMMENT '客户信息来源',
cust_industry varchar(32) DEFAULT NULL COMMENT '客户所属行业',
cust_level varchar(32) DEFAULT NULL COMMENT '客户级别',
cust_address varchar(128) DEFAULT NULL COMMENT '客户联系地址',
cust_phone varchar(64) DEFAULT NULL COMMENT '客户联系电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
准备数据
实体类
package pers.zhang.domain;
import javax.persistence.*;
/**
* 配置映射关系
* 1.实体类和表的映射关系
* @Entity:声明实体类
* @Table:配置实体类和表的映射关系
* name:配置数据库表的名称
* 2.实体类中属性和表中字段的映射关系
*
*
*/
@Entity
@Table(name = "cst_customer")
public class Customer {
/*
@Id:配置主键
@GeneratedValue:配置主键的生成策略
strrategy:
GenerationType.IDENTITY:自增、底层数据库必须支持自动增长方式(MySQL)
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_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 pers.zhang.utils;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JpaUtils {
private static EntityManagerFactory entityManagerFactory;
static{
//加载配置文件,根据配置文件创建entityManagerFactory
entityManagerFactory = Persistence.createEntityManagerFactory("myJpa");
}
//获取EneityManager
public static EntityManager getEntityManager(){
return entityManagerFactory.createEntityManager();
}
}
根据id查询
@Test
public void findByIdTest(){
EntityManager entityManager = JpaUtils.getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
/*
find:根据id查询(立即发送sql查询)
立即加载
class:查询苏剧的结构需要包装的实体类类型的字节码
id:主键
getReference:根据id查询(1.获取的对象是一个动态代理对象; 2.调用时不会立即发送sql语句查询数据库,当调用查询结果对象的时候才发送sql查询)
懒加载
class:查询苏剧的结构需要包装的实体类类型的字节码
id:主键
*/
Customer reference = entityManager.getReference(Customer.class, 1l);
//Customer customer = entityManager.find(Customer.class, 1l);
System.out.println(reference);
transaction.commit();
entityManager.close();
}
控制台打印:
Hibernate: select customer0_.cust_id as cust_id1_0_0_, customer0_.cust_address as cust_add2_0_0_, customer0_.cust_industry as cust_ind3_0_0_, customer0_.cust_level as cust_lev4_0_0_, customer0_.cust_name as cust_nam5_0_0_, customer0_.cust_phone as cust_pho6_0_0_, customer0_.cust_source as cust_sou7_0_0_ from cst_customer customer0_ where customer0_.cust_id=?
Customer{custId=1, custName='阿里巴巴', custSource='null', custIndustry='电商', custLevel='一', custAddress='杭州', custPhone='11111111'}
查询所有
@Test
public void selectAllTest(){
EntityManager entityManager = JpaUtils.getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//没有相同类名时,可以省略包名
String jpql = "from pers.zhang.domain.Customer";
Query query = entityManager.createQuery(jpql );
List resultList = query.getResultList();
for(Object o : resultList)
System.out.println(o);
transaction.commit();
entityManager.close();
}
控制台打印
Hibernate: select customer0_.cust_id as cust_id1_0_, customer0_.cust_address as cust_add2_0_, customer0_.cust_industry as cust_ind3_0_, customer0_.cust_level as cust_lev4_0_, customer0_.cust_name as cust_nam5_0_, customer0_.cust_phone as cust_pho6_0_, customer0_.cust_source as cust_sou7_0_ from cst_customer customer0_
Customer{custId=1, custName='阿里巴巴', custSource='修改后的', custIndustry='电商', custLevel='一', custAddress='杭州', custPhone='11111111'}
Customer{custId=2, custName='腾讯', custSource='null', custIndustry='互联网', custLevel='二', custAddress='深圳', custPhone='22222222'}
Customer{custId=3, custName='万达', custSource='null', custIndustry='房地产', custLevel='三', custAddress='大连', custPhone='33333333'}
Customer{custId=4, custName='恒大', custSource='null', custIndustry='矿泉水', custLevel='四', custAddress='广州', custPhone='44444444'}
Customer{custId=5, custName='网易', custSource='null', custIndustry='游戏', custLevel='五', custAddress='广州', custPhone='55555555'}
update
@Test
public void updateTest(){
EntityManager entityManager = JpaUtils.getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//获取要修改的对象
Customer reference = entityManager.getReference(Customer.class, 1l);
//修改
reference.setCustSource("修改后的");
//执行
entityManager.merge(reference);
transaction.commit();
entityManager.close();
}
控制台打印:
Hibernate: select customer0_.cust_id as cust_id1_0_0_, customer0_.cust_address as cust_add2_0_0_, customer0_.cust_industry as cust_ind3_0_0_, customer0_.cust_level as cust_lev4_0_0_, customer0_.cust_name as cust_nam5_0_0_, customer0_.cust_phone as cust_pho6_0_0_, customer0_.cust_source as cust_sou7_0_0_ from cst_customer customer0_ where customer0_.cust_id=?
Hibernate: update cst_customer set cust_address=?, cust_industry=?, cust_level=?, cust_name=?, cust_phone=?, cust_source=? where cust_id=?
添加
@Test
public void addTest(){
EntityManager entityManager = JpaUtils.getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
Customer customer = new Customer();
customer.setCustName("暴雪");
customer.setCustIndustry("CG制作");
//执行添加
entityManager.persist(customer);
transaction.commit();
entityManager.close();
}
控制台打印
Hibernate: insert into cst_customer (cust_address, cust_industry, cust_level, cust_name, cust_phone, cust_source) values (?, ?, ?, ?, ?, ?)
删除
@Test
public void deleteByIdTest(){
EntityManager entityManager = JpaUtils.getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
Customer reference = entityManager.getReference(Customer.class, 6l);
//执行删除
entityManager.remove(reference);
transaction.commit();
entityManager.close();
}
控制台打印:
Hibernate: select customer0_.cust_id as cust_id1_0_0_, customer0_.cust_address as cust_add2_0_0_, customer0_.cust_industry as cust_ind3_0_0_, customer0_.cust_level as cust_lev4_0_0_, customer0_.cust_name as cust_nam5_0_0_, customer0_.cust_phone as cust_pho6_0_0_, customer0_.cust_source as cust_sou7_0_0_ from cst_customer customer0_ where customer0_.cust_id=?
Hibernate: delete from cst_customer where cust_id=?