Mybatis | 关联查询

作者: 峰眼看世界 | 来源:发表于2020-03-05 06:45 被阅读0次
    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 | 关联查询

        本文链接:https://www.haomeiwen.com/subject/hgfrqhtx.html