Spring+Hibernate+Struts2整合[POJO类配置][3/6]

上一章节主要讲解了WEB.XML的配置,这一章节主要讲解了关于POJO类的具体内容


 

在创建pojo类时,实现Serializable接口,主要原因:

  1. 将对象的状态保存在存储媒体中以便可以在以后重新创建出完全相同的副本;
  2. 按值将对象从一个应用程序域发送至另一个应用程序域。

实现serializable接口的作用是就是可以把对象存到字节流,然后可以恢复。所以你想如果你的对象没实现序列化怎么才能进行网络传输呢,要网络传输就得转为字节流,所以在分布式应用中,你就得实现序列化,如果你不需要分布式应用,那就没那个必要实现序列化。

以一个客户类为例子

 1 /**
 2  * 客户的实体类
 3  * 
 4  * @author Administrator
 5  *
 6  */
 7 
 8 @Entity
 9 @Table(name = "cst_customer")
10 public class Customer implements Serializable {
11     @Id
12     @Column(name = "cust_id")
13     @GeneratedValue(strategy = GenerationType.IDENTITY)
14     private Long custId;
15 
16     @Column(name = "cust_name")
17     private String custName;
18 
19     @Column(name = "cust_source")
20     private String custSource;
21 
22     @Column(name = "cust_industry")
23     private String custIndustry;
24 
25     @Column(name = "cust_level")
26     private String custLevel;
27 
28     @Column(name = "cust_address")
29     private String custAddress;
30 
31     @Column(name = "cust_phone")
32     private String custPhone;
33 
34     public Long getCustId() {
35         return custId;
36     }
37 
38     public void setCustId(Long custId) {
39         this.custId = custId;
40     }
41 
42     public String getCustName() {
43         return custName;
44     }
45 
46     public void setCustName(String custName) {
47         this.custName = custName;
48     }
49 
50     public String getCustSource() {
51         return custSource;
52     }
53 
54     public void setCustSource(String custSource) {
55         this.custSource = custSource;
56     }
57 
58     public String getCustIndustry() {
59         return custIndustry;
60     }
61 
62     public void setCustIndustry(String custIndustry) {
63         this.custIndustry = custIndustry;
64     }
65 
66     public String getCustLevel() {
67         return custLevel;
68     }
69 
70     public void setCustLevel(String custLevel) {
71         this.custLevel = custLevel;
72     }
73 
74     public String getCustAddress() {
75         return custAddress;
76     }
77 
78     public void setCustAddress(String custAddress) {
79         this.custAddress = custAddress;
80     }
81 
82     public String getCustPhone() {
83         return custPhone;
84     }
85 
86     public void setCustPhone(String custPhone) {
87         this.custPhone = custPhone;
88     }
89 
90     @Override
91     public String toString() {
92         return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource
93                 + ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress
94                 + ", custPhone=" + custPhone + "]";
95     }
96 
97 }
Customer实体类源代码
  • 配置方式有2种:

1.pojo.cfg.xml和applicationContext.xml配置文件方式配置

将映射关系用pojo.cfg.xml进行配置,然后在applicationContext.xml中的sessionFactory中引用(Spring+Hibernate+Struts2整合第一章节具体讲解,请回看理解具体含义)

2.注解的方式配置

@Entity:表示实体类
@Table(name = "cst_customer"):设置表名

@Id:设置主键
@Column(name = "cust_id"):设置主键列名
@GeneratedValue(strategy = GenerationType.IDENTITY):设置主键策略

@Column(name = "cust_level"):主要设置表对应字段的名字


 

以上主要讲解了POJO类的主要配置详情,接下来将讲解Dao层的配置详情,请看下一章节内容。

posted @ 2018-06-15 10:25  呦,可以呦  阅读(109)  评论(0)    收藏  举报