用springboot编写以下代码//站点编号 //水位,保留2位小数 //流速,保留2位小数 //流量,保留2位小数 //时间 }
时间: 2023-09-30 21:10:22 浏览: 84
毕设代码1:基于springboot框架、MySQL数据库、onenet云平台,在web端对数据进行可视化展示与操作监测河流水质
5星 · 资源好评率100%
@Entity
public class Site {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String siteNumber;
@DecimalMin(value = "0.00", inclusive = true)
@Digits(integer = 10, fraction = 2)
private BigDecimal waterLevel;
@DecimalMin(value = "0.00", inclusive = true)
@Digits(integer = 10, fraction = 2)
private BigDecimal flowRate;
@DecimalMin(value = "0.00", inclusive = true)
@Digits(integer = 10, fraction = 2)
private BigDecimal flowVolume;
private LocalDateTime time;
// getters and setters
}
@Repository
public interface SiteRepository extends JpaRepository<Site, Long> {
}
@Service
public class SiteService {
@Autowired
private SiteRepository siteRepository;
public List<Site> getAllSites() {
return siteRepository.findAll();
}
public Site getSiteById(Long id) {
return siteRepository.findById(id).orElse(null);
}
public Site createSite(Site site) {
site.setTime(LocalDateTime.now());
return siteRepository.save(site);
}
public Site updateSite(Site site) {
Site existingSite = siteRepository.findById(site.getId()).orElse(null);
if (existingSite != null) {
existingSite.setSiteNumber(site.getSiteNumber());
existingSite.setWaterLevel(site.getWaterLevel());
existingSite.setFlowRate(site.getFlowRate());
existingSite.setFlowVolume(site.getFlowVolume());
existingSite.setTime(LocalDateTime.now());
return siteRepository.save(existingSite);
}
return null;
}
public void deleteSite(Long id) {
siteRepository.deleteById(id);
}
}
@RestController
@RequestMapping("/sites")
public class SiteController {
@Autowired
private SiteService siteService;
@GetMapping
public List<Site> getAllSites() {
return siteService.getAllSites();
}
@GetMapping("/{id}")
public Site getSiteById(@PathVariable Long id) {
return siteService.getSiteById(id);
}
@PostMapping
public Site createSite(@RequestBody Site site) {
return siteService.createSite(site);
}
@PutMapping("/{id}")
public Site updateSite(@PathVariable Long id, @RequestBody Site site) {
site.setId(id);
return siteService.updateSite(site);
}
@DeleteMapping("/{id}")
public void deleteSite(@PathVariable Long id) {
siteService.deleteSite(id);
}
}
阅读全文