前言

上篇学习了一对一关联查询,这篇我们学习一对多关联查询。一对多关联查询关键点则依然是配置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. mysql存储过程中in、out、inout参数使用实际案例

    1.参数in的使用(代表输入,意思说你的参数要传到存过过程的过程里面去)//为了避免存储过程中分号(";")结束语句,我们使用分隔符告诉mysql解释器,该段命令是否已经结束了./ ...

  2. OC类方法的调用

    有个Person类,有个Phone类,Person类想使用Phone类中打电话和发短信的方法 1.Phone.h         Phone有kind和color属性  ,方法定义的时候将用到的参数都 ...

  3. endnote X7 加入文献

    endnote可以管理文献,并且在word中方便的添加参考文献. 1.加入文献: 2.导入以后可以创建自己的group,然后把导入的参考文献拖到group里,这样方便在插入参考文献的时候用group名 ...

  4. js判断MAC地址

    function white_mac_FormCheck(mac)    {           var temp = /[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0- ...

  5. leetcode[170]Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  6. P2P直播承载平台与CDN直播承载平台比较

    收看软件不一样:CDN直播收看无需安装第三方收看软件,一般操作系统已带播放器软件:P2P直播收看需要安装厂家自己的播放器软件,每家P2P的软件不兼容,收看者要装多套软件才能收看不同内容. 收看人数不一 ...

  7. SpringMVC+RestFul详细示例实战教程

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 <!--conf ...

  8. [Machine Learning] 深度学习中消失的梯度

    好久没有更新blog了,最近抽时间看了Nielsen的<Neural Networks and Deep Learning>感觉小有收获,分享给大家. 了解深度学习的同学可能知道,目前深度 ...

  9. Vmware 中安装 Ubuntu Server (或者ubuntu 以文本界面登陆时) 分辨率无法全屏问题

    Vmware 中安装 Ubuntu Server/Ubuntu 分辨率,无法全屏问题 需要更改grub设置 在终端或者文本界面按下列步骤进行设置: 第一步: 输入命令 sudo vim /etc/de ...

  10. [CSS3] 学习笔记-HTML与CSS简单页面效果实例

    一个简单的首页的设计: html文件: <!doctype html> <html> <head> <meta charset="UTF-8&quo ...