前言

上篇学习了一对一关联查询,这篇我们学习一对多关联查询。一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collection属性,别忽略了ofType属性。

搭建开发环境

创建表author、表blog,构建一对多的查询场景。

创建author、blog model。author类中主要是添加属性List<Blog> blogs属性。

public class Author {
private int id;
private String name;
private List<Blog> blogs; public List<Blog> getBlogs() {
return blogs;
} public void setBlogs(List<Blog> blogs) {
this.blogs = blogs;
}
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

 

public class Blog {
private int id;
private String title;
private String category;
private int author_id; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getCategory() {
return category;
} public void setCategory(String category) {
this.category = category;
} public int getAuthor_id() {
return author_id;
} public void setAuthor_id(int author_id) {
this.author_id = author_id;
}
}

  在mybatis.xml创建alias、引用resource mapper.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针对SqlServer进行的配置 -->
<typeAliases>
<typeAlias alias="User" type="com.autohome.model.User"/>
<typeAlias alias="Teacher" type="com.autohome.model.Teacher" />
<typeAlias alias="Student" type="com.autohome.model.Student" />
<typeAlias alias="Author" type="com.autohome.model.Author" />
<typeAlias alias="Blog" type="com.autohome.model.Blog" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=test"/>
<property name="username" value="sa"/>
<property name="password" value="0"/>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="mapper/Author.xml"/>
<mapper resource="mapper/User.xml"/>
<mapper resource="mapper/Student.xml"/>
</mappers>
</configuration>

创建Mapper.xml(Author.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.autohome.mapper.Author">
<resultMap id="authorResultMap" type="Author">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="blogs" ofType="Blog">
<id column="bid" property="id"/>
<result column="title" property="title"/>
<result column="category" property="category"/>
</collection>
</resultMap> <select id="getAuthorBlogsById" parameterType="int" resultMap="authorResultMap">
SELECT a.id,name,b.id bid,title,category FROM t_author a
LEFT JOIN t_blog b on a.id=b.author_id
WHERE a.id=#{id} </select>
</mapper>

单元测试

 @Test
public void getAuthorBlog(){
SqlSession sqlSession=null;
try{
sqlSession=sqlSessionFactory.openSession(); Author author = sqlSession.selectOne("com.autohome.mapper.Author.getAuthorBlogsById",1);
System.out.println("作者信息 id:"+author.getId()+",name:"+author.getName());
System.out.println("作者博客:");
for(Blog blog:author.getBlogs()){
System.out.println("id:"+blog.getId()+",title:"+blog.getTitle()+",category:"+blog.getCategory());
}
}catch(Exception e){
e.printStackTrace();
}finally {
sqlSession.close();
}
}

附单元测试截图

MyBatis从入门到放弃四:一对多关联查询的更多相关文章

  1. MyBatis从入门到放弃三:一对一关联查询

    前言 简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化. 那么mybatis实现一对一的关联查询则是使用association属性和resultM ...

  2. MyBatis:一对多关联查询

    MyBatis从入门到放弃四:一对多关联查询 前言 上篇学习了一对一关联查询,这篇我们学习一对多关联查询.一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collecti ...

  3. MyBatis基础入门《十四》ResultMap子元素(association )

    MyBatis基础入门<十四>ResultMap子元素(association ) 1. id: >> 一般对应数据库中改行的主键ID,设置此项可以提高Mybatis的性能 2 ...

  4. MyBatis初级实战之六:一对多关联查询

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  5. 7.mybatis一对多关联查询

    和第5节一对一查询类似,但是不同的是,一对一使用的是association,而一对多使用collection. 实例: 1个班级Class,对应1个老师Teacher,对应多个学生Student 1. ...

  6. MyBatis关联查询,一对多关联查询

    实体关系图,一个国家对应多个城市 一对多关联查询可用三种方式实现: 单步查询,利用collection标签为级联属性赋值: 分步查询: 利用association标签进行分步查询: 利用collect ...

  7. mybatis一对多关联查询+pagehelper->分页错误

    mybatis一对多关联查询+pagehelper->分页错误. 现象: 网上其他人遇到的类似问题:https://segmentfault.com/q/1010000009692585 解决: ...

  8. mybatis collection 一对多关联查询,单边分页的问题总结!

    若想直接通过sql实现多级关联查询表结构得有2 个必不可少的字段:id ,parentId,levelId id:主键id, parentId:父id levelId:表示第几级(表本身关联查询的时候 ...

  9. Spring Boot入门系列(十七)整合Mybatis,创建自定义mapper 实现多表关联查询!

    之前讲了Springboot整合Mybatis,介绍了如何自动生成pojo实体类.mapper类和对应的mapper.xml 文件,并实现最基本的增删改查功能.mybatis 插件自动生成的mappe ...

随机推荐

  1. wukong搜索引擎源码解读

    转自:https://ayende.com/blog/171745/code-reading-wukong-full-text-search-engine I like reading code, a ...

  2. 按ctrl + c 播放下一曲音乐

    ./a.out . #include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<dirent. ...

  3. MongoDB复制集环境搭建

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://suifu.blog.51cto.com/9167728/1853478 环境介绍 ...

  4. Alamofire 4.0 迁移指南

    Alamofire 4.0 是 Alamofire 最新的一个大版本更新, 一个基于 Swift 的 iOS, tvOS, macOS, watchOS 的 HTTP 网络库. 作为一个大版本更新, ...

  5. js如何准确获取当前页面url网址信息

    1.window.location.href(设置或获取整个 URL 为字符串) var test = window.location.href;alert(test);返回:http://i.cnb ...

  6. 第6组UI组件:ViewAnimator及其子类

    ViewAnimator是一个基类,它继承了FrameLayout,因此它表现出FrameLayout的特征,可以将多个View组件“叠”在一起.ViewAnimator额外增加的功能正如它的名字所暗 ...

  7. 【puthon基础】之str类字符串

    str类字符串是不可变对象 1.创建字符串 s1 = str() #创建一个空字符串 s2 = str("hello") #创建字符串"hello" 2.处理字 ...

  8. Swiper --移动端触摸滑动插件

    Swiper使用方法 1.首先加载插件,需要用到的文件有swiper.min.js和swiper.min.css文件. <!DOCTYPE html> <html> <h ...

  9. LPC4370使用学习:GPIO的引脚功能使用,和12864OLED模拟I2C驱动

    一: 手中有块LPC4370的开发板,因为便宜,所以引脚引出的不多,而且只有基本的底板资源驱动代码和例程. 看着手册和例程看了老半天,写程序写了半天,结果GPIO老是驱动不起来,因为引脚配置寄存器中有 ...

  10. 在点击div中的p时,如何阻止事件冒泡?

    今天整理笔记,发现在学习javaScript的过程中,遇到过一个在当时看来很棘手的问题,现在特地总结一下,也希望能帮助到曾像我一样迷惘的初学者. 我还是以一个案例来说明问题,html代码如下: < ...