Mybatis一对一,一对多对应关系

  com.lxkj.pojo;
  public class User{
    private String id;
    private String username;
    private String password;
    private list<Account> accounts;
  }

  public class Account{
    private String id;
    private String uid;
    private String money;
    private User user;//账户对用户一对一
  }

1、一对一:
public interface  AccountDao{
   @Select("select * from account") //查询账户的语句
   @Results({
             @Result(id=true,property="id",column="id"), //id=true 表示此字段是主键,property:实体类的属性 column:表字段的属性
             @Result(property="uid",column="uid"), //外键
          @Result(property="money",column="money"),
         @Result(property="user",column="uid",javaType=User.class,one=@One(select="com.lxkj.dao.UserDao.findUser",fetchType=FetchType.LAZY))  //column此处表示通过哪一个属性关联另一种表进行查询,fetchType加载类型(立即记载,懒加载)
     })
  public List<Account> findAccountWithUser();
}
public interface  UserDao{
@select("select * from user where id=#{id}")
  public user findUser(Integer id);
}

 2、一对多: 一个用户有多个账户
public interface  UserDao{ 
@select("select * from user")
 @Results({
             @Result(id=true,property="id",column="id"), //id=true 表示此字段是主键,property:实体类的属性 column:表字段的属性
             @Result(property="username",column="username"), 
           @Result(property="password",column="password"), 
         @Result(property="accounts",column="id",javaType=List.class,many=@Many(select="com.lxkj.dao.AccountDao.findCountWidthUid",fetchType=FetchType.LAZY))  //column此处表示通过哪一个属性关联另一种表进行查询,fetchType加载类型(立即记载,懒加载)
     })
  public List<User> findAllUserWithAccount();
}

public interface  AccountDao{
     @Select("select * from account uid=#{uid}")
  public List<Account> findCountWidthUid(Integer uid);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞腾创客

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值