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