• myabtis封装的mapper


    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     }
    当一个男人不再对你啰嗦,不再缠着你,不再没事找你,对你说话也客气了,也不再气你了。那么恭喜你,你已经成功的失去了他。别嫌弃男人幼稚,那是他喜欢你,爱你。女人说男人像小孩子一样不成熟,可又有谁知道,男人在自己喜欢的女人面前才像小孩子,如果不喜欢你了,不爱你了,他比你爸还成熟。
  • 相关阅读:
    在C#中实现Python的分片技术
    如何将松散的dll打包进需要发布的exe
    Python基础学习(第8天)
    javascript中 的 + RegExp['x241'] 怎么理解
    CSS和JavaScript标签style属性对照表
    浏览器 怪异模式(Quirks Mode) 与 标准模式(Standards Mode)
    Javascript如何判断一个变量是数字类型?
    我的第一个jquery插件:下拉多选框
    javascript中===与==
    ztree高级实例(原创)
  • 原文地址:https://www.cnblogs.com/fengtangjiang/p/11117006.html
Copyright © 2020-2023  润新知