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

embedded/2025/1/17 4:53:51/

以下是一个基于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/embedded/154562.html

相关文章

计算机网络 (41)文件传送协议

前言 一、文件传送协议&#xff08;FTP&#xff09; 概述&#xff1a; FTP&#xff08;File Transfer Protocol&#xff09;是互联网上使用得最广泛的文件传送协议。FTP提供交互式的访问&#xff0c;允许客户指明文件的类型与格式&#xff08;如指明是否使用ASCII码&#xff0…

ESP8266固件烧录

一、烧录原理 1、引脚布局 2、引脚定义 3、尺寸封装 4、环境要求 5、接线方式 ESP8266系列模块集成了高速GPI0和外围接口&#xff0c;这可能会导致严重的开关噪声。如果某些应用需要高功率和EMI特性&#xff0c;建议在数字I/0线上串联10到100欧姆。这可以在切换电源时抑制过冲…

《CPython Internals》阅读笔记:p96-p96

《CPython Internals》学习第 6 天&#xff0c;p96-p96 总结&#xff0c;总计 1 页。 一、技术总结 1.parser-tokenizer p92, Creating a concrete syntax tree using a parser-tokenizer, or lexer. p96, CPython has a parser-tokenizer module, written in C. 当做这在…

62_Redis服务器集群优化

Redis集群虽然具备高可用特性,且能实现自动故障恢复,但是如果使用不当,也会存在一些问题,总结如下。 集群完整性问题集群带宽问题数据倾斜问题客户端性能问题命令的集群兼容性问题Lua和事务问题1.集群完整性问题 在 Redis 集群的默认配置下,当节点检测到存在至少一个哈希…

【STM32-学习笔记-2-】外部中断

文章目录 外部中断Ⅰ、EXIT函数Ⅱ、EXTI_InitTypeDef结构体参数①、EXTI_Line②、EXTI_LineCmd③、EXTI_Mode④、EXTI_Trigger Ⅲ、NVIC函数Ⅳ、NVIC_InitTypeDef结构体参数①、NVIC_IRQChannel②、NVIC_IRQChannelCmd③、NVIC_IRQChannelPreemptionPriority④、NVIC_IRQChanne…

认识机器学习中的经验风险最小化准则

经验风险最小化准则的定义 经验风险最小化&#xff08;Empirical Risk Minimization&#xff0c;简称 ERM&#xff09;是机器学习中的一种基本理论框架&#xff0c;用于指导模型的训练过程。其核心思想是通过最小化训练数据上的损失函数来优化模型参数&#xff0c;从而提高模型…

MySQL 5.7 与 MySQL 8 的区别

文章目录 前言一、性能改进二、功能增强三、安全性四、开发体验五、默认排序规则六、支持的排序规则数量七、区分敏感性&#xff08;Sensitivity&#xff09;增强八、Unicode 排序的改进九、性能改进十、自定义排序规则 前言 &#x1f19a;MySQL 5.7 与 MySQL 8.0 是两个重要的…

vue.js辅助函数-mapMutations

在Vue.js中&#xff0c;使用辅助函数可以更方便地使用Vuex的mutation。而mapMutations就是Vuex提供的一个辅助函数&#xff0c;它可以将mutation映射到组件的methods中&#xff0c;使得我们可以在组件中直接调用mutation&#xff0c;而不需要手动进行commit。 mapMutations函数…