demo:
实体:Position

@TableName("tb_position")
public class Position {private static final long serialVersionUID = 1L;@TableId(value = "id", type = IdType.AUTO)@ApiModelProperty(value = "主键id")private int id;@ApiModelProperty(value = "名称")private String tPosName;@ApiModelProperty(value = "名称")private List<Teacher> teachers;@Overridepublic String toString() {return "Position{" +"id='" + id + '\'' +", tPosName='" + tPosName + '\'' +
//          ", teachers=" + teachers +'}';}
}

实体:Teacher

@TableName("tb_teacher")
public class Teacher {private static final long serialVersionUID = 1L;@TableId(value = "id", type = IdType.AUTO)private String id;private String tNo;private String tName;private String positionId;}

mapper: 一个职位可以有多个老师

第一种写法

    <!-- 通用查询映射结果 --><resultMap id="productResultMap" type="org.springblade.product.entity.Position"><result column="id" property="id"/><result column="tPosName" property="tPosName"/><collection property="teachers" javaType="java.util.ArrayList" ofType="org.springblade.product.entity.Teacher"><result column="id" property="id"/><result column="tNo" property="tNo"/><result column="tName" property="tName"/></collection></resultMap><select id="query" resultMap="productResultMap"   resultType="org.springblade.product.entity.Position">select t_pos_name as tPosName,t.t_name as tName,t.t_no as tNo from  tb_position pleft join tb_teacher t on p.id=t.position_id where p.id=#{id}and t.t_name = #{tPosName}</select>

controller:

第二种写法

    <resultMap id="resultMap" type="org.springblade.product.entity.Position"><result column="id" property="id"/><result column="tPosName" property="tPosName"/><collection property="teachers" javaType="java.util.List" ofType="org.springblade.product.entity.Teacher"select="selectOne"column="{sonId=id,tPosName=tPosName}"></collection></resultMap>

需查询出子查询的id。column传递参数,sonId表示子查询的参数,id父查询的值

 <select id="query1" resultMap="resultMap"  resultType="org.springblade.product.entity.Position">select id as id, t_pos_name as tPosName from  tb_position where id=#{id}</select><select id="selectOne"  resultType="org.springblade.product.entity.Teacher" >select t_name as tName from tb_teacher where position_id=#{sonId}</select>

特别注意:子查询的条件,在夫查询一定要查询出来,不然子查询一直不会执行
数据库
tb_teacher

CREATE TABLE `tb_teacher`  (`id` int(11) NOT NULL AUTO_INCREMENT,`t_no` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`t_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`position_id` int(11) NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
INSERT INTO `tb_teacher` VALUES (1, '163314001', '张文远', 1);
INSERT INTO `tb_teacher` VALUES (2, '163314002', '赵传智', 1);
INSERT INTO `tb_teacher` VALUES (3, '163314003', '风清扬', 2);
INSERT INTO `tb_teacher` VALUES (4, '163314004', '王汇智', 2);
INSERT INTO `tb_teacher` VALUES (5, '163314005', '汪思远', 3);


tb_position

CREATE TABLE `tb_position`  (`id` int(11) NOT NULL AUTO_INCREMENT,`t_pos_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
INSERT INTO `tb_position` VALUES (1, '教授');
INSERT INTO `tb_position` VALUES (2, '副教授');
INSERT INTO `tb_position` VALUES (3, '讲师');

两种方式的比较
方式一:只需要执行一次sql查询, 主表分页查询不正确。
方式二:代码复用性高, 主表分页查询正确。
方式一比方式二的效率要高,但是在使用的时候,方案一的代码可重用性要高

MyBatis中的collection使用方法相关推荐

  1. Mybatis中的collection、association来处理结果映射

    前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:) 标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到实体 ...

  2. MyBatis中特殊符号处理方法汇总

    mybatis框架是java web开发必备的框架,很多mybatis新手写代码的时候,需要在sql中使用到大于等于,这时候如果像sql中一样直接使用了>.<.>=.<=,在运 ...

  3. Mybatis中的collection和association一关系

    collection 一对多和association的多对一关系 学生和班级的一对多的例子 班级类: package com.glj.pojo;import java.io.Serializable; ...

  4. Mybatis中mapper接口里方法重载的实现

    看了网上的很多文章,说mapper接口里不能写重载方法,感觉这种说法不对,mapper接口是可以实现重载方法的. 实现方法 例如: package mapper;import pojo.User;im ...

  5. mybatis 中 foreach collection的三种用法

    oreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. i ...

  6. mybatis中,collection配置后查询只显示一条记录

    描述一下问题: 已知有两个表,一个是user表,一个是address,一(user)对多(address)的关系,在user的实体类里面写属性: private List<Address> ...

  7. Mybatis中Collection集合标签的使用

    mybatis简单的CURD就不用多说了,网上相关博客文档一大堆.分析一下Mybatis里面的collection聚集查询.  假设一个班级有多名学生为例,通过班级号查询出该班级的信息,和班级里面的所 ...

  8. Mybatis中使用循环遍历

    Mybatis中传参数的方法 1.使用注解绑定,mapper.xml 对应方法 不需要指定 parameterType,(推荐使用注解绑定方式) 方法接口: List<CalculateIdea ...

  9. mysql触发器可以使用正则表达式_SQL 正则表达式及mybatis中使用正则表达式

    这篇文章主要介绍了SQL 正则表达式及mybatis中使用正则表达式的方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下 mysql 提供的模式匹配的其他类型是使用扩展正则表达式. 当你对这 ...

最新文章

  1. myeclipse使用maven整合ssh配置
  2. Android深度探索(卷1)HAL与驱动开发阅读心得4
  3. android缓存之Lrucache 和LinkedHashMap
  4. SAP HANA云平台YaaS,你不知道的三件事
  5. H3C 无线AP升级步骤
  6. 用java做一个模拟彩票程序_JAVA模拟----- 彩票机子-----抽奖过程的实例化
  7. CCIE-LAB-第十六篇-NAT+OSPF下发默认路由+校验配置(模块一结束篇章)
  8. Complex类与运算符重载
  9. Linux SD卡建立两个分区
  10. 数据分析/数据挖掘/机器学习---- 必读书目
  11. python创意动态图片_Python趣味创意编程
  12. AutoCAD全面卸载的方法
  13. 灵雀云CTO陈恺:从“鸿沟理论”看云原生,哪些技术能够跨越鸿沟?
  14. 超阅科技001:冰火九重天
  15. html5采集手写签名,前端canvas手写签名(含移动端)
  16. 十五款固态硬盘收获季节展示
  17. 企业级权限系统架构设计 (v 2.0)
  18. springboot+vue企业人事人力资源管理系统java公司员工出差考勤办公OA系统
  19. python程序编译错误_Python编译错误集锦
  20. Python函数绘图

热门文章

  1. 美股1周2次熔断,全球已在经济危机边缘,3周跌掉中国1年GDP
  2. 投石问路 IBM于12月出货GPU刀片服务器(1) - 51CTO.COM
  3. close() was never explicitly called on database 和 database is locked 错误原因
  4. java 二嗨租车项目_java实现简单租车系统
  5. 在线Excel转HtmlTable工具
  6. 如何用地道流利的英语写好一份求职信
  7. 极复杂编码,下载《原神》角色高清图、中日无损配音,爬虫 16 / 120 例
  8. 提起薛定谔,除了“猫”你还能想到啥?
  9. 720不能建立远程计算机连接,上网拨号提示“错误720:不能建立到远程计算机的连接”的解决方法...
  10. 51单片机8*8点阵显示“中国”