解决:mybatis+MySQL一对多数据查询翻倍

同事帮忙解决(大牛)

1对多:
a表数据对b表多条(a_id)
a表数据对c表多条(a_id)
关联字段都是(a_id)做的比喻

代码:

XML

            <resultMap>
        <collection property="deviceParamsList"
                    javaType="java.util.List"
                    ofType="com.jc.local.entity.DeviceParam"
                    select="com.jc.local.mapper.DeviceMapper.selectOutput"
                    注意看这里<select>这个标签路径,写道当前的Maaper.java路径,如果在其他Mapper中写这个接口方法,会映射不到,服务报错。
                    column="number">
              <column>这个标签:《需要给一个参数》也就是关联条件的a_id
        </collection>
      
        <collection property="deviceOutputList"
                    javaType="java.util.List"
                    ofType="com.jc.local.entity.DeviceOutput"
                    select="com.jc.local.mapper.DeviceMapper.selectParam"
                    column="number">
        </collection>
            </resultMap>

    <select id="numberJoinOutPutJoinParamList" resultMap="BaseResultMapNumber" parameterType="java.lang.String" >
    //结果类型时封装好的那个Map的id="名称”
    拿到这个结果集进行查询
        SELECT
            *
        FROM
            device d
        where d.NUMBER = #{number}
    </select>

    <select id="selectParam" parameterType="java.lang.String" resultType="com.jc.local.entity.DeviceParam" >
    //结果类型时B表的实体路径
        select * from device_param where DEVICE_NUM = #{number}
    </select>
    
    <select id="selectOutput" parameterType="java.lang.String" resultType="com.jc.local.entity.DeviceOutput">
    //结果类型时C表实体路径
        select * from device_output where DEVICE_NUM = #{number}
    </select>

mapper.java

package com.jc.local.mapper;
接口路径:com.jc.local.mapper.DeviceMapper
接口名:DeviceMapper
	//a关联B也关联c查询
    List<Device> numberJoinOutPutJoinParamList(String number);
    List<DeviceOutput> selectOutput(String number);
    List<DeviceParam> selectParam(String number);

controller.java

返回值给List<>就行

总结:
之前的sql关联两个子表,代码走到第一个子表的时候就会生成一个结果集。然后用第一个生成的结果集,去映射关联第二个那个子表a_id(形成翻倍)

SELECT
 	d.* 
FROM
	device d
关联第一个子表	LEFT JOIN device_param p ON d.NUMBER = p.DEVICE_NUM
关联第二个子表	LEFT JOIN device_output o ON d.NUMBER = o.DEVICE_NUM 
WHERE
	d.NUMBER = 1

编写实属不易,点赞,留言,
查询数据翻倍完美解决!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,针对你的问题,我可以给出以下解决方案: 1. 首先,需要在新的数据库中建立与源数据库相同结构的,可以使用 Navicat 等工具进行快速同步数据库结构。 2. 接着,需要在 SpringBoot 项目中配置两个数据源,一个是源数据库的数据源,另一个是目标数据库的数据源。可以使用 SpringBoot 自带的多数据源配置方式,或者使用第三方库,比如 Druid。 3. 然后,需要编写数据同步的逻辑,可以使用 MyBatis 读取源数据库的数据,然后使用 MyBatis 写入到目标数据库中。具体实现可以参考以下代码: ```java @Service public class DataSyncService { @Autowired private DataSource sourceDataSource; @Autowired private DataSource targetDataSource; @Autowired private SqlSessionFactory sourceSqlSessionFactory; @Autowired private SqlSessionFactory targetSqlSessionFactory; public void syncData() { // 从源数据库读取数据 SqlSession sourceSession = sourceSqlSessionFactory.openSession(); List<SourceData> sourceDataList = sourceSession.selectList("sourceMapper.selectData"); sourceSession.close(); // 写入目标数据库 SqlSession targetSession = targetSqlSessionFactory.openSession(); TargetMapper targetMapper = targetSession.getMapper(TargetMapper.class); for (SourceData sourceData : sourceDataList) { TargetData targetData = convertToTargetData(sourceData); targetMapper.insertData(targetData); } targetSession.commit(); targetSession.close(); } private TargetData convertToTargetData(SourceData sourceData) { // 实现数据转换逻辑 TargetData targetData = new TargetData(); targetData.setId(sourceData.getId()); targetData.setName(sourceData.getName()); // ... return targetData; } } ``` 需要注意的是,建议使用批量插入的方式,可以大大提高数据同步的效率。 4. 最后,可以使用定时任务等方式定时调用数据同步的逻辑,保证数据的实时同步。 以上就是一个简单的基于 SpringBoot、MyBatisMySQL 实现数据数据同步的方案,希望对你有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SteveCode.

永远年轻,永远热泪盈眶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值