前端到后端流程
项目和包:
先写dao
import com.changgou.goods.pojo.Brand;
import tk.mybatis.mapper.common.Mapper;
public interface BrandMapper extends Mapper<Brand>{
}
service
import com.changgou.goods.pojo.Brand;
import com.github.pagehelper.PageInfo;
import java.util.List;
public interface BrandService {
public List<Brand> findAll();
public Brand findById(Integer id);
public void addBrand(Brand brand);
public void updateBrand(Brand brand);
public void deleteBrandById(Integer id);
public List<Brand> findList(Brand brand);
public PageInfo<Brand> findPage(Integer page,Integer size);
public PageInfo<Brand> findPage(Brand brand,Integer page,Integer size);
}
impl
import com.changgou.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
@Service
public class BrandServiceImpl implements BrandService
{
@Autowired
private BrandMapper brandMapper;
@Override
public List<Brand> findAll() {
return brandMapper.selectAll();
}
@Override
public Brand findById(Integer id) {
return brandMapper.selectByPrimaryKey(id);
}
@Override
public void addBrand(Brand brand) {
brandMapper.insertSelective(brand);
}
@Override
public void updateBrand(Brand brand) {
brandMapper.updateByPrimaryKeySelective(brand);
}
@Override
public void deleteBrandById(Integer id) {
brandMapper.deleteByPrimaryKey(id);
}
public List<Brand> findList1(Brand brand) {
Example example = new Example(Brand.class);
Example.Criteria criteria = example.createCriteria();
if (brand!=null){
if (!StringUtils.isEmpty(brand.getName())){
criteria.andLike("name","%"+brand.getName()+"%");
}
if (!StringUtils.isEmpty(brand.getLetter())){
criteria.andEqualTo("letter",brand.getLetter());
}
}
return brandMapper.selectByExample(example);
}
public Example createExample(Brand brand){
Example example = new Example(Brand.class);
Example.Criteria criteria = example.createCriteria();
if (brand!=null){
if (!StringUtils.isEmpty(brand.getName())){
criteria.andLike("name","%"+brand.getName()+"%");
}
if (!StringUtils.isEmpty(brand.getLetter())){
criteria.andEqualTo("letter",brand.getLetter());
}
}
return example;
}
@Override
public List<Brand> findList(Brand brand) {
Example example = createExample(brand);
return brandMapper.selectByExample(example);
}
@Override
public PageInfo<Brand> findPage(Integer page, Integer size) {
PageHelper.startPage(page,size);
List<Brand> brands = brandMapper.selectAll();
return new PageInfo<Brand>(brands);
}
@Override
public PageInfo<Brand> findPage(Brand brand, Integer page, Integer size) {
PageHelper.startPage(page,size);
Example example = createExample(brand);
List<Brand> brands = brandMapper.selectByExample(example);
return new PageInfo<Brand>(brands);
}
}
controller
import com.changgou.goods.pojo.Brand;
import com.changgou.service.BrandService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(value = "/brand")
@CrossOrigin
public class BrandController {
@Autowired
private BrandService brandService;
@GetMapping
public Result<List<Brand>> findAll(){
List<Brand> brands = brandService.findAll();
return new Result<List<Brand>>(true, StatusCode.OK,"品牌集合查询成功",brands);
}
@GetMapping(value = "/{id}")
public Result<Brand> findById(@PathVariable(value = "id") Integer id){
Brand byId = brandService.findById(id);
return new Result<Brand>(true, StatusCode.OK,"品牌id查询成功",byId);
}
@PostMapping
public Result addBrand(@RequestBody Brand brand){
brandService.addBrand(brand);
return new Result(true, StatusCode.OK,"新增品牌查询成功");
}
@PutMapping(value = "/{id}")
public Result updateBrand(@PathVariable(value = "id")Integer id,@RequestBody Brand brand){
brand.setId(id);
brandService.updateBrand(brand);
return new Result(true,StatusCode.OK,"修改成功");
}
@DeleteMapping(value = "/{id}")
public Result deleteBrandById(@PathVariable(value = "id")Integer id){
brandService.deleteBrandById(id);
return new Result(true,StatusCode.OK,"删除品牌成功");
}
@PostMapping(value = "/search")
public Result<List<Brand>> findList(@RequestBody Brand brand){
List<Brand> list = brandService.findList(brand);
return new Result<List<Brand>>(true, StatusCode.OK,"条件搜索查询成功",list);
}
@GetMapping(value = "/search/{page}/{size}")
public Result<PageInfo<Brand>> findPage(@PathVariable(value = "page")Integer page,
@PathVariable(value = "size")Integer size){
PageInfo<Brand> pageInfo = brandService.findPage(page, size);
return new Result<PageInfo<Brand>>(true,StatusCode.OK,"分页查询成功",pageInfo);
}
@PostMapping(value = "/search/{page}/{size}")
public Result<PageInfo<Brand>> findPage(@RequestBody Brand brand,@PathVariable(value = "page")Integer page,
@PathVariable(value = "size")Integer size){
PageInfo<Brand> pageInfo = brandService.findPage(brand,page, size);
return new Result<PageInfo<Brand>>(true,StatusCode.OK,"条件分页查询成功",pageInfo);
}
}
全局异常处理
import entity.Result;
import entity.StatusCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class GloabalExceptionHandler {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result error(Exception e) {
e.printStackTrace();
return new Result(false, StatusCode.ERROR, e.getMessage());
}
}