Springboot Service、ServiceImpl和Repository [已关闭]

gtlvzcf8  于 2023-08-04  发布在  Spring
关注(0)|答案(2)|浏览(92)

已关闭。此问题需要details or clarity。它目前不接受回答。
**希望改进此问题?**通过editing this post添加详细信息并阐明问题。

昨天关门了。
Improve this question
许多Sping Boot 应用程序都是由Repository,Service和Service Implementation构成的。
示例如下:

仓库

@Repository
public interface CarRepository extends JpaRepository<Car, Long> {

    Optional<Car> findById(Long id);    
}

字符串

汽车服务

public interface CarService {
Optional<Car> findById(Long id);
}

CarServiceImpl

@Service
@Transactional
@Slf4j
@RequiredArgsConstructor
public class CarServiceImpl implements CarService {
    private final CarRepository carRepository;

    @Override
    public Optional<Car> findById(Long id) {
        return carRepository.findById(id);
    }    
}


现在在控制器上,例如我已经看到:

车载控制器

@RestController
@RequestMapping("/")
@RequiredArgsConstructor
public class CarController {

@Autowired
private final CarService carService

    // GET ONE CAR
    @GetMapping("/{id}")
    public ResponseEntity<Optional<Car>> find(@PathVariable Long id) {
        return ResponseEntity.ok()
                .header("Custom-Header", "foo")
                .body(carService.findById(id));
    }


但如果我想保存一辆车我需要访问:

@Autowired
private final CarRepository carRepository


然后是控制器上的一个方法,如:

// SAVE ONE CAR
@PostMapping("/")
Car newCar(@RequestBody Car car) {
    return carRepository.save(car);
}


问:我是否遗漏了什么,或者可以解释为什么在保存实体时我直接访问存储库,而在查询时我通过服务实现?

mnemlml8

mnemlml81#

仓库

public interface CarRepository extends JpaRepository<Car, Long> {
    //remove this method because it`s provided by CrudRepository
    Optional<Car> findById(Long id);
  
}

字符串

售后服务

public interface CarService {

    Optional<Car> findById(Long id);
    Car saveCar(Car car);
}

服务含义

@Service
@Slf4j
@RequiredArgsConstructor
public class CarServiceImpl implements CarService {
private final CarRepository carRepository;

@Override
public Car findById(Long id) {

    return carRepository.findById(id).orElse("return something if car by id is not present in database");
}

@Override
public Car saveCar(Car car) {
    return carRepository.save(car);
    }
}

控制器

@RestController
@RequestMapping("/")
@RequiredArgsConstructor
public class CarController {

private final CarService carService;

// GET ONE CAR
@GetMapping("/{id}")
public ResponseEntity<Car> find(@PathVariable Long id) {
    return ResponseEntity.ok()
            .header("Custom-Header", "foo")
            .body(carService.findById(id);
}

// SAVE ONE CAR
@PostMapping("/")
ResponseEntity<Car> newCar(@RequestBody Car Car) {
    return ResponseEntity.ok().body(carService.saveCar(car));
   }
}

jc3wubiy

jc3wubiy2#

我发现您可以在Repository上引用entityRepository.save而不使用保存方法,因为它是您默认获得的CRUD方法的一部分。
将您自己的保存方法添加到服务和存储库中。设置如下:

存储库

@Repository // DELETE @Repository?
public interface CarRepository extends JpaRepository<Car, Long> {

    Optional<Car> findById(Long id);
    // No need for save method here as provided by CRUD out of the box
}

字符串

售后服务

public interface CarService {

    Optional<Car> findById(Long id);
    public Car saveCar(Car car);
}

服务实现

任何相关的业务逻辑都在这个服务实现中。

@Service
@Transactional
@Slf4j
@RequiredArgsConstructor
public class CarServiceImpl implements CarService {
private final CarRepository carRepository;

@Override
public Optional<Car> findById(Long id) {
    return carRepository.findById(id);
}

public Car saveCar(Car car) {
    return carRepository.save(car);
    }
}

控制器

@RestController
@RequestMapping("/")
@RequiredArgsConstructor
public class CarController {

@Autowired
private final CarServiceImpl carServiceImpl;

// GET ONE CAR
@GetMapping("/{id}")
public ResponseEntity<Optional<Car>> find(@PathVariable Long id) {
    return ResponseEntity.ok()
            .header("Custom-Header", "foo")
            .body(carServiceImpl.findById(id));
}

// SAVE ONE CAR
@PostMapping("/")
Car newCar(@RequestBody Car Car) {
    return carServiceImpl.saveCar(car);
   }
}

相关问题