mybatis一对一,一对多,字段重复

1、一对一,association和javaType

    <resultMap type="com.example.ProdTask" id="ProdTaskMapWithDInvChkTask">
        <result property="taskCateg" column="ProdTask_CATEG" jdbcType="VARCHAR"/>
        <result property="taskNo" column="ProdTaskTASK_NO" jdbcType="VARCHAR"/>
        <association property="taskData" javaType="com.example.dyc.myofbatis.DInvChkTask">
            <result property="taskId" column="TASK_ID" jdbcType="VARCHAR"/>
            <result property="taskNo" column="TASK_NO" jdbcType="VARCHAR"/>
        </association>
    </resultMap>

    <select id="queryByTaskNoWithDInvChkTask" resultMap="ProdTaskMapWithDInvChkTask">
        select
        <include refid="Base_Column_List_ProdTask"/>, <include refid="Base_Column_List_DInvChkTask"></include>
        from T_PROD_TASK ProdTask left join D_INV_CHK_TASK d on ProdTask.TASK_NO = d.TASK_NO
        where ProdTask.TASK_NO = #{taskNo}
    </select>

2、一对多,collection和ofType

    <resultMap type="com.example.ProdTask" id="ProdTaskMapWithDInvChkTask">
        <result property="taskCateg" column="ProdTask_CATEG" jdbcType="VARCHAR"/>
        <result property="taskNo" column="ProdTaskTASK_NO" jdbcType="VARCHAR"/>
        <collection property="taskData" ofType="com.example.dyc.myofbatis.DInvChkTask">
            <result property="taskId" column="TASK_ID" jdbcType="VARCHAR"/>
            <result property="taskNo" column="TASK_NO" jdbcType="VARCHAR"/>
        </collection >
    </resultMap>

3、字段名重复问题

可以采用取别名的方式,对应的resultmap中column需要取别名。

4、参数映射

都采用@Param(),避免出问题

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中实现一对多关系可以通过使用collection标签来实现。[2] 在映射文件中,可以使用resultMap标签定义一个结果集映射,通过在resultMap中使用collection标签来指定一对多关系的映射关系。 具体操作如下: 1. 在查询语句中,使用JOIN语句将主表和从表连接起来,以获取一对多关系的数据。在查询语句中使用别名,以便在映射文件中进行引用。 2. 在resultMap中,使用collection标签指定一对多关系的映射关系。在collection标签的ofType属性中指定从表对应的实体类。 3. 在collection标签内部,使用result标签来映射从表的字段到实体类的属性。 例如,下面是一个实现一对多关系的映射文件示例: ```xml <select id="getTeacher" resultMap="TeacherStudent"> select t.id tid, t.name tname, s.name sname, s.id sid from mybatis.teacher t, mybatis.student s where t.id = s.tid </select> <resultMap id="TeacherStudent" type="Teacher"> <result property="name" column="tname"/> <result property="id" column="tid"/> <collection property="students" ofType="Student"> <result property="name" column="sname"/> <result property="id" column="sid"/> <result property="tid" column="tid"/> </collection> </resultMap> ``` 在上面的示例中,查询语句通过连接teacher表和student表获取一对多关系的数据。resultMap中使用collection标签来指定students属性是一个集合类型,并使用result标签来映射student表的字段到Student实体类的属性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SSM框架的学习与应用JavaEE(第二天)Mybatis的深入学习](https://download.csdn.net/download/m0_53659738/88241458)[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* [Mybatis多表查询之一对多、多对一](https://blog.csdn.net/qq_53860947/article/details/123743724)[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、付费专栏及课程。

余额充值