mybatis注解方式实现一对一的连表查询的方法二

需求:查询所有的账户并且显示账户所对应的用户的信息

  1. 准备实体类:
    Account的实体类上代码:
package com.test.domain;

/**
 * @author:shi jiaojie
 * @date:2019/6/26 20:15
 * @description:
 */
public class Account {
   private Integer id ;
   private Integer uid  ;
   private Double money;
    private User user;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", uid=" + uid +
                ", money=" + money +
                ", user=" + user +
                '}';
    }
}

User的实体类:

package com.test.domain;

import java.util.Date;
import java.util.List;

/**
 * @author:shi jiaojie
 * @date:2019/6/26 20:15
 * @description:
 */
public class User {
    private Integer id ;
    private String username ;
    private String sex  ;
    private Date birthday   ;
    private String address;
    private List<Account> accounts;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public List<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                ", address='" + address + '\'' +
                ", accounts=" + accounts +
                '}';
    }
}

2.然后准备AccountDao,废话少说直接上代码:

package com.test.dao;

import com.test.domain.Account;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @author:shi jiaojie
 * @date:2019/6/26 20:22
 * @description:
 */
public interface AccountDao {
    //查询账户并且显示账户所对应的用户信息
    @Select("select * from account2  order by money desc")
    @Results(id = "AccountUser2",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "id",column = "id"),
            @Result(property = "uid",column = "uid"),
            @Result(property = "money",column = "money"),
            @Result(property = "user",column = "uid",one = @One(select = "com.test.dao.UserDao.findByUid",fetchType = FetchType.LAZY))
    })
    public List<Account> findAccountUser2();
    
}

3.准备UserDao的代码:

package com.test.dao;

import com.test.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author:shi jiaojie
 * @date:2019/6/26 20:22
 * @description:
 */
public interface UserDao {
    //通过uid查询用户信息
    @Select("select * from user2 where id=#{uid}")
    public User findByUid(int uid);
}

4.进行单元测试:

//查询所有的账户并且显示用户的信息222
    @Test
    public void findAccountUser2() {
        List<Account> list = mapper.findAccountUser2();
        for (Account account : list) {
            System.out.println(account);
        }
    }

5.测试显示的结果如下:
在这里插入图片描述如有问题欢迎下方提问交流,喜欢就点个赞哦。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值