MyBatis之ResultMap的association和collection

发布于:2024-10-15 ⋅ 阅读:(133) ⋅ 点赞:(0)

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">
    SELECT
        type.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_num
    FROM
        serve
            inner JOIN serve_item AS item ON item.id = serve.serve_item_id
            inner JOIN serve_type AS type ON type.id = item.serve_type_id
    WHERE
        serve.region_id = #{regionId}
      AND serve.sale_status = 2
    ORDER BY
        type.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


网站公告

今日签到

点亮在社区的每一天
去签到