1.环境搭建,注意包结构的问题,src下建立名为META-INF的文件夹,放persistence.xml,位置放错,读不到会报错.

  1     <?xml version="1.0" encoding="UTF-8"?>
2 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
5 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
6 version="2.0">
7
8 <!--配置持久化单元 -->
9 <persistence-unit name="unit" transaction-type="RESOURCE_LOCAL">
10 <!--配置jpa持久化类提供者 -->
11 <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
12 <!--配置数据库 Hibernate基本信息 -->
13 <properties>
14 <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
15 <property name="hibernate.connection.url" value="jdbc:mysql:///day581"/>
16 <property name="hibernate.connection.username" value="root"/>
17 <property name="hibernate.connection.password" value="root"/>
18 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
19 <property name="hibernate.hbm2ddl.auto" value="update"/>
20 <property name="hibernate.show_sql" value="true"/>
21 <property name="hibernate.format_sql" value="true"/>
22 <property name="hibernate.connection.isolation" value="4"/>
23 <!-- c3p0连接池 -->
24 <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
25 </properties>
26 </persistence-unit>
27 </persistence>

2.实体类编写

  1 package huguangqin.com.cnblogs.entity;
2 import java.io.Serializable;
3 import javax.persistence.Column;
4 import javax.persistence.Entity;
5 import javax.persistence.GeneratedValue;
6 import javax.persistence.GenerationType;
7 import javax.persistence.Id;
8 import javax.persistence.Table;
9
10 @Entity
11 @Table(name = "cst_customer")
12 public class Customer implements Serializable {
13 private static final long serialVersionUID = 1L;
14
15 @Id
16 @GeneratedValue(strategy = GenerationType.IDENTITY)
17 @Column(name = "cust_id")
18 private Long custId;
19
20 @Column(name = "cust_name")
21 private String custName;
22
23 @Column(name = "cust_source")
24 private String custSource;
25
26 @Column(name = "cust_industry")
27 private String custIndustry;
28
29 @Column(name = "cust_level")
30 private String custLevel;
31
32 @Column(name = "cust_address")
33 private String custAddress;
34
35 @Column(name = "cust_phone")
36 private String custPhone;
37
38 public Long getCustId() {
39 return custId;
40 }
41
42 public void setCustId(Long custId) {
43 this.custId = custId;
44 }
45
46 public String getCustName() {
47 return custName;
48 }
49
50 public void setCustName(String custName) {
51 this.custName = custName;
52 }
53
54 public String getCustSource() {
55 return custSource;
56 }
57
58 public void setCustSource(String custSource) {
59 this.custSource = custSource;
60 }
61
62 public String getCustIndustry() {
63 return custIndustry;
64 }
65
66 public void setCustIndustry(String custIndustry) {
67 this.custIndustry = custIndustry;
68 }
69
70 public String getCustLevel() {
71 return custLevel;
72 }
73
74 public void setCustLevel(String custLevel) {
75 this.custLevel = custLevel;
76 }
77
78 public String getCustAddress() {
79 return custAddress;
80 }
81
82 public void setCustAddress(String custAddress) {
83 this.custAddress = custAddress;
84 }
85
86 public String getCustPhone() {
87 return custPhone;
88 }
89
90 public void setCustPhone(String custPhone) {
91 this.custPhone = custPhone;
92 }
93
94 // toString
95 @Override
96 public String toString() {
97 return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource
98 + ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress
99 + ", custPhone=" + custPhone + "]";
100 }
101
102 }
103

3.工具类-JPAUtil,用于启动创建一次EntityManagerFactory,并获取EntityManager
   

  1  package huguangqin.com.cnblogs.util;
2 import javax.persistence.EntityManager;
3 import javax.persistence.EntityManagerFactory;
4 import javax.persistence.Persistence;
5
6 public class JPAUtil {
7 private static EntityManagerFactory factory;
8
9 static {
10 factory = Persistence.createEntityManagerFactory("unit");
11 }
12
13 public static EntityManager getEntityManager() {
14 return factory.createEntityManager();
15 }
16 }
17

4.测试代码

  1 package huguangqin.com.cnblogs.demo;
2
3 import javax.persistence.EntityManager;
4 import javax.persistence.EntityTransaction;
5
6 import org.junit.Test;
7
8 import huguangqin.com.cnblogs.entity.Customer;
9 import huguangqin.com.cnblogs.util.JPAUtil;
10
11 /**
12 * 保存一个数据到客户表中:persist
13 */
14 public class JPATest1 {
15 @Test
16 public void addTest() {
17 // 获取EntityManager
18 EntityManager em = JPAUtil.getEntityManager();
19 // 得到事务对象
20 EntityTransaction et = em.getTransaction();
21 // 开启事务
22 et.begin();
23 // CRUD
24 Customer cust = new Customer();
25 cust.setCustName("阿里");
26 // 持久化
27 em.persist(cust);
28 // 提交事务
29 et.commit();
30 // 关闭资源
31 em.close();
32 }
33
34 /**
35 * 根据id查询:find
36 */
37 @Test
38 public void findTest() {
39 // 获取EntityManager
40 EntityManager em = JPAUtil.getEntityManager();
41 // 得到事务对象
42 EntityTransaction et = em.getTransaction();
43 // 开启事务
44 et.begin();
45 // 根据ID查询
46 Customer customer = em.find(Customer.class, 1L);
47 System.out.println(customer);
48
49 // 提交事务
50 et.commit();
51 // 关闭资源
52 em.close();
53 }
54
55 /**
56 * 延迟加载测试(getReference),用到的时候才发送sql
57 */
58 @Test
59 public void delayTest() {
60 // 获取EntityManager
61 EntityManager em = JPAUtil.getEntityManager();
62 // 获取事务对象
63 EntityTransaction et = em.getTransaction();
64 // 开启事务
65 et.begin();
66 // 延迟加载
67 Customer cust = em.getReference(Customer.class, 1L);
68 System.out.println(cust);
69 // 提交事务
70 et.commit();
71 // 关闭资源
72 em.close();
73 }
74
75 /**
76 * 修改:查询-修改(setter)
77 */
78 @Test
79 public void updateTest() {
80 // 获取EntityManager
81 EntityManager em = JPAUtil.getEntityManager();
82 // 获取事务对象
83 EntityTransaction et = em.getTransaction();
84 // 开启事务
85 et.begin();
86 // 修改
87 Customer cust = em.find(Customer.class, 1L);
88 cust.setCustName("Tecent");
89
90 // 提交事务
91 et.commit();
92 // 关闭资源
93 em.close();
94 }
95
96 /**
97 *删除 remove
98 */
99 @Test
100 public void delTest() {
101 // 获取EntityManager
102 EntityManager em = JPAUtil.getEntityManager();
103 // 获取事务对象
104 EntityTransaction et = em.getTransaction();
105 // 开启事务
106 et.begin();
107 // 删除
108 Customer cust = em.find(Customer.class, 1L);
109 em.remove(cust);
110 // 提交事务
111 et.commit();
112 // 关闭资源
113 em.close();
114 }
115 }
116

最新文章

  1. SQL基础教程--实现增删查改功能(W3School)
  2. Ajax实现原理
  3. 区分苹果Safari浏览器
  4. 聚合索引(clustered index) / 非聚合索引(nonclustered index)
  5. Redhat Linux 修改主机名(HOSTNAME)
  6. Tesseract-OCR text2image.exe [ x86 支持 XP ]
  7. 【iCore2双核心板视频教程二】iM_LAN 100M 以太网模块TCP通信例程
  8. Creating Timer in Oracle D2k / Forms 6i and Displaying a Clock
  9. 实例:用jQuery实现垂直和水平下拉 菜单
  10. Web项目练习总结(错误校正篇)
  11. java集合分析(转载)
  12. Swift - 加速传感器(CoreMotion)的用法,小球加速运动并反弹样例
  13. [置顶] JAVA从零单排4-----继承、封装和多态详解
  14. iptables原理详解以及功能说明
  15. psql命令
  16. 怎么调试nodejs restful API 以及API的Authorization
  17. .NET中的各种池
  18. 5. React 组件的协同使用 组件嵌套和Mixin
  19. eclipse maven install 报错 jdk rather than jre?
  20. URI,url简介

热门文章

  1. C++ 从内存的角度,学习虚继承机制
  2. .net core webapi +ddd(领域驱动)+nlog配置+swagger配置 学习笔记(2)
  3. CentOS7 搭建 rsync 服务器
  4. THE WAY TO HACKER
  5. 【Python之os模块】使用
  6. Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)
  7. python web开发之flask框架学习(1) 创建flask项目
  8. 问题:Tomcat启动产生错误严重: Error initializing endpoint java.lang.Exception
  9. js 设置 cookie
  10. springMvc json 参数