mybatis-plus 基本CRUD

基本数据库的表为student:
在这里插入图片描述
使用的数据库和相关代码见前置

增加

    @Autowired
    private StudentDao studentDao;
    @Test
    void test() {
    
    
       student s = new student();
       s.setId(1005);
       s.setName("Kim");
       s.setAge("12");
       studentDao.insert(s);
    }

在这里插入图片描述

删除

在这里插入图片描述
最简单的就是删除通过id:

    @Autowired
    private StudentDao studentDao;
    @Test
    void test() {
    
    
       studentDao.deleteById(1004);
    }

QueryWrapper方法:
queryWrapper.lt()——小于
queryWrapper.le()——小于等于
queryWrapper.gt()——大于
queryWrapper.ge()——大于等于
queryWrapper.eq()——等于
queryWrapper.ne()——不等于
queryWrapper.betweeen(“age”,10,20)——age在值10到20之间
queryWrapper.notBetweeen(“age”,10,20)——age不在值10到20之间
queryWrapper.like(“属性”,“值”)——模糊查询匹配值‘%值%’
queryWrapper.notLike(“属性”,“值”)——模糊查询不匹配值‘%值%’
queryWrapper.likeLeft(“属性”,“值”)——模糊查询匹配最后一位值‘%值’
queryWrapper.likeRight(“属性”,“值”)——模糊查询匹配第一位值‘值%’
queryWrapper.isNull()——值为空或null
queryWrapper.isNotNull()——值不为空或null
queryWrapper.in(“属性”,条件,条件 )——符合多个条件的值
queryWrapper.notIn(“属性”,条件,条件 )——不符合多个条件的值
queryWrapper.or()——或者
queryWrapper.and()——和
queryWrapper.orderByAsc(“属性”)——根据属性升序排序
queryWrapper.orderByDesc(“属性”)——根据属性降序排序
queryWrapper.inSql(“sql语句”)——符合sql语句的值
queryWrapper.notSql(“sql语句”)——不符合SQL语句的值
queryWrapper.esists(“SQL语句”)——查询符合SQL语句的值
queryWrapper.notEsists(“SQL语句”)——查询不符合SQL语句的值

QueryWrapper方法删除:

    @Autowired
    private StudentDao studentDao;

    @Test
    void test() {
    
    
        QueryWrapper<student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name","1");
        studentDao.delete(queryWrapper);
    }

查找

普通查询

    @Autowired
    private StudentDao studentDao;
    @Test
    void selectById() {
    
    
        studentDao.selectById(1000);
    }

在这里插入图片描述
批量查询

@SpringBootTest
class Spring1ApplicationTests {
    
    

    @Autowired
    private StudentDao studentDao;
    @Test
    void selectByIds() {
    
    
        List<Integer> ids = Arrays.asList(1000,1002);
        List<student> st = studentDao.selectBatchIds(ids);
    }
}

在这里插入图片描述
条件查询
查询通过姓名:

    @Autowired
    private StudentDao studentDao;
    @Test
    void selectByName() {
    
    
        QueryWrapper<student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name","Li Ming");
        List<student> userInfoList = studentDao.selectList(queryWrapper);
    }

在这里插入图片描述
限定获取的内容:

    @Autowired
    private StudentDao studentDao;

    @Test
    void test() {
    
    
        QueryWrapper<student> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","name");
        queryWrapper.eq("name","Li Ming");
        studentDao.selectList(queryWrapper);
    }

在这里插入图片描述
查询投影:查询投影视频
看到一个博客总结的查询很全,可以查看查询方法
在这里插入图片描述
简化如下面的:
在这里插入图片描述

更新

在这里插入图片描述
通过Id更新:

    @Autowired
    private StudentDao studentDao;

    @Test
    void test() {
    
    
        student s = new student();
        s.setId(1005);
        s.setAge("19");
        studentDao.updateById(s);
    }

通过名字修改:

    @Autowired
    private StudentDao studentDao;

    @Test
    void test() {
    
    
        QueryWrapper<student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name","Kim");
        student s = new student();
        s.setAge("16");
        studentDao.update(s,queryWrapper);
    }

猜你喜欢

转载自blog.csdn.net/weixin_43788986/article/details/127089896