您的位置:首页 > 其它

mybatis基于注解的关联查询

2018-05-23 22:20 309 查看
一、使用场景

mybatis多表关联查询,1对1,1对多

public class User implements UserDetails, Serializable {
//parent是一个对象,有自己的表,这是1对1的多表关联查询
private Parent scale;
//Authority是一个对象,有自己的表,这是1对多的查询
private List<Authority> authoritys;


二、参考文献

https://blog.csdn.net/gluawwa/article/details/53289162(三、方案中的代码复制的这篇博客里的,用作备份,已实践通过)

三、方案

public interface UserInfoDAO {
//一个user对应一个userinfo
@Select("select * from test_userinfo where id =#{id}")
public UserInfo getUserInfoById(@Param("id") String id);

//select ="com.zwk.dao.UserInfoDAO.getUserInfoById",getUserinfoById方法必须存在
@Select("select * from test_user u where u.id = #{id}")
@Results({
@Result(id = true,property = "id" ,column = "id"),
@Result(property ="password",column = "password"),
@Result(property ="userInfo",column="info_id",one =@One(select ="com.zwk.dao.UserInfoDAO.getUserInfoById"))}
)
public User getById(@Param("id") String id);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: