Mybatis关联查询
2016-06-28 16:15
441 查看
三张表:user article blog
配置文件Configuration.xml
Article.xml的配置:
Blog类中有一个List<Article>。
Blog.xml配置:
IArticleOperation定义:
IBlogOperation定义:
Mybatis的“N+1查询问题”
示例:
有两个查询语句:一个来加载博客,另外一个来加载作者,而且博客的结果映射描述了“selectAuthor”语句应该被用来加载它的 author 属性。
其他所有的属性将会被自动加载,假设它们的列和属性名相匹配。
这种方式很简单,但是对于大型数据集合和列表将不会表现很好。问题就是我们熟知的“N+1 查询问题”。概括地讲,N+1 查询问题可以是这样引起的:
你执行了一个单独的 SQL 语句来获取结果列表(就是“+1”)。
对返回的每条记录,你执行了一个查询语句来为每个加载细节(就是“N”)。
这个问题会导致成百上千的 SQL 语句被执行。这通常不是期望的。
可以采用关联的嵌套结果来解决这个问题:
resultMap 这是结果映射的 ID,可以映射关联的嵌套结果到一个合适的对象图中。这是一种替代方法来调用另外一个查询语句。
配置文件Configuration.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- mybatis别名定义 --> <typeAliases> <typeAlias alias="User" type="com.mybatis.test.User"/> <typeAlias alias="Article" type="com.mybatis.test.Article"/> <typeAlias alias="Blog" type="com.mybatis.test.Blog"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" /> <property name="username" value="root"/> <property name="password" value="admin"/> </dataSource> </environment> </environments> <!-- mybatis的mapper文件,每个xml配置文件对应一个接口 --> <mappers> <mapper resource="com/mybatis/test/User.xml"/> <mapper resource="com/mybatis/test/Article.xml"/> <mapper resource="com/mybatis/test/Blog.xml"/> </mappers> </configuration>
Article.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.mybatis.test.IArticleOperation"> <resultMap type="User" id="userResultMap"> <!-- 属性名和数据库列名映射 --> <id property="id" column="user_id" /> <result property="userName" column="user_userName" /> <result property="userAge" column="user_userAge" /> <result property="userAddress" column="user_userAddress" /> </resultMap> <!-- User join Article进行联合查询 (一对一)--> <resultMap id="articleResultMap" type="Article"> <id property="id" column="article_id" /> <result property="title" column="article_title" /> <result property="content" column="article_content" /> <!-- 将article的user属性映射到userResultMap --> <association property="user" javaType="User" resultMap="userResultMap"/> </resultMap> <!-- 使用别名来映射匹配 --> <select id="getUserArticles" parameterType="int" resultMap="articleResultMap"> select user.id user_id,user.userName user_userName,user.userAddress user_userAddress, article.id article_id,article.title article_title,article.content article_content from user,article where user.id=article.userid and user.id=#{id} </select> <!-- 另一种联合查询 (一对一)的实现,但是这种方式有“N+1”的问题 --> <!-- <resultMap id="articleResultMap" type="Article"> <id property="id" column="article_id" /> <result property="title" column="article_title" /> <result property="content" column="article_content" /> <association property="user" javaType="User" column="userid" select="selectUser"/> </resultMap> <select id="selectUser" parameterType="int" resultType="User"> select * from user where id = #{id} </select> --> </mapper>
Blog类中有一个List<Article>。
Blog.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.mybatis.test.IBlogOperation"> <resultMap id="userResultMap" type="User"> <id property="id" column="user_id" /> <result property="userName" column="user_userName" /> <result property="userAge" column="user_userAge" /> <result property="userAddress" column="user_userAddress" /> </resultMap> <resultMap id="articleResultMap" type="Article"> <id property="id" column="article_id" /> <result property="title" column="article_title" /> <result property="content" column="article_content" /> <association property="user" javaType="User" resultMap="userResultMap"/> </resultMap> <resultMap id="blogResultMap" type="Blog"> <id property="id" column="blog_id" /> <result property="title" column="blog_title" /> <!-- 将article list属性映射到collection --> <collection property="articles" ofType="Article" resultMap="articleResultMap"/> </resultMap> <!-- select语句 --> <select id="getBlogByID" parameterType="int" resultMap="blogResultMap"> select user.id user_id,user.userName user_userName,user.userAddress user_userAddress, article.id article_id,article.title article_title,article.content article_content, blog.id blog_id, blog.title blog_title from user,article,blog where user.id=article.userid and blog.id=article.blogid and blog.id=#{id} </select> </mapper>
IArticleOperation定义:
package com.mybatis.test; import java.util.List; public interface IArticleOperation { public List<Article> getUserArticles(int userID); }
IBlogOperation定义:
package com.mybatis.test; public interface IBlogOperation { Blog getBlogByID(int id); }
Mybatis的“N+1查询问题”
示例:
<resultMap id=”blogResult” type=”Blog”> <association property="author" column="blog_author_id" javaType="Author" select=”selectAuthor” /> </resultMap> <select id=”selectBlog” parameterType=”int” resultMap=”blogResult”> SELECT * FROM BLOG WHERE ID = #{id} </select> <select id=”selectAuthor” parameterType=”int” resultType="Author"> SELECT * FROM AUTHOR WHERE ID = #{id} </select>
有两个查询语句:一个来加载博客,另外一个来加载作者,而且博客的结果映射描述了“selectAuthor”语句应该被用来加载它的 author 属性。
其他所有的属性将会被自动加载,假设它们的列和属性名相匹配。
这种方式很简单,但是对于大型数据集合和列表将不会表现很好。问题就是我们熟知的“N+1 查询问题”。概括地讲,N+1 查询问题可以是这样引起的:
你执行了一个单独的 SQL 语句来获取结果列表(就是“+1”)。
对返回的每条记录,你执行了一个查询语句来为每个加载细节(就是“N”)。
这个问题会导致成百上千的 SQL 语句被执行。这通常不是期望的。
可以采用关联的嵌套结果来解决这个问题:
<resultMap id="blogResult" type="Blog"> <id property=”id” column="blog_id" /> <result property="title" column="blog_title" /> <association property="author" column="blog_author_id" javaType="Author" resultMap=”authorResult” /> </resultMap> <resultMap id="authorResult" type="Author"> <id property="id" column="author_id" /> <result property="username" column="author_username" /> <result property="password" column="author_password" /> <result property="email" column="author_email" /> <result property="bio" column="author_bio" /> </resultMap>
resultMap 这是结果映射的 ID,可以映射关联的嵌套结果到一个合适的对象图中。这是一种替代方法来调用另外一个查询语句。
相关文章推荐
- Mybatis传递多个参数的解决办法(三种)
- 获取Java的MyBatis框架项目中的SqlSession的方法
- Mybatis逆工程jar包的修改和打包
- 深入浅析mybatis oracle BLOB类型字段保存与读取
- MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法
- 详解Java的MyBatis框架和Spring框架的整合运用
- Java的MyBatis框架项目搭建与hellow world示例
- SpringMVC整合mybatis实例代码
- Java实现mybatis批量插入数据到Oracle
- oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert
- MyBatis学习笔记(二)之关联关系
- 浅析Mybatis 在CS程序中的应用
- Java Mybatis框架入门基础教程
- Windows下Java+MyBatis框架+MySQL的开发环境搭建教程
- Mybatis与Ibatis的区别
- MyBatis学习教程(二)―如何使用MyBatis对users表执行CRUD操作
- Java+MyBatis+MySQL开发环境搭建流程详解
- MyBatis学习教程(四)-如何快速解决字段名与实体类属性名不相同的冲突问题
- Java环境中MyBatis与Spring或Spring MVC框架的集成方法
- spring、mybatis 配置方式详解(常用两种方式)