java实体 注解 一对多,MyBatis注解一对一和一对多

## 使用的类

~~~

package com.like.dao;

import com.like.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;

public interface IAccountDao

{

/**

* 查询所有账户,并且获得每个账户所属用户信息

*/

@Select("select * from account")

@Results(id = "accountMap", value = {

@Result(id = true, property = "id", column = "id"),

@Result(property = "uid", column = "uid"),

@Result(property = "money", column = "money"),

//property是关联表在本表的属性,column是关联字段,select是关联接口根据id查询数据的全限定方法名,fetchType是获取方式,此处是立即获取

@Result(property = "user", column = "uid", one = @One(select = "com.like.dao.IUserDao.findById", fetchType = FetchType.EAGER))

})

List findAll();

@Select("select * from account where uid = #{uid}")

List findAccountByUid(Integer uid);

}

~~~

~~~

package com.like.dao;

import com.like.domain.User;

import java.util.List;

import org.apache.ibatis.annotations.*;

import org.apache.ibatis.mapping.FetchType;

public interface IUserDao

{

@Select("select * from user")

@Results(id = "userMap", value = {

@Result(id = true, property = "id", column = "id"),

@Result(property = "username", column = "username"),

@Result(property = "address", column = "address"),

@Result(property = "birthday", column = "birthday"),

@Result(property = "sex", column = "sex"),

//property是关联表在本表的属性,column是关联字段,select是关联接口根据id查询数据的全限定方法名,fetchType是获取方式,此处是立即获取

@Result(property = "accounts", column = "id", many = @Many(select = "com.like.dao.IAccountDao.findAccountByUid", fetchType = FetchType.LAZY))

})

List findAll();

@Select("select * from user where id = #{id}")

User findById(Integer id);

}

~~~

~~~

package com.like.domain;

public class Account

{

private Integer id;

private Integer uid;

private Double money;

private User user;

@Override

public String toString()

{

return "Account{" +

"id=" + id +

", uid=" + uid +

", money=" + money +

", 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;

}

}

~~~

~~~

package com.like.domain;

import java.io.Serializable;

import java.util.Date;

import java.util.List;

public class User implements Serializable

{

private Integer id;

private String username;

private Date birthday;

private String sex;

private String address;

private List accounts;

@Override

public String toString()

{

return "User{" +

"id=" + id +

", username='" + username + '\'' +

", birthday=" + birthday +

", sex='" + sex + '\'' +

", address='" + address + '\'' +

", accounts=" + 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 Date getBirthday()

{

return birthday;

}

public void setBirthday(Date birthday)

{

this.birthday = birthday;

}

public String getSex()

{

return sex;

}

public void setSex(String sex)

{

this.sex = sex;

}

public String getAddress()

{

return address;

}

public void setAddress(String address)

{

this.address = address;

}

public List getAccounts()

{

return accounts;

}

public void setAccounts(List accounts)

{

this.accounts = accounts;

}

}

~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值