微信PK10源码出售持久化类(初步)---- Hibernate入门学习

持久化类的概述和编写微信PK10源码出售Q2152876294 论坛:diguaym.com

  1. 什么叫做类的持久化
    学习什么是类的持久化首先要明白什么叫做持久化,然后再考虑在hibernate中什么是类的持久化;

什么是持久化?
持久化(Persistence),即把数据(如内存中的对象)保存到可永久保存的存储设备中(如磁盘、数据库等)。持久化的主要应用是将内存中的对象存储在数据库中,或者存储在磁盘文件中、XML数据文件中等等;

什么是持久化类?
在应用程序中,用来实现业务问题实体的类就是持久化类。不能认为所有的持久化类的实例都是持久的状态——一个实例的状态也可能是瞬时的或脱管的。就如同它的名字暗示的,它的实例会被持久性保存于数据库中。在hibernate中持久化类就是将一个Java对象与数据库建立了映射关系,则这个类在hibernate中被称为是被持久化的类,更通俗的讲就是: 持久化类 = Java类 + 映射文件

  1. 持久化类的编写规则
    了解其编写规则,先看一个完整的代码,然后对照自己看到的和记录的是否有出入,代码如下:

package com.java.demo;

public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;

public Long getCust_id() {
    return cust_id;
}
public void setCust_id(Long cust_id) {
    this.cust_id = cust_id;
}
public String getCust_name() {
    return cust_name;
}
public void setCust_name(String cust_name) {
    this.cust_name = cust_name;
}
public String getCust_source() {
    return cust_source;
}
public void setCust_source(String cust_source) {
    this.cust_source = cust_source;
}
public String getCust_industry() {
    return cust_industry;
}
public void setCust_industry(String cust_industry) {
    this.cust_industry = cust_industry;
}
public String getCust_level() {
    return cust_level;
}
public void setCust_level(String cust_level) {
    this.cust_level = cust_level;
}
public String getCust_phone() {
    return cust_phone;
}
public void setCust_phone(String cust_phone) {
    this.cust_phone = cust_phone;
}
public String getCust_mobile() {
    return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
    this.cust_mobile = cust_mobile;
}
@Override
public String toString() {
    return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
            + ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone
            + ", cust_mobile=" + cust_mobile + "]";
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
看完代码再看其编写规则,看看自己能看出几个规则来,O(∩_∩)O,其规则如下表所示:

编号 规则 原因
1 持久化对象提供一个无参数的构造方法 hibernate底层需要使用反射生成实例
2 属性需要私有,对私有的属性提供public的get和set方法 hibernate中获取,在hibernate中进行操作做所需要的设置,安全;
3 对持久化类提供一个唯一标识OID与数据库中的主键对应 Java中通过数据地址来确定是否是同一条数据,数据库中通过主键来确定是否为同一条数据,在hibernate中通过持久化类的OID属性来确定是否为同一个对象;
4 持久化类中属性尽量使用包装类类型 若是使用基本类型,则数据默认为0,会产生歧义;而用包装类类型进行包装后数据的默认值为null,数据操作后为0的会显示为为0,未操作过的显示为null,不会搞混;
5 持久化类中不要使用final进行修饰 因为延迟加载是hibernate的一个优化手段,延迟加载与load()方法有关,而load()方法中使用的是所要操作的对象的代理对象,而这个代理对象需要继承索要操作的对象,一旦持久化类被加上final后就不允许被继承,那么这个load()方法的延迟加载就不存在;
本章只是对持久化类进行初步的讲解,后边还有一章是对持久化类的瞬时态、持久态和脱管态的三种状态详细介绍。

  1. 了解:
    包装类和基本类:
    Java中的基本类型有八大类:boolean、char、byte、short、int、float、long、double;
    其分别对应的包装类分别是:Boolean、Character、Byte、Short、Integer、Float、Long、Double;

猜你喜欢

转载自blog.51cto.com/13944793/2165935