mybatis之关联查询

数据库关系图

数据库关系图

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

实体类

// 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字段与之对应。

源码点这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值