基于spring boot的宠物领养管理系统代码
时间: 2025-02-02 12:12:35 浏览: 46
基于Spring Boot的宠物领养管理系统是一个典型的Web应用程序,利用Spring Boot框架的快速开发和高效配置特性来实现。以下是一个基本的代码结构和关键组件的介绍:
### 1. 项目结构
```
pet-adoption-system
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com.example.petadoption
│ │ │ ├── controller
│ │ │ ├── service
│ │ │ ├── repository
│ │ │ └── model
│ │ └── resources
│ │ └── application.properties
└── pom.xml
```
### 2. `pom.xml`
```xml
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>pet-adoption-system</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
```
### 3. `application.properties`
```properties
spring.datasource.url=jdbc:h2:mem:petdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
```
### 4. `model` 包
```java
package com.example.petadoption.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Pet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String species;
private String breed;
private String description;
// Getters and Setters
}
```
### 5. `repository` 包
```java
package com.example.petadoption.repository;
import com.example.petadoption.model.Pet;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PetRepository extends JpaRepository<Pet, Long> {
}
```
### 6. `service` 包
```java
package com.example.petadoption.service;
import com.example.petadoption.model.Pet;
import com.example.petadoption.repository.PetRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class PetService {
@Autowired
private PetRepository petRepository;
public List<Pet> getAllPets() {
return petRepository.findAll();
}
public Optional<Pet> getPetById(Long id) {
return petRepository.findById(id);
}
public Pet savePet(Pet pet) {
return petRepository.save(pet);
}
public void deletePet(Long id) {
petRepository.deleteById(id);
}
}
```
### 7. `controller` 包
```java
package com.example.petadoption.controller;
import com.example.petadoption.model.Pet;
import com.example.petadoption.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/pets")
public class PetController {
@Autowired
private PetService petService;
@GetMapping
public List<Pet> getAllPets() {
return petService.getAllPets();
}
@GetMapping("/{id}")
public Optional<Pet> getPetById(@PathVariable Long id) {
return petService.getPetById(id);
}
@PostMapping
public Pet savePet(@RequestBody Pet pet) {
return petService.savePet(pet);
}
@DeleteMapping("/{id}")
public void deletePet(@PathVariable Long id) {
petService.deletePet(id);
}
}
```
### 8. 启动类
```java
package com.example.petadoption;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PetAdoptionApplication {
public static void main(String[] args) {
SpringApplication.run(PetAdoptionApplication.class, args);
}
}
```
通过上述代码结构和关键组件的介绍,你可以快速搭建一个基于Spring Boot的宠物领养管理系统。你可以根据实际需求进一步扩展和优化功能。
阅读全文
相关推荐














