Commit a75d1380 authored by wujipeng's avatar wujipeng
Browse files

详情查询/根据路名查询数据

parent 6719173e
package com.example.pipelinenetwork.controller;
import com.example.pipelinenetwork.result.Result;
import com.example.pipelinenetwork.service.DefectService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
/**
* @Author: wujipeng
* @Description: TODO
* @Date: 2026/7/22 10:38
* @Version: 1.0
*/
@RestController
@RequestMapping("/defect")
public class DefectController {
@Resource
private DefectService defectService;
@PostMapping("/import")
public Result<Boolean> importDefectData(@RequestBody MultipartFile file) throws IOException {
return Result.success(defectService.importDefectData(file));
}
}
package com.example.pipelinenetwork.controller;
import com.example.pipelinenetwork.pojo.dto.LoginDTO;
import com.example.pipelinenetwork.result.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: wujipeng
* @Description: TODO
* @Date: 2026/7/22 10:10
* @Version: 1.0
*/
@RestController
@RequestMapping("/login")
public class LoginController {
@PostMapping("/in")
public Result<String> loginIn(@RequestBody LoginDTO loginDTO) {
if (!loginDTO.getUsername().equals("admin") || !loginDTO.getPassword().equals("123456")) {
return Result.error(401, "用户名或密码错误");
}
return Result.success("登录成功");
}
}
package com.example.pipelinenetwork.controller; package com.example.pipelinenetwork.controller;
import com.example.pipelinenetwork.pojo.dto.RoadDTO;
import com.example.pipelinenetwork.pojo.vo.RoadVO; import com.example.pipelinenetwork.pojo.vo.RoadVO;
import com.example.pipelinenetwork.result.Result; import com.example.pipelinenetwork.result.Result;
import com.example.pipelinenetwork.service.DefectService;
import com.example.pipelinenetwork.service.DetectionService;
import com.example.pipelinenetwork.service.RoadService; import com.example.pipelinenetwork.service.RoadService;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.Map;
@RestController @RestController
@RequestMapping("/road") @RequestMapping("/road")
...@@ -17,10 +17,6 @@ public class RoadController { ...@@ -17,10 +17,6 @@ public class RoadController {
@Resource @Resource
private RoadService roadService; private RoadService roadService;
@Resource
private DetectionService detectionService;
@Resource
private DefectService defectService;
@GetMapping("/match") @GetMapping("/match")
public Result<RoadVO> match( public Result<RoadVO> match(
...@@ -36,4 +32,22 @@ public class RoadController { ...@@ -36,4 +32,22 @@ public class RoadController {
public List<String> importRoadData(@RequestParam("file") MultipartFile file) throws Exception { public List<String> importRoadData(@RequestParam("file") MultipartFile file) throws Exception {
return roadService.importRoadData(file); return roadService.importRoadData(file);
} }
/**
* 详情查询
*/
@PostMapping("/detail")
public Result<Map<String,Integer>> roadDetail(@RequestBody RoadDTO roadDTO) {
return Result.success(roadService.roadDetail(roadDTO));
}
/**
* 根据路名查询数据
*/
@GetMapping("/road/detail")
public Result<RoadVO> queryByRoadName(@RequestBody RoadDTO roadDTO) {
return Result.success(roadService.queryByRoadName(roadDTO));
}
} }
\ No newline at end of file
package com.example.pipelinenetwork.mapper; package com.example.pipelinenetwork.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pipelinenetwork.pojo.entity.DetectionEntity; import com.example.pipelinenetwork.pojo.entity.BasicEntity;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/** /**
* @Author: wujipeng * @Author: wujipeng
...@@ -11,5 +14,8 @@ import org.apache.ibatis.annotations.Mapper; ...@@ -11,5 +14,8 @@ import org.apache.ibatis.annotations.Mapper;
* @Version: 1.0 * @Version: 1.0
*/ */
@Mapper @Mapper
public interface DetectionMapper extends BaseMapper<DetectionEntity> { public interface BasicMapper extends BaseMapper<BasicEntity> {
List<String> getRelIdsByRegion(@Param("region") String region);
List<String> getRelIdsByRoadName(@Param("roadName") String roadName);
} }
package com.example.pipelinenetwork.pojo.dto;
import lombok.Data;
/**
* @Author: wujipeng
* @Description: TODO
* @Date: 2026/7/22 10:11
* @Version: 1.0
*/
@Data
public class LoginDTO {
private String username;
private String password;
}
package com.example.pipelinenetwork.pojo.dto;
import lombok.Data;
/**
* @Author: wujipeng
* @Description: TODO
* @Date: 2026/7/22 11:05
* @Version: 1.0
*/
@Data
public class RoadDTO {
/**
* 地区
*/
private String region;
/**
* 道路名称
*/
private String roadName;
/**
* 类型
*/
private String type;
/**
* 缺陷类型
*/
private String defectType;
/**
* 缺陷等级
*/
private String lev;
}
package com.example.pipelinenetwork.pojo.entity; package com.example.pipelinenetwork.pojo.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
import java.io.Serializable;
@Data @Data
@TableName("detection") @TableName("basic")
public class DetectionEntity{ public class BasicEntity {
@TableId(type = IdType.ASSIGN_ID) @TableId(type = IdType.AUTO)
private String id; private Long id;
/** /**
* 地区 * 行政区
*/ */
private String region; private String region;
/** /**
* 报告名称 * 报告名称
*/ */
@TableField(value = "report_name")
private String reportName; private String reportName;
/** /**
* 路名 * 道路名称
*/ */
@TableField(value = "road_name")
private String roadName; private String roadName;
/** /**
* 管道数量 * 管道数量
*/ */
@TableField(value = "pipe_num")
private Integer pipeNum; private Integer pipeNum;
/** /**
* 管道长度 * 检测长度
*/ */
private Double pipeLength; @TableField(value = "detection_length")
private Double detectionLength;
/** /**
* 缺陷数量 * 缺陷数量
*/ */
@TableField(value = "defect_num")
private Integer defectNum; private Integer defectNum;
@TableField(value = "rel_id")
private String relId;
} }
package com.example.pipelinenetwork.pojo.entity; package com.example.pipelinenetwork.pojo.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
...@@ -9,8 +10,8 @@ import lombok.Data; ...@@ -9,8 +10,8 @@ import lombok.Data;
@TableName("defect") @TableName("defect")
public class DefectEntity { public class DefectEntity {
@TableId(type = IdType.ASSIGN_ID) @TableId(type = IdType.AUTO)
private String id; private Long id;
/** /**
* 缺陷类型 * 缺陷类型
...@@ -31,4 +32,7 @@ public class DefectEntity { ...@@ -31,4 +32,7 @@ public class DefectEntity {
* 缺陷数量 * 缺陷数量
*/ */
private Integer num; private Integer num;
@TableField(value = "rel_id")
private String relId;
} }
...@@ -31,5 +31,41 @@ public class RoadVO extends RoadEntity { ...@@ -31,5 +31,41 @@ public class RoadVO extends RoadEntity {
* 匹配到的路段的坐标列表 * 匹配到的路段的坐标列表
*/ */
private List<List<Double>> coordinates; private List<List<Double>> coordinates;
/**
* 管道数量
*/
private Integer pipeNum;
/**
* 路段长度
*/
private Double length;
/**
* 结构性缺陷
*/
private Integer structuralDefect;
/**
* 功能性缺陷
*/
private Integer functionalDefect;
/**
* 缺陷类型
*/
private String defectType;
/**
* 缺陷名称
*/
private String defectName;
/**
* 缺陷等级
*/
private String defectLev;
/**
* 缺陷数量
*/
private Integer defectNum;
/**
* 数据
*/
private List<RoadVO> data;
} }
\ No newline at end of file
package com.example.pipelinenetwork.service; package com.example.pipelinenetwork.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.example.pipelinenetwork.pojo.entity.DetectionEntity; import com.example.pipelinenetwork.pojo.entity.BasicEntity;
import java.util.List;
/** /**
* @Author: wujipeng * @Author: wujipeng
...@@ -9,5 +11,9 @@ import com.example.pipelinenetwork.pojo.entity.DetectionEntity; ...@@ -9,5 +11,9 @@ import com.example.pipelinenetwork.pojo.entity.DetectionEntity;
* @Date: 2026/7/20 13:59 * @Date: 2026/7/20 13:59
* @Version: 1.0 * @Version: 1.0
*/ */
public interface DetectionService extends IService<DetectionEntity> { public interface BasicService extends IService<BasicEntity> {
List<String> getRelIdsByRegion(String region);
List<String> getRelIdsByRoadName(String roadName);
} }
...@@ -2,6 +2,9 @@ package com.example.pipelinenetwork.service; ...@@ -2,6 +2,9 @@ package com.example.pipelinenetwork.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.example.pipelinenetwork.pojo.entity.DefectEntity; import com.example.pipelinenetwork.pojo.entity.DefectEntity;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/** /**
* @Author: wujipeng * @Author: wujipeng
...@@ -10,4 +13,6 @@ import com.example.pipelinenetwork.pojo.entity.DefectEntity; ...@@ -10,4 +13,6 @@ import com.example.pipelinenetwork.pojo.entity.DefectEntity;
* @Version: 1.0 * @Version: 1.0
*/ */
public interface DefectService extends IService<DefectEntity> { public interface DefectService extends IService<DefectEntity> {
Boolean importDefectData(MultipartFile file) throws IOException;
} }
package com.example.pipelinenetwork.service; package com.example.pipelinenetwork.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.example.pipelinenetwork.pojo.dto.RoadDTO;
import com.example.pipelinenetwork.pojo.vo.RoadVO; import com.example.pipelinenetwork.pojo.vo.RoadVO;
import com.example.pipelinenetwork.pojo.entity.RoadEntity; import com.example.pipelinenetwork.pojo.entity.RoadEntity;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @Author: wujipeng * @Author: wujipeng
...@@ -19,4 +21,8 @@ public interface RoadService extends IService<RoadEntity> { ...@@ -19,4 +21,8 @@ public interface RoadService extends IService<RoadEntity> {
List<String> importRoadData(MultipartFile file) throws Exception; List<String> importRoadData(MultipartFile file) throws Exception;
Map<String,Integer> roadDetail(RoadDTO roadDTO);
RoadVO queryByRoadName(RoadDTO roadDTO);
} }
package com.example.pipelinenetwork.service.impl; package com.example.pipelinenetwork.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pipelinenetwork.mapper.DetectionMapper; import com.example.pipelinenetwork.mapper.BasicMapper;
import com.example.pipelinenetwork.pojo.entity.DetectionEntity; import com.example.pipelinenetwork.pojo.entity.BasicEntity;
import com.example.pipelinenetwork.service.DetectionService; import com.example.pipelinenetwork.service.BasicService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
/** /**
* @Author: wujipeng * @Author: wujipeng
* @Description: TODO * @Description: TODO
...@@ -13,5 +15,14 @@ import org.springframework.stereotype.Service; ...@@ -13,5 +15,14 @@ import org.springframework.stereotype.Service;
* @Version: 1.0 * @Version: 1.0
*/ */
@Service @Service
public class DetectionServiceImpl extends ServiceImpl<DetectionMapper, DetectionEntity> implements DetectionService { public class BasicServiceImpl extends ServiceImpl<BasicMapper, BasicEntity> implements BasicService {
@Override
public List<String> getRelIdsByRegion(String region) {
return baseMapper.getRelIdsByRegion(region);
}
@Override
public List<String> getRelIdsByRoadName(String roadName) {
return baseMapper.getRelIdsByRoadName(roadName);
}
} }
...@@ -4,7 +4,15 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; ...@@ -4,7 +4,15 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pipelinenetwork.mapper.DefectMapper; import com.example.pipelinenetwork.mapper.DefectMapper;
import com.example.pipelinenetwork.pojo.entity.DefectEntity; import com.example.pipelinenetwork.pojo.entity.DefectEntity;
import com.example.pipelinenetwork.service.DefectService; import com.example.pipelinenetwork.service.DefectService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/** /**
* @Author: wujipeng * @Author: wujipeng
...@@ -14,4 +22,90 @@ import org.springframework.stereotype.Service; ...@@ -14,4 +22,90 @@ import org.springframework.stereotype.Service;
*/ */
@Service @Service
public class DefectServiceImpl extends ServiceImpl<DefectMapper, DefectEntity> implements DefectService { public class DefectServiceImpl extends ServiceImpl<DefectMapper, DefectEntity> implements DefectService {
@Override
@Transactional
public Boolean importDefectData(MultipartFile file) throws IOException {
List<DefectEntity> defectEntities = new ArrayList<>();
try (InputStream inputStream = file.getInputStream();
Workbook workbook = WorkbookFactory.create(inputStream)) {
Sheet sheet = workbook.getSheetAt(0);
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
DefectEntity defect = new DefectEntity();
defect.setType(getCellStringValue(sheet.getRow(i).getCell(0)));
defect.setName(getCellStringValue(sheet.getRow(i).getCell(1)));
defect.setLevel(getCellStringValue(sheet.getRow(i).getCell(2)));
defect.setNum(getCellIntValue(sheet.getRow(i).getCell(3)));
defect.setRelId(getCellStringValue(sheet.getRow(i).getCell(4)));
defectEntities.add(defect);
}
return saveBatch(defectEntities);
}
}
/**
* 安全获取字符串单元格值
*/
private String getCellStringValue(Cell cell) {
if (cell == null) {
return null;
}
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue().trim();
case NUMERIC:
// 处理数字转字符串的情况
double value = cell.getNumericCellValue();
if (value == Math.floor(value) && !Double.isInfinite(value)) {
return String.valueOf((long) value);
}
return String.valueOf(value);
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case FORMULA:
try {
return cell.getStringCellValue();
} catch (IllegalStateException e) {
return String.valueOf(cell.getNumericCellValue());
}
default:
return null;
}
}
/**
* 安全获取整数单元格值
*/
private Integer getCellIntValue(Cell cell) {
if (cell == null) {
return null;
}
switch (cell.getCellType()) {
case NUMERIC:
return (int) cell.getNumericCellValue();
case STRING:
try {
return Integer.parseInt(cell.getStringCellValue().trim());
} catch (NumberFormatException e) {
return null;
}
default:
return null;
}
}
/**
* 检查行是否为空
*/
private boolean isRowEmpty(Row row) {
if (row == null) {
return true;
}
for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
Cell cell = row.getCell(c);
if (cell != null && cell.getCellType() != CellType.BLANK) {
return false;
}
}
return true;
}
} }
package com.example.pipelinenetwork.service.impl; package com.example.pipelinenetwork.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pipelinenetwork.mapper.RoadMapper; import com.example.pipelinenetwork.mapper.RoadMapper;
import com.example.pipelinenetwork.pojo.dto.RoadDTO;
import com.example.pipelinenetwork.pojo.entity.BasicEntity;
import com.example.pipelinenetwork.pojo.entity.DefectEntity;
import com.example.pipelinenetwork.pojo.entity.RoadEntity; import com.example.pipelinenetwork.pojo.entity.RoadEntity;
import com.example.pipelinenetwork.pojo.vo.RoadVO; import com.example.pipelinenetwork.pojo.vo.RoadVO;
import com.example.pipelinenetwork.service.BasicService;
import com.example.pipelinenetwork.service.DefectService;
import com.example.pipelinenetwork.service.RoadService; import com.example.pipelinenetwork.service.RoadService;
import com.example.pipelinenetwork.util.LinestringUtil; import com.example.pipelinenetwork.util.LinestringUtil;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
...@@ -13,14 +19,21 @@ import org.apache.poi.ss.usermodel.WorkbookFactory; ...@@ -13,14 +19,21 @@ import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Service @Service
public class RoadServiceImpl extends ServiceImpl<RoadMapper, RoadEntity> implements RoadService { public class RoadServiceImpl extends ServiceImpl<RoadMapper, RoadEntity> implements RoadService {
@Resource
private BasicService basicService;
@Resource
private DefectService defectService;
@Override @Override
public RoadVO match(Double lng, Double lat) { public RoadVO match(Double lng, Double lat) {
RoadVO roadVO = new RoadVO(); RoadVO roadVO = new RoadVO();
...@@ -46,6 +59,74 @@ public class RoadServiceImpl extends ServiceImpl<RoadMapper, RoadEntity> impleme ...@@ -46,6 +59,74 @@ public class RoadServiceImpl extends ServiceImpl<RoadMapper, RoadEntity> impleme
return parseAndGenerateSQL(file); return parseAndGenerateSQL(file);
} }
@Override
public Map<String, Integer> roadDetail(RoadDTO roadDTO) {
Map<String, Integer> result = new HashMap<>();
//获取relId
List<String> relIds = basicService.getRelIdsByRegion(roadDTO.getRegion());
if ("检测".equals(roadDTO.getType())) {
List<DefectEntity> defectList = defectService.list(new LambdaQueryWrapper<DefectEntity>()
.in(DefectEntity::getRelId, relIds).eq(DefectEntity::getType, roadDTO.getDefectType())
.eq(DefectEntity::getLevel, roadDTO.getLev())
);
for (DefectEntity defectEntity : defectList) {
if (result.containsKey(defectEntity.getType())) {
result.put(defectEntity.getType(), result.get(defectEntity.getType()) + defectEntity.getNum());
} else {
result.put(defectEntity.getType(), defectEntity.getNum());
}
}
} else if ("修复".equals(roadDTO.getType())) {
}
return result;
}
@Override
public RoadVO queryByRoadName(RoadDTO roadDTO) {
RoadVO result = new RoadVO();
//管道数量
int pipeNum = 0;
//路段长度
double length = 0;
//结构性缺陷
int structuralDefect = 0;
//功能性缺陷
int functionalDefect = 0;
//列表
List<RoadVO> data = new ArrayList<>();
//获取relId
List<BasicEntity> list = basicService.list(new LambdaQueryWrapper<BasicEntity>().eq(BasicEntity::getRoadName, roadDTO.getRoadName()));
List<String> relIds = new ArrayList<>();
for (BasicEntity basicEntity : list) {
relIds.add(basicEntity.getRelId());
pipeNum += basicEntity.getPipeNum();
length += basicEntity.getDetectionLength();
}
if ("检测".equals(roadDTO.getType())) {
List<DefectEntity> defectList = defectService.list(new LambdaQueryWrapper<DefectEntity>()
.in(DefectEntity::getRelId, relIds).eq(DefectEntity::getType, roadDTO.getDefectType())
.eq(DefectEntity::getLevel, roadDTO.getLev())
);
for (DefectEntity defectEntity : defectList) {
RoadVO roadVO = new RoadVO();
roadVO.setDefectType(defectEntity.getType());
roadVO.setDefectName(defectEntity.getName());
roadVO.setDefectNum(defectEntity.getNum());
roadVO.setDefectLev(defectEntity.getLevel());
data.add(roadVO);
}
} else if ("修复".equals(roadDTO.getType())) {
}
result.setPipeNum(pipeNum);
result.setLength(length);
result.setStructuralDefect(structuralDefect);
result.setFunctionalDefect(functionalDefect);
result.setData(data);
return result;
}
/** /**
* 解析Excel并生成SQL语句(不执行) * 解析Excel并生成SQL语句(不执行)
*/ */
......
package com.example.pipelinenetwork.util;
import java.math.BigDecimal;
import java.util.regex.Pattern;
public class NumberUtils {
/**
* 判断字符串是否为整数(包括正整数、负整数、0)
*/
public static boolean isInteger(String str) {
if (str == null || str.trim().isEmpty()) {
return false;
}
try {
Long.parseLong(str.trim());
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* 判断字符串是否为浮点数(包括小数、科学计数法)
*/
public static boolean isDouble(String str) {
if (str == null || str.trim().isEmpty()) {
return false;
}
try {
Double.parseDouble(str.trim());
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* 判断字符串是否为数值(使用正则表达式,更精确)
* 支持:整数、小数、负数、科学计数法
*/
private static final Pattern NUMBER_PATTERN =
Pattern.compile("^-?\\d+(\\.\\d+)?([eE][+-]?\\d+)?$");
public static boolean isNumeric(String str) {
if (str == null || str.trim().isEmpty()) {
return false;
}
return NUMBER_PATTERN.matcher(str.trim()).matches();
}
/**
* 判断字符串是否为数值(使用BigDecimal,最安全)
* 能处理各种数值格式,包括前导零等特殊情况
*/
public static boolean isValidNumber(String str) {
if (str == null || str.trim().isEmpty()) {
return false;
}
try {
new BigDecimal(str.trim());
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* 判断字符串是否为正整数
*/
public static boolean isPositiveInteger(String str) {
if (!isInteger(str)) {
return false;
}
return Long.parseLong(str.trim()) > 0;
}
/**
* 判断字符串是否为非负整数(包括0)
*/
public static boolean isNonNegativeInteger(String str) {
if (!isInteger(str)) {
return false;
}
return Long.parseLong(str.trim()) >= 0;
}
/**
* 综合判断方法 - 推荐使用
* 先尝试快速判断,再使用BigDecimal兜底
*/
public static boolean isNumber(String str) {
if (str == null || str.trim().isEmpty()) {
return false;
}
String trimmed = str.trim();
// 快速判断:检查基本字符
boolean hasDigit = false;
boolean hasDot = false;
boolean hasE = false;
for (int i = 0; i < trimmed.length(); i++) {
char c = trimmed.charAt(i);
if (Character.isDigit(c)) {
hasDigit = true;
} else if (c == '.') {
if (hasDot || hasE) return false;
hasDot = true;
} else if (c == 'e' || c == 'E') {
if (hasE || !hasDigit) return false;
hasE = true;
hasDigit = false; // 科学计数法后面必须有数字
} else if (c == '+' || c == '-') {
// 正负号只能在第一位或E/e后面
if (i != 0 && (trimmed.charAt(i-1) != 'e' && trimmed.charAt(i-1) != 'E')) {
return false;
}
} else {
return false;
}
}
if (!hasDigit) return false;
// 最后用BigDecimal验证,确保准确性
try {
new BigDecimal(trimmed);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
\ No newline at end of file
<?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.pipelinenetwork.mapper.BasicMapper">
<select id="getRelIdsByRegion" resultType="java.lang.String">
SELECT rel_id FROM basic WHERE region = #{region}
</select>
<select id="getRelIdsByRoadName" resultType="java.lang.String">
SELECT rel_id FROM basic WHERE road_name = #{roadName}
</select>
</mapper>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment