通过MybatisGenerator实现基本的CURD操作

1.先写Controller

@Controller
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;

    //static 修饰的变量是不管创建了new了多少个实例,也只创建一次,节省空间,如果每次都创建Logger的话比较浪费内存;
    // final修饰表示不可更改,常量
    //创建日志实例
    private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class);


    // 获取全部品牌列表
    @RequestMapping(value = "/listall",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsBrand>> getBrandList(){
        return  CommonResult.success(pmsBrandService.listAllBrand());
    }


    // 添加品牌
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody PmsBrand pmsBrand){
        CommonResult commonResult;
        int count = pmsBrandService.create(pmsBrand);
        if (count==1){
            commonResult = CommonResult.success(pmsBrand);
            LOGGER.debug("createBrand success:{}",pmsBrand);
        }else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createBrand failed:{}",pmsBrand);
        }
        return commonResult;
    }


    // 删除品牌
    @RequestMapping(value = "/delete/{id}" ,method = RequestMethod.GET)
    @ResponseBody
    public CommonResult delete(@PathVariable("id") Long id){
        int count = pmsBrandService.delete(id);
        if (count == 1){
            return CommonResult.success(null);
        }else {
            return CommonResult.failed();
        }
    }


    // 更新品牌
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable("id") Long id,
                               @Validated @RequestBody PmsBrand pmsBrand){
        CommonResult commonResult;
        System.out.println(id);
        System.out.println(pmsBrand.toString());
        int count = pmsBrandService.update(id,pmsBrand);
        if (count==1){
            commonResult =  CommonResult.success(count);
        }else {
            commonResult =  CommonResult.failed();
        }
        return commonResult;
    }


    // 根据id查找品牌
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsBrand> findBrandById(@PathVariable("id") Long id){
        CommonResult commonResult;
        if (pmsBrandService.findBrandById(id)!=null){
            commonResult =  CommonResult.success(pmsBrandService.findBrandById(id));
        }else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
}

2.根据定义好的功能定义PmsBrandService接口

public interface PmsBrandService {

    List<PmsBrand> listAllBrand();

    int create(PmsBrand pmsBrand);

    int delete(Long id);

    int update(Long id, PmsBrand pmsBrand);

    PmsBrand findBrandById(Long id);
}

3.写PmsBrandServiceImpl实现接口

@Service
public class PmsBrandServiceImpl implements PmsBrandService {

    @Autowired
    private PmsBrandMapper pmsBrandMapper;


    @Override
    public List<PmsBrand> listAllBrand() {
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        return pmsBrandMapper.selectByExample(pmsBrandExample);
    }

    @Override
    public int create(PmsBrand pmsBrand) {
        return pmsBrandMapper.insertSelective(pmsBrand);
    }

    @Override
    public int delete(Long id) {
        return pmsBrandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Long id, PmsBrand pmsBrand) {
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.createCriteria().andIdEqualTo(id);
        return pmsBrandMapper.updateByExampleSelective(pmsBrand, pmsBrandExample);
    }


    @Override
    public PmsBrand findBrandById(Long id) {
        return pmsBrandMapper.selectByPrimaryKey(id);
    }
    
}

4.利用PostMan进行测试
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值