spring Boot中的service层是业务逻辑层,负责处理业务需求,封装业务方法,调用dao层的数据操作1。service层一般是一个接口和一个实现类,用@Service注解标注实现类2。service层的接口可以在controller层中调用,实现数据的传递和处理。
一个service层的示例代码如下:
- 首先,需要定义一个service层接口,例如ProductService.java,用于声明业务方法,如增加、编辑、获取和删除产品。
- 然后,需要定义一个service层实现类,例如ProductServiceImpl.java,用@Service注解标注,并实现接口中的业务方法,调用dao层的数据操作。
- 最后,在controller层中,用@Autowired注解自动注入service层接口,并调用其方法,返回数据给客户端。
具体的代码如下:
ProductService.java
package com.tutorialspoint.demo.service;
import java.util.Collection;
import com.tutorialspoint.demo.model.Product;
publicinterfaceProductService {
publicabstractvoidcreateProduct(Product product);
publicabstractvoidupdateProduct(String id, Product product);
publicabstractvoiddeleteProduct(String id);
publicabstract Collection<Product> getProducts();
}
ProductServiceImpl.java
package com.tutorialspoint.demo.service;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.tutorialspoint.demo.model.Product;
@ServicepublicclassProductServiceImplimplementsProductService {
privatestatic Map<String, Product> productRepo = newHashMap<>();
static {
Producthoney=newProduct();
honey.setId("1");
honey.setName("Honey");
productRepo.put(honey.getId(), honey);
Productalmond=newProduct();
almond.setId("2");
almond.setName("Almond");
productRepo.put(almond.getId(), almond);
}
@OverridepublicvoidcreateProduct(Product product) {
productRepo.put(product.getId(), product);
}
@OverridepublicvoidupdateProduct(String id, Product product) {
productRepo.remove(id);
product.setId(id);
productRepo.put(id, product);
}
@OverridepublicvoiddeleteProduct(String id) {
productRepo.remove(id);
}
@Overridepublic Collection<Product> getProducts() {
return productRepo.values();
}
}
ProductServiceController.java
package com.tutorialspoint.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.tutorialspoint.demo.model.Product;
import com.tutorialspoint.demo.service.ProductService;
@RestControllerpublicclassProductServiceController {
@Autowired
ProductService productService;
@RequestMapping(value = "/products")public ResponseEntity<Object> getProduct() {
returnnewResponseEntity<>(productService.getProducts(), HttpStatus.OK);
}
@RequestMapping(value = "/products/{id}", method = RequestMethod.PUT)public ResponseEntity<Object> updateProduct(@PathVariable("id") String id, @RequestBody Product product) {
productService.updateProduct(id, product);
returnnewResponseEntity<>("Product is updated successsfully", HttpStatus.OK);
}
@RequestMapping(value = "/products/{id}", method = RequestMethod.DELETE)public ResponseEntity<Object> delete(@PathVariable("id") String id) {
productService.deleteProduct(id);
returnnewResponseEntity<>("Product is deleted successsfully", HttpStatus.OK);
}
@RequestMapping(value = "/products", method = RequestMethod.POST)public ResponseEntity<Object> createProduct(@RequestBody Product product) {
productService.createProduct(product);
returnnewResponseEntity<>("Product is created successfully", HttpStatus.CREATED);
}
}
版权归原作者 EuclideanSpace 所有, 如有侵权,请联系我们删除。