Advertisement

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

阅读量:

以下是一个采用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
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/m3nTU4iBs9LPGXVzJ2NhaWbevlct.png)

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>
    
    
    xml
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/fUw5F1e3oNVkrC4s9TjilRKgqh2c.png)

3. 实体类 (model 包)

Elderly.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 Setters
    public 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
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/4JRy7tjguVeniPsThNqFKLvWU52k.png)
HealthRecord.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 Setters
    public 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
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/LUjn0fcQYI83ZEzSP7NdgFKrvhTD.png)
EmergencyAlert.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 Setters
    public 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
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/mOSBZ34VswjiXU01qQ7xKLEtyT8r.png)
ServiceAppointment.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 Setters
    public 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;
    }
    }
    
    
    java
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/U7z8wSF6uN0c4oMIKat2Z1PQm5hs.png)

4. 仓库接口 (repository 包)

ElderlyRepository.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
HealthRecordRepository.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
EmergencyAlertRepository.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
ServiceAppointmentRepository.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> {
    }
    
    
    java

5. 服务层 (service 包)

ElderlyService.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 {
    @Autowired
    private 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
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/hCuKyRI34YpT1EfeQWzbwFMk0DUv.png)
HealthRecordService.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 {
    @Autowired
    private 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
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/LqWfPm2tB5jDUR0uwOZ4QH1pShKM.png)
EmergencyAlertService.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 {
    @Autowired
    private 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
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/2rfc7lxFE5bXGtZp4KLgNBQhPq3v.png)
ServiceAppointmentService.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 {
    @Autowired
    private 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);
    }
    }
    
    
    java
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/x9qnUaZDckzjmFodRLGYyAM4Ot3h.png)

6. 控制器层 (controller 包)

ElderlyController.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 {
    @Autowired
    private ElderlyService elderlyService;
    
    @GetMapping
    public List<Elderly> getAllElderly() {
        return elderlyService.getAllElderly();
    }
    
    @GetMapping("/{id}")
    public Elderly getElderlyById(@PathVariable Long id) {
        return elderlyService.getElderlyById(id);
    }
    
    @PostMapping
    public 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
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/270XJLrMZPvsqONHbFdwnjTuYS6g.png)
HealthRecordController.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 {
    @Autowired
    private HealthRecordService healthRecordService;
    
    @GetMapping
    public List<HealthRecord> getAllHealthRecords() {
        return healthRecordService.getAllHealthRecords();
    }
    
    @GetMapping("/{id}")
    public HealthRecord getHealthRecordById(@PathVariable Long id) {
        return healthRecordService.getHealthRecordById(id);
    }
    
    @PostMapping
    public 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
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/fEyqnrIdLSbQTJR7C9hcOWjuwvkV.png)
EmergencyAlertController.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 {
    @Autowired
    private EmergencyAlertService emergencyAlertService;
    
    @GetMapping
    public List<EmergencyAlert> getAllEmergencyAlerts() {
        return emergencyAlertService.getAllEmergencyAlerts();
    }
    
    @GetMapping("/{id}")
    public EmergencyAlert getEmergencyAlertById(@PathVariable Long id) {
        return emergencyAlertService.getEmergencyAlertById(id);
    }
    
    @PostMapping
    public 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
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/abdRlkYp0VCjcoXeFJMDxvNLQWtg.png)
ServiceAppointmentController.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 {
    @Autowired
    private ServiceAppointmentService serviceAppointmentService;
    
    @GetMapping
    public List<ServiceAppointment> getAllServiceAppointments() {
        return serviceAppointmentService.getAllServiceAppointments();
    }
    
    @GetMapping("/{id}")
    public ServiceAppointment getServiceAppointmentById(@PathVariable Long id) {
        return serviceAppointmentService.getServiceAppointmentById(id);
    }
    
    @PostMapping
    public 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
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/7K6EnozCetx0M43udBpvRUSgFwiq.png)

7. 主应用类 (SmartElderlyCareApplication.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);
    }
    }
    
    
    java
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/wefSM23JnIE5qmWyuFYXdNzrgDoT.png)

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
    
    
    properties

9. 项目

  1. 启动 mvn spring-boot:run 命令用于启动项目。
  2. 访问网站地址 ${url} 以查看H2数据库。
  3. 通过API工具(如Postman)来测试各个接口。

10. 扩展功能

  • 增添注册与权限控制功能(基于Spring Security实现)。

    • 增添健康数据分析功能(基于健康记录自动生成分析报告)。
  • 增添注册与权限控制功能(基于Spring Security实现)。

    • 增添健康数据分析功能(基于健康记录自动生成分析报告)。

全部评论 (0)

还没有任何评论哟~