實體類User:
1 public class User implements Serializable { 2 3 private Integer id; 4 private String username; 5 private Date birthday; 6 private String sex; 7 private String address; 8 9 //一對多關系映射:一個用戶對應多個賬戶 10 private List<Account> accounts; 11 12 public List<Account> getAccounts() { 13 return accounts; 14 } 15 16 public void setAccounts(List<Account> accounts) { 17 this.accounts = accounts; 18 } 19 20 public Integer getId() { 21 return id; 22 } 23 24 public void setId(Integer id) { 25 this.id = id; 26 } 27 28 public String getUsername() { 29 return username; 30 } 31 32 public void setUsername(String username) { 33 this.username = username; 34 } 35 36 public Date getBirthday() { 37 return birthday; 38 } 39 40 public void setBirthday(Date birthday) { 41 this.birthday = birthday; 42 } 43 44 public String getSex() { 45 return sex; 46 } 47 48 public void setSex(String sex) { 49 this.sex = sex; 50 } 51 52 public String getAddress() { 53 return address; 54 } 55 56 public void setAddress(String address) { 57 this.address = address; 58 } 59 60 @Override 61 public String toString() { 62 return "User{" + 63 "id=" + id + 64 ", username='" + username + '\'' + 65 ", birthday=" + birthday + 66 ", sex='" + sex + '\'' + 67 ", address='" + address + '\'' + 68 '}'; 69 } 70 }
dao層:
IUserDao:
1 @Select("select * from user") 2 @Results(id = "userMap",value = { 3 @Result(id = true,column = "id",property = "id"), 4 @Result(column = "username",property = "username"), 5 @Result(column = "address",property = "address"), 6 @Result(column = "sex",property = "sex"), 7 @Result(column = "birthday",property = "birthday"), 8 @Result(property = "accounts",column = "id",many = @Many(select = "cn.flypig666.dao.IAccountDao.findAccountByUid", 9 fetchType = FetchType.EAGER)) 10 }) 11 List<User> findAll();
IAccountDao:
/** * 根據用戶id查詢賬戶信息 * @param userId * @return */ @Select("select * from account where uid = #{uid}") List<Account> findAccountByUid(Integer userId); }