mybatis的resultmap、association、collection使用

学习链接:添加链接描述
添加链接描述

resultType

得到的数据的一次性输出。java类型的全限定类型或者使用别名。sql执行完以后直接按照该指定对象输出。

resultMap

作用:对于一个表格创建关系。可以解决实体类属性和数据库字段不一致的问题;提供一对一、一对多、多对多的映射。
属性列表:

属性描述
id命名空间中的唯一标识
type类的完全限定名,或类别名
autoMapping为本结果映射开启或者关闭自动映射
extends继承其他resultMap属性

标签列表: < id property=“” column=“”>

标签描述
constructor用于在实例化类时,注入结果到构造方法中。idArg:id参数,标记出作为ID的结果可以帮助提高整体性能;arg:将被注入到构造方法的一个普通结果
id标记ID结果
result注入到实体类属性的普通结果
association复杂类型关联,用于一对一的关联
collection复杂类型集合,用于一对多的关联
discriminator使用结果值决定使用哪个resultMap

id和result标签参数:

属性描述
property映射到实体类的属性。如果实体类有该属性,则使用;否则mybatis会寻找给定名称的字段
column映射到数据库中的字段名。
javaTypejava类的全限定名,或者类型别名。如果映射到Map时,要指定javaType值为Map
jdbcTypeJDBC类型,数据库中的字段类型。
typeHandler默认的类型处理器。

association

常用标签:

属性描述
property映射到实体类的属性。如果实体类有该属性,则使用;否则mybatis会寻找给定名称的字段
javatypejava类的全限定名,或者类型别名。如果映射到Map时,要指定javaType值为Map
resultMap引入一个resultMap的映射对象

例子:该标签常用于一对一。一个学生的一门课程->对应的分数

<resultMap id="chooseCourseMap" type="com.ztest.Student">
        <id property="stu_id" column="stu_id"></id>
        <result property="stu_name" column="stu_name"></result>
        <association property="chooseCourseList" javaType="com.ztest.ChooseCourse">
            <id property="stu_id" column="stu_id"></id>
            <id property="c_id" column="c_id"></id>
            <result property="genre" column="genre"></result>
            <result property="credit" column="credit"></result>
            <result property="score" column="score"></result>
        </association>
 </resultMap>

collection

常用标签:

属性描述
property映射到实体类的属性。如果实体类有该属性,则使用;否则mybatis会寻找给定名称的字段
javatypejava类的全限定名,或者类型别名。如果映射到Map时,要指定javaType值为Map
ofType指定一对多集合存放的实体类类型
resultMap引入一个resultMap的映射对象

例子:常用与一对多。

 <resultMap id="chooseCourseMap" type="com.ztest.Student">
        <id property="stu_id" column="stu_id"></id>
        <result property="stu_name" column="stu_name"></result>
        <!--  一对多      -->
        <collection property="chooseCourseList" ofType="com.ztest.ChooseCourse" javaType="List">
            <id property="stu_id" column="stu_id"></id>
            <id property="c_id" column="c_id"></id>
            <result property="genre" column="genre"></result>
            <result property="credit" column="credit"></result>
            <result property="score" column="score"></result>
        </collection>

    </resultMap>

多对多

例子:
在这里插入图片描述
z_course:课程表
z_student:学生表
z_choosecourse:表示学生表和课程表的中间表,即学生所选课程的分数以及对应学分等。
方法一:
在student实体类中加入:
在这里插入图片描述

 <resultMap id="chooseCourseMap" type="com.ztest.Student">
        <id property="stu_id" column="stu_id"></id>
        <result property="stu_name" column="stu_name"></result>
        <!--  一对多      -->
        <collection property="courseList" ofType="com.ztest.Course" javaType="List">
           <id property="c_id" column="c_id" />
           <result property="course_name" column="course_name" />
       </collection>

        <!--  一对多      -->
        <collection property="chooseCourseList" ofType="com.ztest.ChooseCourse" javaType="List">
           <id property="stu_id" column="stu_id"></id>
           <id property="c_id" column="c_id"></id>
           <result property="genre" column="genre"></result>
           <result property="credit" column="credit"></result>
           <result property="score" column="score"></result>
     	</collection>
    </resultMap>

返回结果:

 "result": [
        {
            "chooseCourseList": [
                {
                    "c_id": 5,
                    "credit": "4",
                    "genre": "主修",
                    "score": 98,
                    "stu_id": 14
                },
                {
                    "c_id": 6,
                    "credit": "4",
                    "genre": "主修",
                    "score": 80,
                    "stu_id": 14
                }
            ],
            "course": [{
                        "c_id": 5,
                        "course_name": "math"
                    },
                    {
                    	"c_id": 6,
                        "course_name": "yuwen"
                    }
             ],
            "stu_id": 14,
            "stu_name": "xz"
        }
    ]

方法二:
在这里插入图片描述
在中间表加入了两个对应实体类属性;
在course表中加入了映射:
在这里插入图片描述
student表和方法一中的一样。
映射实现如下:

    <resultMap id="chooseCourseMap" type="com.stec.masterdata.entity.promis.ztest.Student">
        <id property="stu_id" column="stu_id"></id>
        <result property="stu_name" column="stu_name"></result>
        <!--  一对多      -->
        <collection property="chooseCourseList" resultMap="chooseMap" />
    </resultMap>
    <resultMap type="com.stec.masterdata.entity.promis.ztest.ChooseCourse" id="chooseMap">
        <id property="stu_id" column="stu_id"></id>
        <id property="c_id" column="c_id"></id>
        <result property="genre" column="genre"></result>
        <result property="credit" column="credit"></result>
        <result property="score" column="score"></result>
        <!--   一对一         -->
        <association property="course" javaType="com.stec.masterdata.entity.promis.ztest.Course">
            <id property="c_id" column="c_id" />
            <result property="course_name" column="course_name" />
        </association>
    </resultMap>

最后得到三层嵌套结果:

"result": [
        {
            "chooseCourseList": [
                {
                    "c_id": 5,
                    "course": {
                        "c_id": 5,
                        "course_name": "math"
                    },
                    "credit": "4",
                    "genre": "主修",
                    "score": 98,
                    "stu_id": 14
                },
                {
                    "c_id": 6,
                    "course": {
                        "c_id": 6,
                        "course_name": "yuwen"
                    },
                    "credit": "4",
                    "genre": "主修",
                    "score": 80,
                    "stu_id": 14
                }
            ],
            "stu_id": 14,
            "stu_name": "xz"
        }
    ]

关键点在于使用了collection中的resultMap属性。

jdbcType和javaType对照表

在这里插入图片描述
来源:添加链接描述
添加链接描述

补充说明:
mysql中的jdbcType blob和Longtext字段类型区别:添加链接描述

  1. blob(binary large object)是二进制大对象,可以容纳可变量数量的数据,其中blob分为四种类型:TinyBlob、blob、mediumBlob和LongBlob。
    text也分为四种类型:tinyText,Text,MediumText,LongText。

  2. blob被视为二进制字符串,text被视为非二进制字符串;
    blob没有字符集,并且排序和比较基于列值字节的数值值。
    在这里插入图片描述

    text有字符集,按照字符集的校对规则对值进行排序和比较。
    在这里插入图片描述
    blob加入不了值。
    在这里插入图片描述

  3. blob和text不能有默认值。
    当保存或检索BLOB和TEXT列的值时不删除尾部空格。

  4. text只能存储纯文本,长度不足时扩展;blob除了存储文本外,由于其二进制存储格式,所以还可以保存图片。

varchar和varbinary的区别:添加链接描述

  1. varchar是可变长度二进制字符类型,如果对应的数据库排序规则是utf8_general_ci,查询不分大小写;排序规则为utf8_bin,区分大小写。varbinary是可变长度二进制字符类型,utf8_general_ci可以区分大小写。
  2. char(n)存储非二进制字符串,插入时,对于少于n字符在尾部加空格,查询时,丢弃尾部空格;
    varchar(n)存储非二进制字符串,插入时,对于少于n字符在尾部加空格,查询时,丢弃尾部空格;
    添加链接描述在这里插入图片描述

mysql不支持full join,oracle支持,想实现full join解决方案
select * from t1 left join t2 on t1.id = t2.id

union

select * from t1 right join t2 on t1.id = t2.id

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中,ResultMap是用来映射查询结果到Java对象的工具。AssociationResultMap中的一个标签,用于描述一个一对一的关联关系。 在ResultMap使用Association标签可以定义一个复杂类型的关联关系,它可以将查询结果中的某些字段映射到一个子对象中。Association标签需要设置property属性来指定Java对象中对应的属性名,column属性来指定查询结果中对应的列名。 使用Association标签的示例代码如下所示: ``` <resultMap id="orderResultMap" type="Order"> <id property="id" column="order_id" /> <result property="orderNo" column="order_no" /> <association property="customer" javaType="Customer"> <id property="customerId" column="customer_id" /> <result property="customerName" column="customer_name" /> <result property="customerAddress" column="customer_address" /> </association> </resultMap> ``` 在这个示例中,我们定义了一个名为orderResultMapResultMap,映射的Java对象类型是Order。在Order对象中,有一个名为customer的属性,它是一个Customer类型的对象。使用Association标签,我们定义了customer属性的映射关系,将查询结果中的customer_id映射到Customer对象的customerId属性,将customer_name映射到customerName属性,将customer_address映射到customerAddress属性。 这样,在查询结果中如果有对应的关联数据,MyBatis就会自动将查询结果映射到Java对象的关联属性中。 希望这个解释能帮到你,如果有需要进一步了解的话可以收藏起来哦!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MyBatisResultMap的associationcollection标签详解(图文例子)](https://blog.csdn.net/qq_52423918/article/details/120828850)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [mybatisresultmap、associationcollection使用](https://blog.csdn.net/weixin_44236424/article/details/125765174)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值