我们建一个简单的表如下:

+-------+-------------+------+-----+---------+-------+
| 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的接口

 1 None.gif package  com.martin.pdo;
 2 None.gif
 3 None.gif import  java.util.List;
 4 None.gif
 5 ExpandedBlockStart.gifContractedBlock.gif /** */ /**
 6InBlock.gif * @author martin.xus
 7ExpandedBlockEnd.gif */

 8 ExpandedBlockStart.gifContractedBlock.gif public   interface  UserManager  dot.gif {
 9InBlock.gif    public User load(String id);
10InBlock.gif
11InBlock.gif    public List loadByName(String name);
12InBlock.gif
13InBlock.gif    public void add(User user);
14InBlock.gif
15InBlock.gif    public void remove(User user);
16ExpandedBlockEnd.gif}

17 None.gif


User,在这里,我们的user已经不在是简单的getter&setter,我们引用了一个manager的对象
并且通过spring注入给它,供它使用,这样我们需要操作用户时,就如下一样

添加新用户



1 None.gif User user  =  (User) context.getBean( " user " );
2 None.gifUser _u1  =   new  User();
3 None.gif // dot.gif
4 None.gif user.getManager().add(_u1);
5 None.gif


读取用户
1 None.gif User _u2  =  (User) user.getManager().loadByName( " martin xus " ).get( 0 );


让User带上manager的功能,这样,我们便不再需要主动的去声明manager。

相对而言,如果我们只是在domain object种getter&setter,就需要如下。

1 None.gif UserManager userManager  =  (userManager ) context.getBean( " userManager  " );
2 None.gifuserManager.saveUser(_u1);


整代码如下:

  1 None.gif package  com.martin.pdo;
  2 None.gif
  3 None.gif import  java.io.Serializable;
  4 None.gif
  5 ExpandedBlockStart.gifContractedBlock.gif /** */ /**
  6InBlock.gif * @author martin.xus
  7InBlock.gif * @hibernate.class table="t_user"
  8InBlock.gif * @spring.bean name="user"
  9InBlock.gif * @spring.property name="manager" ref="userManager"
 10ExpandedBlockEnd.gif */

 11 ExpandedBlockStart.gifContractedBlock.gif public   class  User  implements  Serializable  dot.gif {
 12InBlock.gif
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 14InBlock.gif     * @hibernate.id generator-class="uuid.hex"
 15InBlock.gif     * length="32"
 16InBlock.gif     * column="user_id"
 17ExpandedSubBlockEnd.gif     */

 18InBlock.gif    private String id;
 19InBlock.gif
 20ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 21InBlock.gif     * @hibernate.property column="user_name"
 22InBlock.gif     * length="80"
 23ExpandedSubBlockEnd.gif     */

 24InBlock.gif    private String name;
 25InBlock.gif
 26ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 27InBlock.gif     * @hibernate.property
 28ExpandedSubBlockEnd.gif     */

 29InBlock.gif    private char sex;
 30InBlock.gif
 31ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 32InBlock.gif     * @hibernate.property
 33ExpandedSubBlockEnd.gif     */

 34InBlock.gif    private int age;
 35InBlock.gif    private UserManager manager = null;
 36InBlock.gif
 37ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 38InBlock.gif     * @return Returns the manager.
 39ExpandedSubBlockEnd.gif     */

 40ExpandedSubBlockStart.gifContractedSubBlock.gif    public UserManager getManager() dot.gif{
 41InBlock.gif        return manager;
 42ExpandedSubBlockEnd.gif    }

 43InBlock.gif
 44ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 45InBlock.gif     * @param manager The manager to set.
 46ExpandedSubBlockEnd.gif     */

 47ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setManager(UserManager manager) dot.gif{
 48InBlock.gif        this.manager = manager;
 49ExpandedSubBlockEnd.gif    }

 50InBlock.gif
 51ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 52InBlock.gif     * @return Returns the age.
 53ExpandedSubBlockEnd.gif     */

 54ExpandedSubBlockStart.gifContractedSubBlock.gif    public int getAge() dot.gif{
 55InBlock.gif        return age;
 56ExpandedSubBlockEnd.gif    }

 57InBlock.gif
 58ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 59InBlock.gif     * @param age The age to set.
 60ExpandedSubBlockEnd.gif     */

 61ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setAge(int age) dot.gif{
 62InBlock.gif        this.age = age;
 63ExpandedSubBlockEnd.gif    }

 64InBlock.gif
 65ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 66InBlock.gif     * @return Returns the id.
 67ExpandedSubBlockEnd.gif     */

 68ExpandedSubBlockStart.gifContractedSubBlock.gif    public String getId() dot.gif{
 69InBlock.gif        return id;
 70ExpandedSubBlockEnd.gif    }

 71InBlock.gif
 72ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 73InBlock.gif     * @param id The id to set.
 74ExpandedSubBlockEnd.gif     */

 75ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setId(String id) dot.gif{
 76InBlock.gif        this.id = id;
 77ExpandedSubBlockEnd.gif    }

 78InBlock.gif
 79ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 80InBlock.gif     * @return Returns the name.
 81ExpandedSubBlockEnd.gif     */

 82ExpandedSubBlockStart.gifContractedSubBlock.gif    public String getName() dot.gif{
 83InBlock.gif        return name;
 84ExpandedSubBlockEnd.gif    }

 85InBlock.gif
 86ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 87InBlock.gif     * @param name The name to set.
 88ExpandedSubBlockEnd.gif     */

 89ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setName(String name) dot.gif{
 90InBlock.gif        this.name = name;
 91ExpandedSubBlockEnd.gif    }

 92InBlock.gif
 93ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 94InBlock.gif     * @return Returns the sex.
 95ExpandedSubBlockEnd.gif     */

 96ExpandedSubBlockStart.gifContractedSubBlock.gif    public char getSex() dot.gif{
 97InBlock.gif        return sex;
 98ExpandedSubBlockEnd.gif    }

 99InBlock.gif
100ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
101InBlock.gif     * @param sex The sex to set.
102ExpandedSubBlockEnd.gif     */

103ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setSex(char sex) dot.gif{
104InBlock.gif        this.sex = sex;
105ExpandedSubBlockEnd.gif    }

106InBlock.gif
107ExpandedBlockEnd.gif}

108 None.gif


UserManager

 1 None.gif package  com.martin.pdo;
 2 None.gif
 3 None.gif import  org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 4 None.gif
 5 None.gif import  java.util.List;
 6 None.gif
 7 ExpandedBlockStart.gifContractedBlock.gif /** */ /**
 8InBlock.gif * @spring.bean name="userManager"
 9InBlock.gif&nb