我们建一个简单的表如下:
+-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | varchar(32) | | PRI | | | | name | varchar(80) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
manager的接口
1package com.martin.pdo;
2
3import java.util.List;
4
5/**
6 * @author martin.xus
7 */
8public interface UserManager {
9 public User load(String id);
10
11 public List loadByName(String name);
12
13 public void add(User user);
14
15 public void remove(User user);
16}
17
2
3import java.util.List;
4
5/**
6 * @author martin.xus
7 */
8public interface UserManager {
9 public User load(String id);
10
11 public List loadByName(String name);
12
13 public void add(User user);
14
15 public void remove(User user);
16}
17
User,在这里,我们的user已经不在是简单的getter&setter,我们引用了一个manager的对象
并且通过spring注入给它,供它使用,这样我们需要操作用户时,就如下一样
添加新用户1User user = (User) context.getBean("user");
2User _u1 = new User();
3//
4user.getManager().add(_u1);
5
读取用户1User _u2 = (User) user.getManager().loadByName("martin xus").get(0);
让User带上manager的功能,这样,我们便不再需要主动的去声明manager。
相对而言,如果我们只是在domain object种getter&setter,就需要如下。
1UserManager userManager = (userManager ) context.getBean("userManager ");
2userManager.saveUser(_u1);
2userManager.saveUser(_u1);
整代码如下:
1package com.martin.pdo;
2
3import java.io.Serializable;
4
5/**
6 * @author martin.xus
7 * @hibernate.class table="t_user"
8 * @spring.bean name="user"
9 * @spring.property name="manager" ref="userManager"
10 */
11public class User implements Serializable {
12
13 /**
14 * @hibernate.id generator-class="uuid.hex"
15 * length="32"
16 * column="user_id"
17 */
18 private String id;
19
20 /**
21 * @hibernate.property column="user_name"
22 * length="80"
23 */
24 private String name;
25
26 /**
27 * @hibernate.property
28 */
29 private char sex;
30
31 /**
32 * @hibernate.property
33 */
34 private int age;
35 private UserManager manager = null;
36
37 /**
38 * @return Returns the manager.
39 */
40 public UserManager getManager() {
41 return manager;
42 }
43
44 /**
45 * @param manager The manager to set.
46 */
47 public void setManager(UserManager manager) {
48 this.manager = manager;
49 }
50
51 /**
52 * @return Returns the age.
53 */
54 public int getAge() {
55 return age;
56 }
57
58 /**
59 * @param age The age to set.
60 */
61 public void setAge(int age) {
62 this.age = age;
63 }
64
65 /**
66 * @return Returns the id.
67 */
68 public String getId() {
69 return id;
70 }
71
72 /**
73 * @param id The id to set.
74 */
75 public void setId(String id) {
76 this.id = id;
77 }
78
79 /**
80 * @return Returns the name.
81 */
82 public String getName() {
83 return name;
84 }
85
86 /**
87 * @param name The name to set.
88 */
89 public void setName(String name) {
90 this.name = name;
91 }
92
93 /**
94 * @return Returns the sex.
95 */
96 public char getSex() {
97 return sex;
98 }
99
100 /**
101 * @param sex The sex to set.
102 */
103 public void setSex(char sex) {
104 this.sex = sex;
105 }
106
107}
108
2
3import java.io.Serializable;
4
5/**
6 * @author martin.xus
7 * @hibernate.class table="t_user"
8 * @spring.bean name="user"
9 * @spring.property name="manager" ref="userManager"
10 */
11public class User implements Serializable {
12
13 /**
14 * @hibernate.id generator-class="uuid.hex"
15 * length="32"
16 * column="user_id"
17 */
18 private String id;
19
20 /**
21 * @hibernate.property column="user_name"
22 * length="80"
23 */
24 private String name;
25
26 /**
27 * @hibernate.property
28 */
29 private char sex;
30
31 /**
32 * @hibernate.property
33 */
34 private int age;
35 private UserManager manager = null;
36
37 /**
38 * @return Returns the manager.
39 */
40 public UserManager getManager() {
41 return manager;
42 }
43
44 /**
45 * @param manager The manager to set.
46 */
47 public void setManager(UserManager manager) {
48 this.manager = manager;
49 }
50
51 /**
52 * @return Returns the age.
53 */
54 public int getAge() {
55 return age;
56 }
57
58 /**
59 * @param age The age to set.
60 */
61 public void setAge(int age) {
62 this.age = age;
63 }
64
65 /**
66 * @return Returns the id.
67 */
68 public String getId() {
69 return id;
70 }
71
72 /**
73 * @param id The id to set.
74 */
75 public void setId(String id) {
76 this.id = id;
77 }
78
79 /**
80 * @return Returns the name.
81 */
82 public String getName() {
83 return name;
84 }
85
86 /**
87 * @param name The name to set.
88 */
89 public void setName(String name) {
90 this.name = name;
91 }
92
93 /**
94 * @return Returns the sex.
95 */
96 public char getSex() {
97 return sex;
98 }
99
100 /**
101 * @param sex The sex to set.
102 */
103 public void setSex(char sex) {
104 this.sex = sex;
105 }
106
107}
108
UserManager
1package com.martin.pdo;
2
3import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
4
5import java.util.List;
6
7/**
8 * @spring.bean name="userManager"
9&nb
2
3import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
4
5import java.util.List;
6
7/**
8 * @spring.bean name="userManager"
9&nb