1.全找findAll
@RestController @RequestMapping("/brand") public class BrandController { @Autowired private BrandService brandService; @GetMapping public Result findAll(){ List<Brand> list = brandService.findAll(); return new Result( true, StatusCode.OK,"查询成功",list ); } } public interface BrandService { /** * 查询全部数据 * @return */ List<Brand> findAll(); } @Service public class BrandServiceImpl implements BrandService { @Autowired private BrandMapper brandMapper; @Autowired private BrandMapper brandMAapper; public List<Brand> findAll{return brandMapper.selectAll();} /** * 品牌数据访问接口 */ public interface BrandMapper extends Mapper<Brand> { }
2.根据id找
1 @RestController 2 @RequestMapping("/brand") 3 public class BrandController { 4 @GetMapping("/id") 5 pbublic Result findById( @PathVariable Integer id){ 6 Brand brand = brandService.findById( id ); 7 return new Result( true,StatusCode.OK,"查询成功",brand ); 8 } 9 } 10 11 12 public interface BrandService { 13 Brand findById(Integer id); 14 } 15 16 @Service 17 public class BrandServiceImpl implements BrandService { 18 19 @Autowired 20 private BrandMapper brandMapper; @Override 21 public Brand findById(Integer id) { 22 return brandMapper.selectByPrimaryKey( id ); 23 } 24 25 26 import tk.mybatis.mapper.common.Mapper; 27 28 /** 29 * 品牌数据访问接口 30 */ 31 public interface BrandMapper extends Mapper<Brand> { 32 33 34 }
3.新增
1 /** 2 * 新增数据 3 * @param brand 4 * @return 5 */ 6 @PostMapping 7 public Result add( @RequestBody Brand brand){ 8 brandService.add( brand ); 9 return new Result(true,StatusCode.OK,"添加成功" ); 10 } 11 12 13 14 15 void add(Brand brand); 16 17 18 19 20 21 public void add (Brand brand){ 22 brandMapper.insert(brand);} 23 24 25 import tk.mybatis.mapper.common.Mapper; 26 27 /** 28 * 品牌数据访问接口 29 */ 30 public interface BrandMapper extends Mapper<Brand> { 31 32 33 }
4.修改
@PutMapping("/id") public Result update (@PathVariable Integer id,@RequestBody Brand brand){ brand.setId(id); brandService.update(brand); return new Result(true,StatueCode.Ok."修改成功"); } void update(Brand brand); public void update(Brand brand){ //brandMapper.update(brand);
brandMapper.updateByPrimaryKey(brand);
}
5.删除
1 @RequestMapping("/id") 2 public Result delete(@PathVariable Integer id){ 3 brandServie.delete(id); 4 return new Result (true,StatuesCode.OK,"删除成功"); 5 6 } 7 8 9 10 11 void delete(Integer id); 12 13 @Service 14 public void delete(Integer id){ 15 brandMapper.deleteByPrimaryKey(id); 16 }
6.根据条件查询
1 @GetMapping 2 public Result findList(@RequestParam Map<String,String>searchMap){ 3 List<Brand> list=brandService.findList(searchMap); 4 return new Result(ture,StatusCode.OK.list); 5 } 6 7 8 9 list<Brand> findList( Map<String,String> searchMap) 10 11 12 public List<Brand> findList(Map<String,String> searchMap){ 13 Example example=this.createEWxample(searchMap) 14 return brandMapper.selecctByExample(example); 15 } 16 17 private Example createExample(Map<String,String> searchMap){ 18 Example example=new Example(Brand.class); 19 Example.Criteria criteria=example.createCriteria(); 20 criteria.andLike("name","%"+searchMap.get("name")+"%"); 21 22 retuen example; 23 }
7.分页
@GetMapping("/page/{page}/{size}") public Result findPage(@PathVariable int page,@PathVariable int size ){ Page<Brand> brandPage = brandService.findPage( page, size ); PageResult pageResult=new PageResult( brandPage.getTotal(),brandPage.getResult() ); return new Result( true,StatusCode.OK,"查询成功",pageResult ); } Page<Brand> findPage(int page, int size); @Override public Page<Brand> findPage(int page, int size) { PageHelper.startPage( page,size ); return (Page<Brand>)brandMapper.selectAll(); }
8分页加条件
1 /** 2 * 条件+分页 3 * @param searchMap 查询条件 4 * @param page 页码 5 * @param size 页大小 6 * @return 7 */ 8 @GetMapping("/search/{page}/{size}") 9 public Result findPage( @RequestParam Map<String,String> searchMap, @PathVariable int page,@PathVariable int size ){ 10 Page<Brand> brandPage = brandService.findPage(searchMap, page, size ); 11 PageResult pageResult=new PageResult( brandPage.getTotal(),brandPage.getResult() ); 12 return new Result( true,StatusCode.OK,"查询成功",pageResult ); 13 } 14 15 /** 16 * 条件+分页 17 * @param searchMap 查询条件 18 * @param page 页码 19 * @param size 页大小 20 * @return 21 */ 22 Page<Brand> findPage(Map<String, String> searchMap, int page, int size); 23 24 25 @Override 26 public Page<Brand> findPage(Map<String, String> searchMap, int page, int size) { 27 PageHelper.startPage( page,size ); 28 Example example = createExample( searchMap ); 29 return (Page<Brand>)brandMapper.selectByExample( example ); 30 }