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);
}