官方文档

Mapper接口

public interface UserMapper {
User getUser(int userId);
}
public interface ArticleMapper {
List<Article> getArticleByUserId(int userId);
}

第一种方法

ArticleMapper.xml


<resultMap id="article" type="com.mybatis.model.Article">
<id property="articleId" column="article_id" ></id>
<result property="title" column="title"></result>
<result property="content" column="content"></result>
<result property="createDate" column="create_date" jdbcType="TIMESTAMP"></result>
<association property="user" column="user_id" select="com.mybatis.mapper.UserMapper.getUser"></association>
</resultMap>
<select id="getArticleByUserId" parameterType="int" resultMap="article">
SELECT
a.article_id,
a.user_id,
a.title,
a.content,
a.create_date
FROM
t_artile a
where a.user_id = #{id}
</select>

UserMapper.xml

    <select id="getUser" parameterType="int" resultType="com.mybatis.model.User">
select user_id as userId,
username,
password,
age
from t_user where user_id=#{id}
</select>

ArticleMapper.xml 中使用的是UserMapper的查询方法的接口,这样有两个查询语句,一个查询文章,一个查询用户。

这种方式很简单, 但是对于大型数据集合和列表将不会表现很好。 问题就是我们熟知的 “N+1 查询问题”。概括地讲,N+1 查询问题可以是这样引起的:

  • 你执行了一个单独的 SQL 语句来获取结果列表(就是“+1”)。
  • 对返回的每条记录,你执行了一个查询语句来为每个加载细节(就是“N”)。

这个问题会导致成百上千的 SQL 语句被执行。这通常不是期望的。

MyBatis 能延迟加载这样的查询就是一个好处,因此你可以分散这些语句同时运行的消 耗。然而,如果你加载一个列表,之后迅速迭代来访问嵌套的数据,你会调用所有的延迟加 载,这样的行为可能是很糟糕的。

第二种方法

ArticleMapper.xml

<resultMap id="article" type="com.mybatis.model.Article">
<id property="articleId" column="article_id" ></id>
<result property="title" column="title"></result>
<result property="content" column="content"></result>
<result property="createDate" column="create_date" jdbcType="TIMESTAMP"></result>
<association property="user" column="user_id" resultMap="user"></association>
</resultMap> <resultMap id="user" type="com.mybatis.model.User">
<id property="userId" column="user_id"></id>
<result property="username" column="username"></result>
<result property="password" column="password"></result>
<result property="age" column="age"></result>
</resultMap>
--------------OR-------------------------
<resultMap id="article" type="com.mybatis.model.Article">
<id property="articleId" column="article_id" ></id>
<result property="title" column="title"></result>
<result property="content" column="content"></result>
<result property="createDate" column="create_date" jdbcType="TIMESTAMP"></result>
<association property="user" javaType="com.mybatis.model.User" >
<id property="userId" column="user_id"></id>
<result property="username" column="username"></result>
<result property="password" column="password"></result>
<result property="age" column="age"></result>
</association>
</resultMap>
<select id="getArticleByUserId" parameterType="int" resultMap="article">
SELECT
a.article_id,
a.user_id,
a.title,
a.content,
a.create_date,
b.username,
b.password,
b.age
FROM
t_artile a
INNER JOIN t_user b ON a.user_id = b.user_id
where a.user_id = #{id}
</select>

这样写sql时就要关联user表了。

最新文章

  1. loadrunner --global schedule设置
  2. Android Studio 入门指南
  3. 20145235李涛 《Java程序设计》第3周学习总结
  4. NeHe OpenGL教程 第十五课:纹理图形字
  5. cisco路由基于策略的路由选择
  6. [CF]codeforces round 369(div2)
  7. openlayers wfs获取要素
  8. css中table-layout:fixed 属性的用法
  9. 计数排序之python 实现源码
  10. 《Java程序员面试笔试宝典》之 instanceof有什么作用
  11. 提示框的优化之自定义Toast组件之(二)Toast组件的业务逻辑实现
  12. 电子设计省赛--DMA与ADC
  13. php内存分析
  14. 无法Debug SQL: Unable to start T-SQL Debugging. Could not attach to SQL Server process on
  15. CentOS 7安装Tomcat8
  16. InnoSetup 脚本打包及管理员权限设置
  17. Html 改变原有标签属性
  18. tomcat 请求处理流程分析(基于nio)
  19. MySQL5.5 安装配置方法教程
  20. 16、JDBC-DBUtils封装

热门文章

  1. Oracle建库常用命令
  2. No-sql之redis常用命令
  3. 三种分布式锁 简易说说(包含前一篇提到的redis分布式锁)
  4. P2619 [国家集训队2]Tree I(最小生成树+二分)
  5. c# 获取 Apk ,Aar 文件包名
  6. git账号失效问题解决
  7. JS面向对象——工厂模型
  8. CentOS7单用户模式修改密码
  9. python常用函数 E
  10. rabbitmq必须应答