前言

上篇学习了一对一关联查询,这篇我们学习一对多关联查询。一对多关联查询关键点则依然是配置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. Hdu-2112 HDU Today (单源多点最短路——Dijsktra算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给你N个公交车站,起点,终点,各站之间的距离,求起点到终点之间的最短距离.(起点终点相 ...

  2. --@angularJS--综合小实例1

    <!DOCTYPE HTML><html ng-app="myapp"><head> <title>综合小实例</title& ...

  3. Html在网页、页面中放置Swf、Flash 背景

    Html 在网页.页面中放置Swf.Flash背景: <embed src="image/index.swf" wmode=transparent style="p ...

  4. Objective-C Effective 技巧

    1.除非有必要,否则不要引用头文件,一般来说应该利用@class使用前向声明,并在实现中引用头文件:如果实在无法使用,比如要声明某个类遵循一项协议,这种情况下,尽量把这条声明移到分类中,如果不行的话, ...

  5. Canvas createRadialGradient API

    Canvas createRadialGradient API <!DOCTYPE html> <html lang="en"> <head> ...

  6. html标签大全(1)

     http标签详解及讲解        1.基础标签 <!DOCTYPE html> <!--表示文本类型--> <html> <!--<html> ...

  7. 【.net 深呼吸】细说CodeDom(10):生成异常处理语句

    写完这一篇,大概可以准备过年了,就算是这系列文章的收尾吧. 异常处理语句,就是常说的try...catch语句,有时候,也会带有finally子句.要生成异常处理语句,得用到CodeTryCatchF ...

  8. 开源第三方登录组件OAuthLogin2.0 支持QQ,阿里巴巴,淘宝,京东,蘑菇街,有赞等平台

    Nuget地址:https://www.nuget.org/packages/OAuthLogin2.0/ 项目结构说明: AuthorizationProviders文件夹下主要存放内置的授权平台. ...

  9. css,html性能优化

    css性能优化 CSS是负责布局和渲染的重要角色,漂亮的页面当然能够吸引用户.本文是自己在开发过程中总结的关于CSS与性能的关系,可能有不对之处,希望能够指出. ? 1.所有的样式尽量放在css文件中 ...

  10. [CSS3] 学习笔记-选择器详解(二)

    1.选择器first-child.last-child.nth-child和nth-last-child 利用first-child.last-child.nth-child和nth-last-chi ...