官方文档

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表了。

mybatis 查询一对一的更多相关文章

  1. MyBatis:一对一关联查询

    MyBatis从入门到放弃三:一对一关联查询 前言 简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化. 那么mybatis实现一对一的关联查询则是 ...

  2. java web(六):mybatis之一对一、一对多、多对多映射

    前言: 百度百科: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...

  3. mybatis 查询 xml list参数

    mybatis 查询 xml list参数: <select id="getByIds" resultType="string" parameterTyp ...

  4. MyBatis 查询映射自定义枚举

    背景                  MyBatis查询若想映射枚举类型,则需要从 EnumTypeHandler 或者 EnumOrdinalTypeHandler 中选一个来使用         ...

  5. mybatis查询异常-Error querying database. Cause: java.lang.ClassCastException: org.apache.ibatis.executor.ExecutionPlaceholder cannot be cast to java.util.List

    背景,mybatis查询的时候直接取的sqlsession,没有包装成SqlSessionTemplate,没有走spring提供的代理. 然后我写的获取sqlsession的代码没有考虑到并发的情况 ...

  6. mybatis查询语句的背后

    转载请注明出处... 一.前言 在先了解mybatis查询之前,先大致了解下以下代码的为查询做了哪些铺垫,在这里我们要事先了解,myabtis会默认使用DefaultSqlSessionFactory ...

  7. mybatis查询语句的背后之参数解析

    转载请注明出处... 一.前言 通过前面我们也知道,通过getMapper方式来进行查询,最后会通过mapperMehod类,对接口中传来的参数也会在这个类里面进行一个解析,随后就传到对应位置,与sq ...

  8. mybatis查询语句的背后之封装数据

    转载请注明出处... 一.前言 继上一篇mybatis查询语句的背后,这一篇主要围绕着mybatis查询的后期操作,即跟数据库交互的时候.由于本人也是一边学习源码一边记录,内容难免有错误或不足之处,还 ...

  9. mybatis查询结果和接收的不一样

    记一次大坑:mybatis查询结果和接收的不一样,折腾我好几个小时. 先上代码:代码是要查询排名,sql执行的结果 SELECT b.operator_id, b.class_count, b.cla ...

随机推荐

  1. vuex基本使用

    1.组件之间共享数据的方式 父向子传值:v-bind 属性绑定 子向父传值:v-on 事件绑定 兄弟组件之间共享数据:EventBus $on 接收数据的那个组件 $emit 发送数据的那个组件 2. ...

  2. Map与JSON Conver

    function strMapToObj(strMap) {     let obj = Object.create(null);     for (let [k,v] of strMap) {    ...

  3. MySQL-第九篇分组和组函数

    1.组函数 组函数:即多行函数,组函数将一组记录作为整体计算,每组记录返回一个结果,而不是每条记录返回一个结果. 2.常用的组函数有: 1>avg([distinct|all]expr):计算多 ...

  4. Android -ui控件

    一:TextView控件 TextView --> View 1.创建TextView的两种方式: 1.1编写TextView类 TextView tv = new TextView(this) ...

  5. 【洛谷p1464】 Function

    中考第一天: 感觉我超废: 就是看这道题特别顺眼emmm SOLUTION: 思路的话是开三维数组s[i][j][k],先三重for循环预处理出s[0~20][0~20][0~20]的w(a,b,c) ...

  6. 洛谷 P1168 中位数(优先队列)

    题目链接 https://www.luogu.org/problemnew/show/P1168 解题思路 这个题就是求中位数,但是暴力会tle,所以我们用一种O(nlogn)的算法来实现. 这里用到 ...

  7. 洛谷 P1886 滑动窗口(单调队列)

    题目链接 https://www.luogu.org/problemnew/show/P1886 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始 ...

  8. mysql (mariadb)表结构添加修改删除方法总结

    1,添加表字段 alter table table1 add ptel varchar(100) not Null; alter table table1 add id int unsigned no ...

  9. 廖雪峰Python电子书总结

    函数 1.注意:函数的默认参数必须指向不可变对象 未修改前: def add_end(L=[]): L.append('END') return L 存在的问题:如果连续调用多次,会出现多个 'END ...

  10. 八、请求post、get、jsonp

    1.创建个 news 组件使用 2.在module.ts 引入模块 3.在使用的“Component”中不一样.这里是 http和jsonp 4.编写get请求查看效果 (1).编写好的get请求,点 ...