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进行测试