Loading

mybatis学习:mybatis注解开发一对一的查询配置

实体类:

 1 public class Account {
 2 
 3     private Integer id;
 4     private Integer uid;
 5     private Double money;
 6 
 7     private User user;
 8 
 9     public User getUser() {
10         return user;
11     }
12 
13     public void setUser(User user) {
14         this.user = user;
15     }
16     public Integer getId() {
17         return id;
18     }
19 
20     public void setId(Integer id) {
21         this.id = id;
22     }
23 
24     public Integer getUid() {
25         return uid;
26     }
27 
28     public void setUid(Integer uid) {
29         this.uid = uid;
30     }
31 
32     public Double getMoney() {
33         return money;
34     }
35 
36     public void setMoney(Double money) {
37         this.money = money;
38     }
39 
40     @Override
41     public String toString() {
42         return "Account{" +
43                 "id=" + id +
44                 ", uid=" + uid +
45                 ", money=" + money +
46                 '}';
47     }
48 }

dao层:

 1 public interface IAccountDao {
 2 
 3     /**
 4      * 查询所有账户,并且获取每个账户所属的用户信息
 5      * @return
 6      */
 7     @Select("select * from account")
 8     @Results(id = "accountMap",value = {
 9             @Result(id = true,column = "id",property = "id"),
10             @Result(column = "uid",property = "uid"),
11             @Result(column = "money",property = "money"),
12             @Result(property = "user",column = "uid",one = @One(select = "cn.flypig666.dao.IUserDao.findById",fetchType = FetchType.EAGER))
13     })
14     List<Account> findAll();
15 }

test:

@Test
    public void testFindAll(){
        List<Account> accounts = accountDao.findAll();
        for (Account account : accounts) {
            System.out.println("账户信息:");
            System.out.println(account);
            System.out.println(account.getUser());
        }
    }

 

posted @ 2019-09-11 15:21  小飞猪咯咯  阅读(715)  评论(0编辑  收藏  举报