添加事务@Transactional导致整个controller映射路径404

余生颓废 提交于 2019-12-28 14:33:24

记一次代码不规范导致的问题,那时图省事,直接加@Transactional加在控制层,使用dao进行数据保存,没使用service。

  • 问题原因:@Transactional加在控制层
package com.ms.base.controller;

import com.ms.base.dao.WaterMessageDetailMapper;
import com.ms.base.domain.WaterMessageDetail;
import com.ms.base.service.WaterMessageDetailService;
import com.ms.base.utils.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/v6/importWateQualityData")
public class ImportWaterQualityDataController {

    @Resource
    private WaterMessageDetailMapper waterMessageDetailMapper;
    @Autowired
    private WaterMessageDetailService waterMessageDetailService;

    @Value("${import.startLine}")
    private int startLine;

    @Value("${import.endLine}")
    private int endLine;

    @Value("${import.fileUrl}")
    private String fileUrl;

    @Value("${import.stationId}")
    private String stationId;

    @Value("${import.stationMnCode}")
    private String stationMnCode;

    /**
     * 导入水质监测站数据
     * @return
     */
    @GetMapping("/import")
    @Transactional
    public void importWaterData() throws IOException {
        File file = new File(fileUrl);
        //读取本地的文件
        log.info("开始执行");
        InputStream in = new FileInputStream(file);
        Workbook wb = null;
        if (ExcelUtil.isExcel2007(file.getName())) {
            wb = new XSSFWorkbook(in);
        } else {
            wb = new HSSFWorkbook(in);
        }
        Sheet sheetAt = wb.getSheetAt(0);
        doImport(sheetAt);
    }

    private void doImport(Sheet sheetAt) {
        //获取所有行
        for (int i = startLine; i < endLine; i++) {
            log.info("---------------行号={}--------------",i+1);
            Row row = sheetAt.getRow(i);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            //时间
            Cell cell1 = row.getCell(1);
            cell1.setCellType(CellType.NUMERIC);
            Double dataValue = cell1.getNumericCellValue();
            Date dataTime = HSSFDateUtil.getJavaDate(dataValue);
            log.info("时间={}",dataTime);

            //氨氮
            Cell cell2 = row.getCell(2);
            cell2.setCellType(CellType.STRING);
            String ammoniaNitrogenIndex = cell2.getStringCellValue();
            log.info("氨氮={}",ammoniaNitrogenIndex);
            insertInfo(simpleDateFormat, dataTime, ammoniaNitrogenIndex,"w21003-Avg");

            //CODCr
            Cell cell3 = row.getCell(3);
            cell3.setCellType(CellType.STRING);
            String codcrIndex = cell3.getStringCellValue();
            log.info("CODCr={}",codcrIndex);
            insertInfo(simpleDateFormat, dataTime, codcrIndex,"w01018-Avg");

            //溶解氧
            Cell cell4 = row.getCell(4);
            cell4.setCellType(CellType.STRING);
            String dissolvedOxygenIndex = cell4.getStringCellValue();
            log.info("溶解氧={}",dissolvedOxygenIndex);
            insertInfo(simpleDateFormat, dataTime, dissolvedOxygenIndex,"w01009-Avg");

            //高锰酸钾
            Cell cell5 = row.getCell(5);
            cell5.setCellType(CellType.STRING);
            String potassiumPermanganateIndex = cell5.getStringCellValue();
            log.info("高锰酸钾={}",potassiumPermanganateIndex);
            insertInfo(simpleDateFormat, dataTime, potassiumPermanganateIndex,"w01019-Avg");

            //总磷
            Cell cell6 = row.getCell(6);
            cell6.setCellType(CellType.STRING);
            String totalPhosphorusIndex = cell6.getStringCellValue();
            log.info("总磷={}",totalPhosphorusIndex);
            insertInfo(simpleDateFormat, dataTime, totalPhosphorusIndex,"w21011-Avg");
        }
    }

    private void insertInfo(SimpleDateFormat simpleDateFormat, Date dataTime, String indexValue, String indexType) {
        WaterMessageDetail waterMessageDetail = new WaterMessageDetail();
//            waterMessageDetail.setId(UUID.randomUUID().toString().replaceAll("-",""));
        //监测站id
        waterMessageDetail.setStationId(stationId);
        //创建时间
        waterMessageDetail.setCreateTime(new Date());
        //发送时间戳
        waterMessageDetail.setSendTime(String.valueOf(dataTime.getTime()));
        //数据状态
        waterMessageDetail.setDataStatus("2061");
        //数据时间戳
        String dateString = simpleDateFormat.format(dataTime);
        waterMessageDetail.setDataTime(dateString);
        //指标名(氨氮)
        waterMessageDetail.setIndexType(indexType);
        //指标值
        waterMessageDetail.setIndexValue(Double.valueOf(indexValue));
        //mn码
        waterMessageDetail.setMnCode(stationMnCode);
        //数据类型
        waterMessageDetail.setDataType("N");
        waterMessageDetailMapper.insertSelective(waterMessageDetail);
    }

    @GetMapping("/test")
    public String test(){
        return "test";
    }
}

在这里插入图片描述
解决方案

  • waterMessageDetailMapper.insertSelective(waterMessageDetail);修改为 waterMessageDetailService.insertSelective(waterMessageDetail);
  • 将@Transactional加入到serviceImpl
	@Transactional
    public int insertSelective(WaterMessageDetail record) {
        return waterMessageDetailMapper.insertSelective(record);
    }

在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!