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 +
                '}';
    }
}

AccountDao:

package com.test.dao;

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

import java.util.List;

/**
 * @author:shi jiaojie
 * @date:2019/6/26 20:22
 * @description:
 */
public interface AccountDao {
    //查询账户并且显示账户所对应的用户信息
    @Select("select u.*,a.id as aid,a.uid,a.money from account2 a left join user2 u  on a.uid=u.id order by a.money desc")
    @Results(id = "AccountUser",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "id",column = "aid"),
            @Result(property = "uid",column = "uid"),
            @Result(property = "money",column = "money"),
            @Result(property = "user.id",column = "id"),
            @Result(property = "user.username",column = "username"),
            @Result(property = "user.sex",column = "sex"),
            @Result(property = "user.birthday",column = "birthday"),
            @Result(property = "user.address",column = "address")
    })
    public List<Account> findAccountUser();
}

以上步骤做完后就开始测试了上代码:

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

运行结果如下图所示:
在这里插入图片描述谢谢你的阅览,如有疑问欢迎在下方提问。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值