一个基于Spring Boot的智慧养老平台

server/2025/1/13 15:31:14/

以下是一个基于Spring Boot的智慧养老平台的案例代码。这个平台包括老人信息管理、健康监测、紧急呼叫、服务预约等功能。代码结构清晰,适合初学者学习和参考。
在这里插入图片描述

1. 项目结构

src/main/java/com/example/smartelderlycare├── controller│   ├── ElderlyController.java│   ├── HealthRecordController.java│   ├── EmergencyAlertController.java│   └── ServiceAppointmentController.java├── model│   ├── Elderly.java│   ├── HealthRecord.java│   ├── EmergencyAlert.java│   └── ServiceAppointment.java├── repository│   ├── ElderlyRepository.java│   ├── HealthRecordRepository.java│   ├── EmergencyAlertRepository.java│   └── ServiceAppointmentRepository.java├── service│   ├── ElderlyService.java│   ├── HealthRecordService.java│   ├── EmergencyAlertService.java│   └── ServiceAppointmentService.java└── SmartElderlyCareApplication.java

2. 依赖配置 (pom.xml)

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>

3. 实体类 (model 包)

java_65">Elderly.java
java">package com.example.smartelderlycare.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class Elderly {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private int age;private String address;private String contactNumber;// Getters and Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getContactNumber() {return contactNumber;}public void setContactNumber(String contactNumber) {this.contactNumber = contactNumber;}
}
java_128">HealthRecord.java
java">package com.example.smartelderlycare.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDate;@Entity
public class HealthRecord {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private Long elderlyId;private LocalDate recordDate;private double bloodPressure;private double heartRate;private String notes;// Getters and Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public Long getElderlyId() {return elderlyId;}public void setElderlyId(Long elderlyId) {this.elderlyId = elderlyId;}public LocalDate getRecordDate() {return recordDate;}public void setRecordDate(LocalDate recordDate) {this.recordDate = recordDate;}public double getBloodPressure() {return bloodPressure;}public void setBloodPressure(double bloodPressure) {this.bloodPressure = bloodPressure;}public double getHeartRate() {return heartRate;}public void setHeartRate(double heartRate) {this.heartRate = heartRate;}public String getNotes() {return notes;}public void setNotes(String notes) {this.notes = notes;}
}
java_201">EmergencyAlert.java
java">package com.example.smartelderlycare.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;@Entity
public class EmergencyAlert {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private Long elderlyId;private LocalDateTime alertTime;private String message;// Getters and Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public Long getElderlyId() {return elderlyId;}public void setElderlyId(Long elderlyId) {this.elderlyId = elderlyId;}public LocalDateTime getAlertTime() {return alertTime;}public void setAlertTime(LocalDateTime alertTime) {this.alertTime = alertTime;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}
java_256">ServiceAppointment.java
java">package com.example.smartelderlycare.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;@Entity
public class ServiceAppointment {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private Long elderlyId;private LocalDateTime appointmentTime;private String serviceType;private String notes;// Getters and Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public Long getElderlyId() {return elderlyId;}public void setElderlyId(Long elderlyId) {this.elderlyId = elderlyId;}public LocalDateTime getAppointmentTime() {return appointmentTime;}public void setAppointmentTime(LocalDateTime appointmentTime) {this.appointmentTime = appointmentTime;}public String getServiceType() {return serviceType;}public void setServiceType(String serviceType) {this.serviceType = serviceType;}public String getNotes() {return notes;}public void setNotes(String notes) {this.notes = notes;}
}

4. 仓库接口 (repository 包)

java_322">ElderlyRepository.java
java">package com.example.smartelderlycare.repository;import com.example.smartelderlycare.model.Elderly;
import org.springframework.data.jpa.repository.JpaRepository;public interface ElderlyRepository extends JpaRepository<Elderly, Long> {
}
java_334">HealthRecordRepository.java
java">package com.example.smartelderlycare.repository;import com.example.smartelderlycare.model.HealthRecord;
import org.springframework.data.jpa.repository.JpaRepository;public interface HealthRecordRepository extends JpaRepository<HealthRecord, Long> {
}
java_346">EmergencyAlertRepository.java
java">package com.example.smartelderlycare.repository;import com.example.smartelderlycare.model.EmergencyAlert;
import org.springframework.data.jpa.repository.JpaRepository;public interface EmergencyAlertRepository extends JpaRepository<EmergencyAlert, Long> {
}
java_358">ServiceAppointmentRepository.java
java">package com.example.smartelderlycare.repository;import com.example.smartelderlycare.model.ServiceAppointment;
import org.springframework.data.jpa.repository.JpaRepository;public interface ServiceAppointmentRepository extends JpaRepository<ServiceAppointment, Long> {
}

5. 服务层 (service 包)

java_372">ElderlyService.java
java">package com.example.smartelderlycare.service;import com.example.smartelderlycare.model.Elderly;
import com.example.smartelderlycare.repository.ElderlyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class ElderlyService {@Autowiredprivate ElderlyRepository elderlyRepository;public List<Elderly> getAllElderly() {return elderlyRepository.findAll();}public Elderly getElderlyById(Long id) {return elderlyRepository.findById(id).orElse(null);}public Elderly saveElderly(Elderly elderly) {return elderlyRepository.save(elderly);}public void deleteElderly(Long id) {elderlyRepository.deleteById(id);}
}
java_407">HealthRecordService.java
java">package com.example.smartelderlycare.service;import com.example.smartelderlycare.model.HealthRecord;
import com.example.smartelderlycare.repository.HealthRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class HealthRecordService {@Autowiredprivate HealthRecordRepository healthRecordRepository;public List<HealthRecord> getAllHealthRecords() {return healthRecordRepository.findAll();}public HealthRecord getHealthRecordById(Long id) {return healthRecordRepository.findById(id).orElse(null);}public HealthRecord saveHealthRecord(HealthRecord healthRecord) {return healthRecordRepository.save(healthRecord);}public void deleteHealthRecord(Long id) {healthRecordRepository.deleteById(id);}
}
java_442">EmergencyAlertService.java
java">package com.example.smartelderlycare.service;import com.example.smartelderlycare.model.EmergencyAlert;
import com.example.smartelderlycare.repository.EmergencyAlertRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class EmergencyAlertService {@Autowiredprivate EmergencyAlertRepository emergencyAlertRepository;public List<EmergencyAlert> getAllEmergencyAlerts() {return emergencyAlertRepository.findAll();}public EmergencyAlert getEmergencyAlertById(Long id) {return emergencyAlertRepository.findById(id).orElse(null);}public EmergencyAlert saveEmergencyAlert(EmergencyAlert emergencyAlert) {return emergencyAlertRepository.save(emergencyAlert);}public void deleteEmergencyAlert(Long id) {emergencyAlertRepository.deleteById(id);}
}
java_477">ServiceAppointmentService.java
java">package com.example.smartelderlycare.service;import com.example.smartelderlycare.model.ServiceAppointment;
import com.example.smartelderlycare.repository.ServiceAppointmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class ServiceAppointmentService {@Autowiredprivate ServiceAppointmentRepository serviceAppointmentRepository;public List<ServiceAppointment> getAllServiceAppointments() {return serviceAppointmentRepository.findAll();}public ServiceAppointment getServiceAppointmentById(Long id) {return serviceAppointmentRepository.findById(id).orElse(null);}public ServiceAppointment saveServiceAppointment(ServiceAppointment serviceAppointment) {return serviceAppointmentRepository.save(serviceAppointment);}public void deleteServiceAppointment(Long id) {serviceAppointmentRepository.deleteById(id);}
}

6. 控制器层 (controller 包)

java_514">ElderlyController.java
java">package com.example.smartelderlycare.controller;import com.example.smartelderlycare.model.Elderly;
import com.example.smartelderlycare.service.ElderlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/elderly")
public class ElderlyController {@Autowiredprivate ElderlyService elderlyService;@GetMappingpublic List<Elderly> getAllElderly() {return elderlyService.getAllElderly();}@GetMapping("/{id}")public Elderly getElderlyById(@PathVariable Long id) {return elderlyService.getElderlyById(id);}@PostMappingpublic Elderly createElderly(@RequestBody Elderly elderly) {return elderlyService.saveElderly(elderly);}@PutMapping("/{id}")public Elderly updateElderly(@PathVariable Long id, @RequestBody Elderly elderly) {elderly.setId(id);return elderlyService.saveElderly(elderly);}@DeleteMapping("/{id}")public void deleteElderly(@PathVariable Long id) {elderlyService.deleteElderly(id);}
}
java_560">HealthRecordController.java
java">package com.example.smartelderlycare.controller;import com.example.smartelderlycare.model.HealthRecord;
import com.example.smartelderlycare.service.HealthRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/health-records")
public class HealthRecordController {@Autowiredprivate HealthRecordService healthRecordService;@GetMappingpublic List<HealthRecord> getAllHealthRecords() {return healthRecordService.getAllHealthRecords();}@GetMapping("/{id}")public HealthRecord getHealthRecordById(@PathVariable Long id) {return healthRecordService.getHealthRecordById(id);}@PostMappingpublic HealthRecord createHealthRecord(@RequestBody HealthRecord healthRecord) {return healthRecordService.saveHealthRecord(healthRecord);}@PutMapping("/{id}")public HealthRecord updateHealthRecord(@PathVariable Long id, @RequestBody HealthRecord healthRecord) {healthRecord.setId(id);return healthRecordService.saveHealthRecord(healthRecord);}@DeleteMapping("/{id}")public void deleteHealthRecord(@PathVariable Long id) {healthRecordService.deleteHealthRecord(id);}
}
java_606">EmergencyAlertController.java
java">package com.example.smartelderlycare.controller;import com.example.smartelderlycare.model.EmergencyAlert;
import com.example.smartelderlycare.service.EmergencyAlertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/emergency-alerts")
public class EmergencyAlertController {@Autowiredprivate EmergencyAlertService emergencyAlertService;@GetMappingpublic List<EmergencyAlert> getAllEmergencyAlerts() {return emergencyAlertService.getAllEmergencyAlerts();}@GetMapping("/{id}")public EmergencyAlert getEmergencyAlertById(@PathVariable Long id) {return emergencyAlertService.getEmergencyAlertById(id);}@PostMappingpublic EmergencyAlert createEmergencyAlert(@RequestBody EmergencyAlert emergencyAlert) {return emergencyAlertService.saveEmergencyAlert(emergencyAlert);}@PutMapping("/{id}")public EmergencyAlert updateEmergencyAlert(@PathVariable Long id, @RequestBody EmergencyAlert emergencyAlert) {emergencyAlert.setId(id);return emergencyAlertService.saveEmergencyAlert(emergencyAlert);}@DeleteMapping("/{id}")public void deleteEmergencyAlert(@PathVariable Long id) {emergencyAlertService.deleteEmergencyAlert(id);}
}
java_652">ServiceAppointmentController.java
java">package com.example.smartelderlycare.controller;import com.example.smartelderlycare.model.ServiceAppointment;
import com.example.smartelderlycare.service.ServiceAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/service-appointments")
public class ServiceAppointmentController {@Autowiredprivate ServiceAppointmentService serviceAppointmentService;@GetMappingpublic List<ServiceAppointment> getAllServiceAppointments() {return serviceAppointmentService.getAllServiceAppointments();}@GetMapping("/{id}")public ServiceAppointment getServiceAppointmentById(@PathVariable Long id) {return serviceAppointmentService.getServiceAppointmentById(id);}@PostMappingpublic ServiceAppointment createServiceAppointment(@RequestBody ServiceAppointment serviceAppointment) {return serviceAppointmentService.saveServiceAppointment(serviceAppointment);}@PutMapping("/{id}")public ServiceAppointment updateServiceAppointment(@PathVariable Long id, @RequestBody ServiceAppointment serviceAppointment) {serviceAppointment.setId(id);return serviceAppointmentService.saveServiceAppointment(serviceAppointment);}@DeleteMapping("/{id}")public void deleteServiceAppointment(@PathVariable Long id) {serviceAppointmentService.deleteServiceAppointment(id);}
}

java_698">7. 主应用类 (SmartElderlyCareApplication.java)

java">package com.example.smartelderlycare;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SmartElderlyCareApplication {public static void main(String[] args) {SpringApplication.run(SmartElderlyCareApplication.class, args);}
}

8. 配置文件 (application.properties)

spring.datasource.url=jdbc:h2:mem:elderlycare
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true

9. 运行项目

  1. 使用 mvn spring-boot:run 命令运行项目。
  2. 访问 http://localhost:8080/h2-console 查看H2数据库。
  3. 使用API工具(如Postman)测试各个接口。

10. 扩展功能

  • 添加用户登录和权限管理(使用Spring Security)。
  • 添加健康数据分析功能,根据健康记录生成报告。

http://www.ppmy.cn/server/158023.html

相关文章

uniapp中rpx和upx的区别

在 UniApp 中&#xff0c;rpx 和 upx 是两种不同的单位&#xff0c;它们的主要区别在于适用的场景和计算方式。 ### rpx&#xff08;Responsive Pixel&#xff09; - **适用场景**&#xff1a;rpx 是一种响应式单位&#xff0c;主要用于小程序和移动端的布局。 - **计算方式**…

HarMonyOS 鸿蒙系统使用 Grid构建网格

网格布局是由“行”和“列”分割的单元格所组成&#xff0c;通过指定“项目”所在的单元格做出各种各样的布局。网格布局具有较强的页面均分能力&#xff0c;子组件占比控制能力&#xff0c;是一种重要自适应布局&#xff0c;其使用场景有九宫格图片展示、日历、计算器等。 Ar…

论文阅读:Searching for Fast Demosaicking Algorithms

今天介绍一篇有关去马赛克的工作&#xff0c;去马赛克是 ISP 流程里面非常重要的一个模块&#xff0c;可以说是将多姿多彩的大千世界进行色彩还原的重要一步。这篇工作探索的是如何从各种各样的去马赛克算法中&#xff0c;选择最佳的一种。 Abstract 本文提出了一种方法&…

同时支持ERC721和ERC1155数字资产管理的智能合约架构

一、全景洞察 在 NFT 市场中&#xff0c;这款基于 ERC1967&#xff08;代理合约&#xff09;和 ERC11822&#xff08;可升级代理&#xff09;架构的智能合约产品&#xff0c;宛如一位幕后超级英雄&#xff0c;为各类 NFT 业务提供坚实的技术支撑与多样化功能保障。简单来说&…

GNSS经典误差搜索记录:对流层误差

GNSS&#xff08;全球导航卫星系统&#xff09;中的对流层延迟误差是由于电磁波信号在通过未被电离的中性大气层时产生的延迟&#xff0c;这种延迟与地面气候、大气压力、温度及湿度等因素密切相关。对流层延迟是GNSS定位的主要误差源之一&#xff0c;尤其是在高精度定位应用中…

第P5周-Pytorch实现运动鞋品牌识别

&#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 具体实现 &#xff08;一&#xff09;环境 语言环境&#xff1a;Python 3.10 编 译 器: PyCharm 框 架: Pytorch &#xff08;二&#xff09;具体步骤 时间…

用 HTML5 Canvas 和 JavaScript 实现雪花飘落特效

这篇文章将带您深入解析使用 HTML5 Canvas 和 JavaScript 实现动态雪花特效的代码原理。 1,效果展示 该效果模拟了雪花从天而降的动态场景,具有以下特点: 雪花数量、大小、透明度和下落速度随机。雪花会在屏幕底部重置到顶部,形成循环效果。随窗口大小动态调整,始终覆盖…

fastgpt 调用api 调试 写 localhost, 127.0.0.1不行,要 ipconfig 找到本机ip

fastgpt 调用api 调试 写 localhost&#xff0c; 127.0.0.1不行&#xff0c;要 ipconfig 找到本机ip IPv4 地址 . . . . . . . . . . . . : 192.168.1.2