栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot学习--spring boot整合mybaties-plus的数据擦操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot学习--spring boot整合mybaties-plus的数据擦操作

说先说一下application.yml和application.properties区别:

接下来说一下我们之前创建好的并且抱起来的springboot工程,我们的端口号设置成7099,然后我们在第一次运行成功打开的时候是这个样子:

这是因为这个项目没有映射!也就是我们需要写一个映射也就Mapping, 接下来我们新建一个controller层里面写一个类,然后去写映射:
package com.example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class RootApp {
    @GetMapping(value = "/")
    public String Hello(){
        return "Hello 范涛之";
    }

}

再次启动这个项目:

这里就成功了 接下来说一下,怎样去使用mybaties连接数据库: 需要新建实体层·,mapper层,然后实现:

仿照写一个新增操作:
    @GetMapping(value = "/Insert")
    public void Insert(){
        User user = new User();
        user.setName("ftz");
        user.setAge(20);
        user.setEmail("2831826106@qq.com");
        int result = userMapper.insert(user);
        System.out.println(result);
        System.out.println(user);

    }


刚刚上面设置的是唯一ID,这里设置为主键自增:


接下来说一下@GetMapping和@PostMapping
  • @GetMapping用于将HTTP get请求映射到特定处理程序的方法注解

具体来说,@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。

  • @PostMapping用于将HTTP post请求映射到特定处理程序的方法注解

具体来说,@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。

更新操作:使用@GetMapping:
    @GetMapping(value = "/Update")
    public void Update(){
        User user = new User();
        user.setId(1L);
        user.setAge(60);
        user.setName("王老五");
        int result = userMapper.updateById(user);
        System.out.println(result);
    }

接下来是乐观锁:


第一次修改成功第二次失败·: 根据id查询记录,通过多个id批量查询,简单的条件查询:
    @GetMapping(value = "/SelectById")
    public  void  SelectById(){
        User user = userMapper.selectById(1);
        System.out.println(user);
    }
    
    @GetMapping(value = "/SelectByIdS")
    public  void  SelectByIds(){
       List users = userMapper.selectBatchIds(Arrays.asList(1,2,3));
       users.forEach(System.out::println);
    }

    
    @GetMapping(value = "/SelectByMap")
    public  void  SelectByMap(){
        HashMap map = new HashMap<>();
        map.put("name","ftz");
        map.put("age","20");
        List users = userMapper.selectByMap(map);
        users.forEach(System.out::println);
    }



分页:
    

    @GetMapping(value = "/SelectPage")
    public  void  SelectPage(){
        Page page = new Page<>(1,5);
        userMapper.selectPage(page,null);
        page.getRecords().forEach(System.out::println);
        System.out.println(page.getCurrent());
        System.out.println(page.getPages());
        System.out.println(page.getSize());
        System.out.println(page.getTotal());
        System.out.println(page.hasNext());
        System.out.println(page.hasPrevious());
    }

    
    @GetMapping(value = "/testSelectMapsPage")
    public  void  testSelectMapsPage(){
        Page page = new Page<>(1,5);
        IPage> mapIPage = userMapper.selectMapsPage(page, null);
        //注意:此行必须使用 mapIPage 获取记录列表,否则会有数据类型转换错误
        mapIPage.getRecords().forEach(System.out::println);
        System.out.println(page.getCurrent());
        System.out.println(page.getPages());
        System.out.println(page.getSize());
        System.out.println(page.getTotal());
        System.out.println(page.hasNext());
        System.out.println(page.hasPrevious());
    }


删除:
 

    @DeleteMapping (value = "/SelectByMap")
    public  void  DeleteById(){
        int result = userMapper.deleteById(4L);
        System.out.println(result);
    }

    

    @DeleteMapping (value = "/DeleteBatchIds")
    public  void  DeleteBatchIds(){
        int result = userMapper.deleteBatchIds(Arrays.asList(1, 2, 3));
        System.out.println(result);
    }

    
    @DeleteMapping (value = "/DeleteByMap")
    public  void  DeleteByMap(){
        HashMap map = new HashMap<>();
        map.put("name", "ftz");
        map.put("age", 20);
        int result = userMapper.deleteByMap(map);
        System.out.println(result);
    }



逻辑删除:
  
    @GetMapping (value = "/LogicDelete")
    public  void  LogicDelete(){
        int result = userMapper.deleteById(8L);
        System.out.println(result);
    }
    
    @GetMapping (value = "/LogicDeleteSelect")
    public  void  LogicDeleteSelect(){
        User user = new User();
        List users = userMapper.selectList(null);
        users.forEach(System.out::println);
    }



性能分析: 首先我们设置100毫秒内插入数据:
  
    @GetMapping (value = "/Performance")
    public  void  Performance(){
        User user = new User();
        user.setName("哈哈哈");
        user.setEmail("he468n@sina.com");
        user.setAge(58);
        userMapper.insert(user);
    }

设置为1毫秒后:

这就是简单的spring boot整合mybaties-plus
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/690751.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号