mybatis 查询一对一
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 查询一对一的更多相关文章
- MyBatis:一对一关联查询
MyBatis从入门到放弃三:一对一关联查询 前言 简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化. 那么mybatis实现一对一的关联查询则是 ...
- java web(六):mybatis之一对一、一对多、多对多映射
前言: 百度百科: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...
- mybatis 查询 xml list参数
mybatis 查询 xml list参数: <select id="getByIds" resultType="string" parameterTyp ...
- MyBatis 查询映射自定义枚举
背景 MyBatis查询若想映射枚举类型,则需要从 EnumTypeHandler 或者 EnumOrdinalTypeHandler 中选一个来使用 ...
- 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的代码没有考虑到并发的情况 ...
- mybatis查询语句的背后
转载请注明出处... 一.前言 在先了解mybatis查询之前,先大致了解下以下代码的为查询做了哪些铺垫,在这里我们要事先了解,myabtis会默认使用DefaultSqlSessionFactory ...
- mybatis查询语句的背后之参数解析
转载请注明出处... 一.前言 通过前面我们也知道,通过getMapper方式来进行查询,最后会通过mapperMehod类,对接口中传来的参数也会在这个类里面进行一个解析,随后就传到对应位置,与sq ...
- mybatis查询语句的背后之封装数据
转载请注明出处... 一.前言 继上一篇mybatis查询语句的背后,这一篇主要围绕着mybatis查询的后期操作,即跟数据库交互的时候.由于本人也是一边学习源码一边记录,内容难免有错误或不足之处,还 ...
- mybatis查询结果和接收的不一样
记一次大坑:mybatis查询结果和接收的不一样,折腾我好几个小时. 先上代码:代码是要查询排名,sql执行的结果 SELECT b.operator_id, b.class_count, b.cla ...
随机推荐
- 二维码生成器,基于python,segno库
import segno temp = input("Please enter value:") qr = segno.make(temp) qr.save("qrcod ...
- dp(最长升序列:二分查找时间优化nlogn)
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, sele ...
- 获取Linux内核未导出符号的几种方式
从Linux内核的2.6某个版本开始,内核引入了导出符号的机制.只有在内核中使用EXPORT_SYMBOL或EXPORT_SYMBOL_GPL导出的符号才能在内核模块中直接使用.然而,内核并没有导出所 ...
- bzoj1897. tank 坦克游戏(决策单调性分治)
题目描述 有这样一款新的坦克游戏.在游戏中,你将操纵一辆坦克,在一个N×M的区域中完成一项任务.在此的区域中,将会有许多可攻击的目标,而你每摧毁这样的一个目标,就将获得与目标价值相等的分数.只有获得了 ...
- Mybatis-技术专区-中的条件查询createCriteria example里面的条件
之前用Mybatis框架反向的实体,还有实体里面的Example,之前只是知道Example里面放的是条件查询的方法,可以一直不知道怎么用,到今天才开始知道怎么简单的用. 在我们前台查询的时候会有许多 ...
- Linux查看关闭进程
ps:进程的静态列表(Process status) - PID:进程号,每个进程独一无二的标识符(关闭进程需要使用) - TTY:终端所属,表明进程产生于哪一个终端,对于多用户使用的Linux服务器 ...
- docker镜像源设置
由于docker默认镜像源为国外官方源,下载速度较慢.设置国内镜像源可加速 修改文件 /etc/docker/daemon.json vi /etc/docker/daemon.json 添加以下内容 ...
- psfgettable - 从控制台字体中提取出嵌入的Unicode字符表
总览 psfgettable 字体文件 [输出文件] 描述 psfgettable 命令从一个 .psf 格式的控制台字体中提取出嵌入的 Unicode字符表, 以易读格式输入到一个ASCII文件, ...
- Kotlin定义静态变量、静态方法
Kotlin定义静态变量.静态方法kotlin定义静态变量.方法可以采用伴生对象companion object的方式. 经典例子,实例化Fragment. java写法: public class ...
- java Scanner输入数字、字符串
package java05; import java.util.Scanner;//1.导包 /* Scanner类的功能,可以实现键盘输入数据,到程序当中 引用类型的一班使用步骤: 1.导包 2. ...