Mybatis | 关联查询

mybatis

使用Mybatis进行关联查询,之前发在我的CSDN博客中,现在搬到简书上来。

数据库关系图

数据库关系图

如图是一个博客系统的数据库关系图,用户和博客是一对多的关系(一个用户拥有多个博客),博客和文章是一对多的关系(一个博客拥有多篇文章)。

实体类

// MyUser类
public class MyUser{
    private Integer id;
    private String name; 
    
    // getter and setter
}

// MyBlog类
public class MyBlog {
    private int id;
    private String title;
    private int userId;
    
     // getter and setter ...
}

// MyPost类
public class MyPost {
    private int id;
    private String body;
    private int blogId;
    
  // getter and setter ...    
}

// 博客信息类,包含用户信息和文章列表,作为关联查询结果的实体类
public class MyBlogInfo {
    private int blogId;
    private String title;
    private MyUser myUser;
    private List<MyPost> myPostList;
    
   // getter and setter ...
}

Dao类

@Component
public class MyBlogInfoDao {

    @Autowired
    private MyBlogInfoMapper myBlogInfoMapper;
    
    public MyBlogInfo queryAllBlogInfo(int id)
    {
        return myBlogInfoMapper.queryAllBlogInfo(id);
    }
}

mapper接口类

@Mapper
public interface MyBlogInfoMapper {
    MyBlogInfo queryAllBlogInfo(int id);

    List<MyPost> queryAllBlogInfo1(int id);
}

mapper映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.mapper.MyBlogInfoMapper">
    <resultMap id="MyBlogInfo" type="com.example.bean.MyBlogInfo">
        <id column="id" property="blogId"/>
        <result column="title" property="title"/>
        <association property="myUser" column="user_id" javaType="com.example.bean.MyUser">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
        </association>
        <collection property="myPostList" column="blog_id" ofType="com.example.bean.MyPost" javaType="list">
            <id column="post_id" property="id"/>
            <result column="body" property="body"/>
            <result column="blog_id" property="blogId"/>
        </collection>
    </resultMap>

    <select id="queryAllBlogInfo" resultMap="MyBlogInfo">
        SELECT
            b.id,
            b.title,
            b.user_id,
            u.id,
            u.name,
            p.id AS post_id,
            p.body,
            p.blog_id
        FROM myblog b
        LEFT OUTER JOIN myuser u ON b.user_id = u.id
        LEFT OUTER JOIN mypost p ON p.blog_id = b.id
        WHERE b.id = #{id}
    </select>
</mapper>

controller测试类

@Controller
public class DemoController
{
    @RequestMapping(value = "/queryAllBlogInfo")
    @ResponseBody
    public String queryAllBlogInfo(@RequestParam(value = "id") int id)
    {
        MyBlogInfo myBlogInfo = myBlogInfoDao.queryAllBlogInfo(id);
        return "success!";
    }
}

mapper映射文件中几点注意

  1. 根据MyBlog表查询MyUser,由于MyBlog表中持有MyUser表的主键,所以关联写法为:
    <association property="myUser" column="user_id" javaType="com.example.bean.MyUser"> <id column="id" property="id"/> <result column="name" property="name"/> </association>
  2. 根据MyBlog表查询MyPost, 不同于查询MyUser,这里MyPost表中持有MyBlog表主键,所以关联写法为:
    <collection property="myPostList" column="blog_id" ofType="com.example.bean.MyPost" javaType="list"> <id column="post_id" property="id"/> <result column="body" property="body"/> <result column="blog_id" property="blogId"/> </collection>
    特别注意:这里为 oftype
  3. mybatis多表关联查询(一对多,collection)时,如果两个表的字段名字一样,需要为重名字段指定别名,否则只能查询到一条记录。如上述例子中,MyBlog表和MyPost表中id字段重名,这里在select语句中将MyPost设别名post_id,同时修改<collection><id column="post_id" property="id"/></collection>中id字段与之对应。

备注

  1. 原始博客地址
  2. 源码点这里

图片源自网络,侵权必删!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • MyBatis--关联查询 当查询内容涉及到具有关联关系的多个表时,就需要使用关联查询。根据表与表间的关联关系的不...
    我可能是个假开发阅读 9,648评论 0 16
  • 结果映射(ResultMap) resultMap是MyBatis中非常重要强大的元素。它在处理一些链接比较复杂的...
    丿蜡笔阅读 366评论 0 1
  • 一、概念 所谓的一对一查询,就是说我们在查询一个表的数据的时候,需要关联查询其他表的数据 二、需求 查询所有订单信...
    任未然阅读 358评论 0 1
  • 一、概念 所谓的一对一查询,就是说我们在查询一个表的数据的时候,需要关联查询其他表的数据 二、需求 查询所有订单信...
    唯老阅读 299评论 1 0
  • 一、概念 一对多(多对一)是指一方持有多方的引用。例如:去京东购物,那么一个京东用户可以对应多个购物订单 二、实现...
    任未然阅读 258评论 0 1