使用Service CRUD接口
1.在service
接口中继承IService
import com.baomidou.mybatisplus.extension.service.IService;
import com.xianhuo.xianhuobackend.entity.*;
public interface ProductService extends IService<Product> {
}
IService
,T为实体类
2.在service
接口的实现类中继承ServiceImpl
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xianhuo.xianhuobackend.entity.Product;
import com.xianhuo.xianhuobackend.mapper.ProductMapper;
import com.xianhuo.xianhuobackend.service.ProductService;
import org.springframework.stereotype.Service;
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}
ServiceImpl<M,T>,M表示Mapper类,T表示实体类。
- 在
controller
中注入并进行使用
import com.xianhuo.xianhuobackend.entity.Product;
import com.xianhuo.xianhuobackend.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api")
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("/goods")
public List<Product> allProduct(){
//获取所有
return productService.list();
}
}
更多方法见:[CRUD 接口 | MyBatis-Plus](
标签:Service,service,xianhuo,CRUD,接口,xianhuobackend,import,com From: https://www.cnblogs.com/ewar-k/p/17963099