使用Service CRUD接口

使用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表示实体类。

  1. 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](

posted @ 2024-01-13 22:16  Ewar-k  阅读(47)  评论(0)    收藏  举报