使用vo和dto利用springBoot框架写一个酒店房型的增删改查功能
时间: 2023-07-18 16:22:04 浏览: 176
基于springboot实现的增删改查
5星 · 资源好评率100%
好的,我可以为您提供一些参考代码。首先,需要定义一个RoomTypeVO类和一个RoomTypeDTO类,分别用于前端展示和数据传输。
RoomTypeVO.java:
```java
public class RoomTypeVO {
private Long id;
private String name;
private String description;
private Double price;
// getters and setters
}
```
RoomTypeDTO.java:
```java
public class RoomTypeDTO {
private Long id;
private String name;
private String description;
private Double price;
// getters and setters
}
```
接下来,需要定义一个RoomTypeService接口和一个RoomTypeServiceImpl实现类,用于处理业务逻辑。
RoomTypeService.java:
```java
public interface RoomTypeService {
List<RoomTypeDTO> getAllRoomTypes();
RoomTypeDTO getRoomTypeById(Long id);
void createRoomType(RoomTypeDTO roomTypeDTO);
void updateRoomType(RoomTypeDTO roomTypeDTO);
void deleteRoomType(Long id);
}
```
RoomTypeServiceImpl.java:
```java
@Service
public class RoomTypeServiceImpl implements RoomTypeService {
@Autowired
private RoomTypeRepository roomTypeRepository;
@Override
public List<RoomTypeDTO> getAllRoomTypes() {
List<RoomType> roomTypes = roomTypeRepository.findAll();
return roomTypes.stream().map(this::convertToDTO).collect(Collectors.toList());
}
@Override
public RoomTypeDTO getRoomTypeById(Long id) {
RoomType roomType = roomTypeRepository.findById(id).orElse(null);
return convertToDTO(roomType);
}
@Override
public void createRoomType(RoomTypeDTO roomTypeDTO) {
RoomType roomType = convertToEntity(roomTypeDTO);
roomTypeRepository.save(roomType);
}
@Override
public void updateRoomType(RoomTypeDTO roomTypeDTO) {
RoomType roomType = convertToEntity(roomTypeDTO);
roomTypeRepository.save(roomType);
}
@Override
public void deleteRoomType(Long id) {
roomTypeRepository.deleteById(id);
}
private RoomTypeDTO convertToDTO(RoomType roomType) {
if (roomType == null) {
return null;
}
RoomTypeDTO roomTypeDTO = new RoomTypeDTO();
roomTypeDTO.setId(roomType.getId());
roomTypeDTO.setName(roomType.getName());
roomTypeDTO.setDescription(roomType.getDescription());
roomTypeDTO.setPrice(roomType.getPrice());
return roomTypeDTO;
}
private RoomType convertToEntity(RoomTypeDTO roomTypeDTO) {
if (roomTypeDTO == null) {
return null;
}
RoomType roomType = new RoomType();
roomType.setId(roomTypeDTO.getId());
roomType.setName(roomTypeDTO.getName());
roomType.setDescription(roomTypeDTO.getDescription());
roomType.setPrice(roomTypeDTO.getPrice());
return roomType;
}
}
```
最后,需要定义一个RoomTypeController类,用于处理HTTP请求。
RoomTypeController.java:
```java
@RestController
@RequestMapping("/room-types")
public class RoomTypeController {
@Autowired
private RoomTypeService roomTypeService;
@GetMapping
public List<RoomTypeDTO> getAllRoomTypes() {
return roomTypeService.getAllRoomTypes();
}
@GetMapping("/{id}")
public RoomTypeDTO getRoomTypeById(@PathVariable Long id) {
return roomTypeService.getRoomTypeById(id);
}
@PostMapping
public void createRoomType(@RequestBody RoomTypeDTO roomTypeDTO) {
roomTypeService.createRoomType(roomTypeDTO);
}
@PutMapping("/{id}")
public void updateRoomType(@PathVariable Long id, @RequestBody RoomTypeDTO roomTypeDTO) {
roomTypeDTO.setId(id);
roomTypeService.updateRoomType(roomTypeDTO);
}
@DeleteMapping("/{id}")
public void deleteRoomType(@PathVariable Long id) {
roomTypeService.deleteRoomType(id);
}
}
```
以上代码是一个简单的酒店房型的增删改查功能的实现,您可以根据实际需求进行修改和扩展。
阅读全文