当前位置: 首页 > news >正文

MyBatis之ResultMap的association和collection

association例子演示

  • 实体类演示
  • @Data
    //书籍
    public class Book {private String id;private String name;private String author;private Double price;private Integer del;private Date publishdate;private String info;//把出版社对象当作属性private Publisher pub;//------重点在这里一本书对应一个出版社,这是一个出版社对象
    }
    
    @Data
    //出版社
    public class Publisher {private String id;private String name;private String phone;private String address;
    }
    

    xml  type是实体类的路径  jdbctype 省略即可

  • 如果数据库中的字段类型和 Java 对象的属性类型一致,可以省略 jdbcType 属性。但是,在数据库和 Java 对象之间存在类型差异时,使用 jdbcType 属性来指定数据库字段的类型是非常必要的。

  • <resultMap id="rMap_book" type="com.wang.test.demo.entity.Book"><!-- 主键  property为实体类属性 column为数据库字段 jdbcType为实体类对应的jdbc类型--><id property="id" column="b_id" jdbcType="VARCHAR"></id><!-- 普通属性  property为实体类属性 column为数据库字段  jdbcType为实体类对应的jdbc类型--><result property="name" column="b_name" jdbcType="VARCHAR"></result><result property="author" column="author" jdbcType="VARCHAR"></result><result property="price" column="price" jdbcType="VARCHAR"></result><result property="del" column="del" jdbcType="NUMERIC"></result><result property="publisherid" column="publisher_id" jdbcType="VARCHAR"></result><result property="publishdate" column="publish_date" jdbcType="TIMESTAMP"></result><!--一对一映射association property 为实体类book中的属性名字 javaType为实体类属性的类型 --><association property="pub" javaType="com.wang.test.demo.entity.Publisher"><id property="id" column="p_id" jdbcType="VARCHAR"></id><result property="name" column="name" jdbcType="VARCHAR"></result><result property="phone" column="phone" jdbcType="VARCHAR"></result><result property="address" column="address" jdbcType="VARCHAR"></result></association>
    </resultMap>
    

collection例子

@Data
//班级类
public class Class {private String id;private String name;private List<Student> students;//----重点在这里,一个班级对应多个学生}@Data
public class Student {private int id;private String name;private int age;
}

 

<resultMap id="rMap_class" type="com.wang.test.demo.entity.Class"><id property="id" column="id" jdbcType="VARCHAR"></id><result property="name" column="name" jdbcType="VARCHAR"></result><!--一对多映射用这个  ofTyp是一对多的集合的所存放的实体类  javaType实体类的属性类型--><collection property="students" ofType="com.wang.test.demo.entity.Student" javaType="list"><id property="id" column="id" jdbcType="INTEGER"></id><result property="name" column="name" jdbcType="VARCHAR"></result><result property="age" column="age" jdbcType="INTEGER"></result></collection>
</resultMap>

 项目实战中的例子

<select id="findServeIconCategoryByRegionId" resultMap="ServeCategoryMap">SELECTtype.id as serve_type_id,type.name as serve_type_name,type.serve_type_icon,serve.city_code,serve.id as serve_id,item.id as serve_item_id,item.name as serve_item_name,item.serve_item_icon,item.sort_num as serve_item_sort_numFROMserveinner JOIN serve_item AS item ON item.id = serve.serve_item_idinner JOIN serve_type AS type ON type.id = item.serve_type_idWHEREserve.region_id = #{regionId}AND serve.sale_status = 2ORDER BYtype.sort_num,item.sort_num
</select><!--手动的映射-->
<resultMap id="ServeCategoryMap" type="com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO"><!--id映射主键字段--><id column="serve_type_id" property="serveTypeId"></id><!--result映射普通字段--><result column="serve_type_name" property="serveTypeName"></result><result column="serve_type_icon" property="serveTypeIcon"></result><result column="city_code" property="cityCode"></result><!--column 数据库中的字段名--><!--property 实体类中对应的属性 该关键字可以省略... --><!--ofType 是javaType中的单个对象类型--><collection property="serveResDTOList" ofType="com.jzo2o.foundations.model.dto.response.ServeSimpleResDTO"><id column="serve_id" property="id"></id><result column="serve_item_id" property="serveItemId"></result><result column="serve_item_name" property="serveItemName"></result><result column="serve_item_icon" property="serveItemIcon"></result><result column="serve_item_sort_num" property="serveItemSortNum"></result></collection>
</resultMap>

一种比较复杂的结果集映射 

ResultMap collection多层嵌套使用_一个resultmap中有多个collection-CSDN博客文章浏览阅读1.2w次,点赞14次,收藏60次。ResultMap collection多层嵌套使用ResultMap介绍在Mybatis使用中,ResultMap是最复杂的一种结构,也是功能最强大的结构之一。通过ResultMap能够将复杂的1对多的结果集映射到一个实体当中去,可以借助Mybatis来将复杂结构的数据对象映射到一个结果集中组装好。结构ResultMap有3个属性,如下:https://blog.csdn.net/qq_20377675/article/details/103396537?fromshare=blogdetail&sharetype=blogdetail&sharerId=103396537&sharerefer=PC&sharesource=2201_75600005&sharefrom=from_link


http://www.mrgr.cn/news/46258.html

相关文章:

  • AGI时代存内计算芯片的语音识别之旅 —— 知存科技开发板体验与感悟
  • 【代码随想录Day38】动态规划Part07
  • vue路由缓存问题
  • 【springboot入门之YAML使用】
  • 非刚性点云配准 Non-rigid registration of two surfaces.SHREC 14 Human 数据集
  • 一键从想法到上线:Bolt.new重新定义全栈开发流程
  • ubuntu22.04的wayland协议修改掉,因为很多软件不支持
  • [vue/no-use-v-if-with-v-for] v-for 和 v-if 在同一个元素中的处理方法
  • Java中System类和RunTime类的Api
  • HTML5实现古典音乐网站源码模板1
  • 洞察AI趋势:智享AI直播,打造专属你的数字化直播AIGC系统!
  • 【JavaScript】事件 - 实现元素拖拽至画布
  • linux 禁用ipv6
  • Nacos 与 Eureka、Zookeeper 和 Consul 等其他注册中心的区别
  • WEB安全该学习哪些知识
  • 11、论文阅读:无监督夜间图像增强:层分解与光效抑制的结合
  • Qt C++设计模式->中介者模式
  • 带你了解linux:学习第十二课 linux 之 sort
  • 抓包工具检测手把手教学 - 某招聘网站
  • 详情说明HTTP/2和HTTP/3两者间的区别