MybatisPlus学习记录
1、导入mybatisplus依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
2、mapper接口(dao层)继承BaseMapper<实体类>
3、基本CRUD
1.selectById():按照主键查询
2.selectList(null):全查
3.insert(entity):添加。
报错:Could not set property ‘id’ of ‘class com.badao.beans.Employee’ with value
问题原因:javaType和jdbcType的类型不匹配
解决方案:
在实体类添加主键策略以及指定表名
实体类主键id属性添加
@TableId(value = “id”,type = IdType.AUTO)
insert添加主键自动回填
4.selectBatchIds(list):按id列表查询,参数为list集合
5. selectByMap():条件查询
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put(“password”,“123”);//map中存放的是表中的列名,不是类中的属性名
List userList = userMapper.selectByMap(hashMap);
6.updateById(entity):按照id修改
-----------------------------------------------------
7.update(entity,Wrapper):通过条件查询到,把查询到的记录做修改
User user1 = new User(); user1.setPassword("888"); QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(); userQueryWrapper.like("password","7"); List<Map<String, Object>> maps = userMapper.selectMaps(userQueryWrapper); System.out.println(maps); int update = userMapper.update(user1, userQueryWrapper);
8.模糊查询
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(); userQueryWrapper.like("password","7"); List<Map<String, Object>> maps = userMapper.selectMaps(userQueryWrapper);
9.删除
userMapper.delete(Wrapper):按条件删除(模糊,between,eq…)
deleteBatchIds(Arraylist):删除集合内的id数据
deleteById():根据id删除
userMapper.deleteByMap(Map):按条件删除