使用springboot 生成一个设备管理系统
时间: 2023-11-01 15:08:26 浏览: 97
好的,下面是一个简单的设备管理系统的 Spring Boot 项目:
1. 创建一个新的 Spring Boot 项目,可以使用 Spring Initializr 进行快速创建。
2. 添加以下依赖:
```
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
```
3. 创建设备实体类 Device.java,并添加必要的属性和注解:
```
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Device {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
private String name;
@NotBlank
private String type;
@NotNull
private Integer quantity;
private String description;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdDate;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedDate;
}
```
4. 创建设备 Repository 接口 DeviceRepository.java:
```
@Repository
public interface DeviceRepository extends JpaRepository<Device, Long> {
}
```
5. 创建设备 Service 接口 DeviceService.java:
```
public interface DeviceService {
List<Device> getAllDevices();
Device createDevice(Device device);
Device updateDevice(Long id, Device device);
void deleteDevice(Long id);
}
```
6. 创建设备 Service 实现类 DeviceServiceImpl.java:
```
@Service
public class DeviceServiceImpl implements DeviceService {
@Autowired
private DeviceRepository deviceRepository;
@Override
public List<Device> getAllDevices() {
return deviceRepository.findAll();
}
@Override
public Device createDevice(Device device) {
device.setCreatedDate(LocalDateTime.now());
return deviceRepository.save(device);
}
@Override
public Device updateDevice(Long id, Device device) {
Device existingDevice = deviceRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Device not found with id " + id));
existingDevice.setName(device.getName());
existingDevice.setType(device.getType());
existingDevice.setQuantity(device.getQuantity());
existingDevice.setDescription(device.getDescription());
existingDevice.setUpdatedDate(LocalDateTime.now());
return deviceRepository.save(existingDevice);
}
@Override
public void deleteDevice(Long id) {
Device existingDevice = deviceRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Device not found with id " + id));
deviceRepository.delete(existingDevice);
}
}
```
7. 创建设备 Controller 类 DeviceController.java:
```
@RestController
@RequestMapping("/api/devices")
public class DeviceController {
@Autowired
private DeviceService deviceService;
@GetMapping
public List<Device> getAllDevices() {
return deviceService.getAllDevices();
}
@PostMapping
public ResponseEntity<Device> createDevice(@Valid @RequestBody Device device) {
return new ResponseEntity<>(deviceService.createDevice(device), HttpStatus.CREATED);
}
@PutMapping("/{id}")
public ResponseEntity<Device> updateDevice(@PathVariable Long id, @Valid @RequestBody Device device) {
return new ResponseEntity<>(deviceService.updateDevice(id, device), HttpStatus.OK);
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteDevice(@PathVariable Long id) {
deviceService.deleteDevice(id);
return ResponseEntity.noContent().build();
}
}
```
8. 启动项目并测试接口。
以上就是一个简单的设备管理系统的 Spring Boot 项目。当然,在实际开发中,还需要考虑数据校验、异常处理等方面的问题。
阅读全文