## 使用的类
~~~
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;
}
}
~~~