Mybatis嵌套查询讲解内容的基础

本文介绍了一个基于MyBatis实现的关联查询示例,包括四个表的定义及使用XML映射文件进行复杂查询的过程。通过这个例子,读者可以了解到如何在MyBatis中配置一对一、一对多的关系映射。

例子是参考Mybatis文档创建的一个简单例子,一共涉及4个表,每个表都有很少的几个属性。

由于说明该内容需要东西太多,专门分成两篇文章,一篇提供表以及基础代码,一篇专门讲内容。


表如下:

Blog表



Blog_user表



Blog_post表



Blog_comments表



代码(其他对象不在这儿贴出来了,请需要的自己下载):

BlogMapper.java

public interface BlogMapper {
    BlogDto selectById(String id);
}

BlogMapper.xml

<?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.easternie.records.dao.BlogMapper" >
  <resultMap id="userMap" type="com.easternie.records.vo.model.BlogUser">
  	<id property="userid" column="userid"/>
	<result property="username" column="username" />
	<result property="password" column="password" />
	<result property="userinfo" column="userinfo" />
  </resultMap>
  
  <resultMap id="BlogDtoMap" type="com.easternie.records.vo.model.BlogDto">
  	<constructor>
		<idArg column="id" javaType="String"/>  	
  	</constructor>
  	<result property="title" column="title" />
  	<result property="url" column="url" />
  	<result property="userid" column="userid" />
  	<association property="user" resultMap="userMap" />
  	<collection property="posts" ofType="com.easternie.records.vo.model.BlogPostDto">
  		<id property="postid" column="postid" />
  		<!-- <result property="userid" column="userid" /> -->
  		<result property="postdate" column="postdate" />
  		<result property="postinfo" column="postinfo" />
  		<association property="user" resultMap="userMap"/>
  		<collection property="comments" ofType="com.easternie.records.vo.model.BlogComments">
  			<id property="commentid" column="commentid" />
  			<result property="username" column="username2"/>
  			<result property="msg" column="msg"/>
  		</collection>
  	</collection>
  </resultMap>
  
  <select id="selectById" resultMap="BlogDtoMap" parameterType="java.lang.String" >
    select a.id,
       a.title,
       a.userid,
       a.url,
       b.username,
       b.password,
       b.userinfo,
       c.postid,
       c.postdate,
       c.postinfo,
       d.commentid,
       d.username username2,
       d.msg
  from blog a

  left join blog_user b
    on a.userid = b.userid

  left join blog_post c
    on a.userid = c.userid

  left join blog_comments d
    on c.postid = d.postid
 where id = #{id}
  </select>
</mapper>

调用代码:

MyBatis myBatis = MyBatis.getInstance();
		try {
			SqlSessionFactory sessionFactory = myBatis.getSqlSessionFactory();
			SqlSession session = sessionFactory.openSession();
			BlogMapper blogMapper = session.getMapper(BlogMapper.class);
			BlogDto blogDto = blogMapper.selectById("1");
			show(blogDto);
		} catch (Exception e) {
			e.printStackTrace();
		}


下载地址: http://pan.baidu.com/s/1kT7HLdl
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

isea533

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值