mybatis基于注解的关联查询

一、使用场景

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

猜你喜欢

转载自my.oschina.net/Cubicluo/blog/1817659