spring Boot中的service層是業(yè)務(wù)邏輯層,負(fù)責(zé)處理業(yè)務(wù)需求,封裝業(yè)務(wù)方法,調(diào)用dao層的數(shù)據(jù)操作1。service層一般是一個(gè)接口和一個(gè)實(shí)現(xiàn)類,用@Service注解標(biāo)注實(shí)現(xiàn)類2。service層的接口可以在controller層中調(diào)用,實(shí)現(xiàn)數(shù)據(jù)的傳遞和處理。
一個(gè)service層的示例代碼如下:
- 首先,需要定義一個(gè)service層接口,例如ProductService.java,用于聲明業(yè)務(wù)方法,如增加、編輯、獲取和刪除產(chǎn)品。
- 然后,需要定義一個(gè)service層實(shí)現(xiàn)類,例如ProductServiceImpl.java,用@Service注解標(biāo)注,并實(shí)現(xiàn)接口中的業(yè)務(wù)方法,調(diào)用dao層的數(shù)據(jù)操作。
- 最后,在controller層中,用@Autowired注解自動(dòng)注入service層接口,并調(diào)用其方法,返回?cái)?shù)據(jù)給客戶端。
具體的代碼如下:
ProductService.java
package com.tutorialspoint.demo.service;
import java.util.Collection;
import com.tutorialspoint.demo.model.Product;
public interface ProductService {
public abstract void createProduct(Product product);
public abstract void updateProduct(String id, Product product);
public abstract void deleteProduct(String id);
public abstract Collection<Product> getProducts();
}
ProductServiceImpl.java文章來源:http://www.zghlxwxcb.cn/news/detail-526075.html
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;
@Service
public class ProductServiceImpl implements ProductService {
private static Map<String, Product> productRepo = new HashMap<>();
static {
Product honey = new Product();
honey.setId("1");
honey.setName("Honey");
productRepo.put(honey.getId(), honey);
Product almond = new Product();
almond.setId("2");
almond.setName("Almond");
productRepo.put(almond.getId(), almond);
}
@Override
public void createProduct(Product product) {
productRepo.put(product.getId(), product);
}
@Override
public void updateProduct(String id, Product product) {
productRepo.remove(id);
product.setId(id);
productRepo.put(id, product);
}
@Override
public void deleteProduct(String id) {
productRepo.remove(id);
}
@Override
public Collection<Product> getProducts() {
return productRepo.values();
}
}
ProductServiceController.java文章來源地址http://www.zghlxwxcb.cn/news/detail-526075.html
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;
@RestController
public class ProductServiceController {
@Autowired
ProductService productService;
@RequestMapping(value = "/products")
public ResponseEntity<Object> getProduct() {
return new ResponseEntity<>(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);
return new ResponseEntity<>("Product is updated successsfully", HttpStatus.OK);
}
@RequestMapping(value = "/products/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Object> delete(@PathVariable("id") String id) {
productService.deleteProduct(id);
return new ResponseEntity<>("Product is deleted successsfully", HttpStatus.OK);
}
@RequestMapping(value = "/products", method = RequestMethod.POST)
public ResponseEntity<Object> createProduct(@RequestBody Product product) {
productService.createProduct(product);
return new ResponseEntity<>("Product is created successfully", HttpStatus.CREATED);
}
}
到了這里,關(guān)于Spring Boot中的service層的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!