qq_34556596 2023-02-24 09:39 采纳率: 33.3%
浏览 142
已结题

springboot框架下如何循环数据库id进行更新数据库

springboot框架下如何循环数据库id进行更新数据库。
目前数据库的字段有id、labName、startTime、endTime、useTime,useTime=endTime-startTime。
按照目前已有代码,在Postman中测试接口,每次输入一个id时就可以计算出一个useTime。
目前有两个问题:
1、如果已知startTime和endTime,怎样循环计算更新整个数据表的useTime,即xml里面的id=#{id}这个id需要循环整个数据表。
2、怎样把对应labName的下一行的startTime赋值到上一行的endTime,也就是筛选出labName,id号不一定连续,把下一行的startTime赋值到上一行的endTime。
目前已有代码如下:
controller部分:


package com.example.time.controller;

import com.example.time.entity.Time;
import com.example.time.mapper.TimeMapper;
import com.example.time.service.TimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/time")
public class TimeController {
    @Autowired
    private TimeMapper timeMapper;

    @Resource
    private TimeService timeService;

    @PostMapping("/updateUseTime")
    public Integer updateUseTime(@RequestBody Time time){
        return timeService.updateUseTime(time);
    }
}

entity部分:

package com.example.time.entity;

import lombok.Data;

import java.time.LocalDateTime;
import java.util.Date;

@Data
public class Time {
    private Integer id;
    private String labName;
    private Date startTime;
    private Date endTime;
    private String useTime;

mapper部分:

package com.example.time.mapper;

import com.example.time.entity.Time;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface TimeMapper {
    int updateUseTime(Time time);
}

xml部分:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.time.mapper.TimeMapper">
    <update id="updateUseTime">
        update table set useTime=(select datediff(minute,startTime,endTime) from table where id=#{id})
        <where>
            id=#{id}
        </where>
    </update>
</mapper>

service部分:

package com.example.time.service;

import com.example.time.entity.Time;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface TimeService {
    int updateUseTime(Time time);
}

Ipml部分:

package com.example.time.service.impl;

import com.example.time.entity.Time;
import com.example.time.mapper.TimeMapper;
import com.example.time.service.TimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务层实现类
 */

@Service
public class TimeServiceImpl implements TimeService{

    @Autowired
    private TimeMapper timeMapper;

    @Transactional
    @Override
    public int updateUseTime(Time time) {
        return timeMapper.updateUseTime(time);
    }
}
  • 写回答

4条回答 默认 最新

  • 社区专家-Monster-XH 2023-02-24 09:47
    关注

    基于Monster 组和GPT的调写:

    问题2:
    1.新增Mapper接口方法
    在TimeMapper中新增一个方法来更新endTime

    
    int updateEndTimeByLabName(String labName);
    
    

    2.在xml文件中添加SQL语句
    在mapper对应的xml文件中,添加一个update语句,通过labName找到需要更新的行,将下一行的startTime赋值给当前行的endTime,代码如下:

    <update id="updateEndTimeByLabName">
        update table set endTime = (
            select startTime
            from table t2
            where t2.labName = #{labName}
                and t2.startTime > table.startTime
            order by t2.startTime asc
            limit 1
        )
        where labName = #{labName};
    </update>
    
    
    

    3.在Service中添加方法
    在TimeService接口中添加新的方法声明:

    int updateEndTimeByLabName(String labName);
    
    
    

    在TimeServiceImpl中实现该方法:

    @Transactional
    @Override
    public int updateEndTimeByLabName(String labName) {
        return timeMapper.updateEndTimeByLabName(labName);
    }
    
    
    

    4.在Controller中添加接口
    在TimeController中添加以下接口,用于调用Service中的updateEndTimeByLabName方法:

    @PutMapping("/updateEndTimeByLabName/{labName}")
    public Integer updateEndTimeByLabName(@PathVariable String labName) {
        return timeService.updateEndTimeByLabName(labName);
    }
    
    
    

    5.在Postman中测试接口
    在Postman中使用PUT方法访问 http://localhost:8080/time/updateEndTimeByLabName/%7BlabName} 接口,其中{labName}为需要更新的实验室名称。
    基本就完成了,又问题再咨询!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月4日
  • 已采纳回答 2月24日
  • 赞助了问题酬金50元 2月24日
  • 创建了问题 2月24日

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因