目录
一、添加
1、添加
二、修改
1、根据id修改
2、根据条件修改
三、删除
1、根据id删除
2、根据条件删除deleteByMap
3、根据id批量删除
4、根据条件删除delete
四、查询
1、根据id查询
2、根据id批量查询
3、查询一条数据
4、查询数据条数
5、根据条件查询数据总条数
6、根据条件查询集合
7、分页查询
//添加
@Test
void insert() {
User user = new User();
user.setAge(20);
user.setEmail("[email protected]");
user.setName("小黑子");
user.setPassword("ctrl");
user.setUserName("haha");
// //返回的result是受影响的行数,并不是自增后的id
int result = userMapper.insert(user);
System.out.println(result);
}
//根据id修改
@Test
void updateById() {
User user = new User();
user.setId(1L);
user.setAge(22);
user.setName("坤坤");
int result = userMapper.updateById(user);
System.out.println(result);
}
//根据条件修改
@Test
void update() {
User user = new User();
user.setAge(22);
user.setName("坤坤");
user.setPassword("1111");
//添加条件
QueryWrapper wrapper = new QueryWrapper<>();
//根究 字段user_name 值为zhangsan进行更新操作
wrapper.eq("user_name","zhangsan");
//根据条件更新
int result = userMapper.update(user,wrapper);
System.out.println(result);
}
//根据id删除
@Test
void deleteById() {
//根据id删除
int result = userMapper.deleteById(2L);
System.out.println(result);
}
//根据条件删除
@Test
public void testDeleteByMap() {
Map columnMap = new HashMap<>();
columnMap.put("age",20);
columnMap.put("user_name","haha");
//将columnMap中的元素设置为删除的条件,多个之间为and关系
int result = this.userMapper.deleteByMap(columnMap);
System.out.println("result = " + result);
}
//根据id批量删除
@Test
public void deleteBatchIds() {
//根据id批量删除
int result = this.userMapper.deleteBatchIds(Arrays.asList(6L,7L));
System.out.println("result = " + result);
}
//删除delete
@Test
public void delete() {
//方式一
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_name","11")
.eq("password","11");
//方式二
User user = new User();
user.setUserName("11");
user.setPassword("11");
QueryWrapper queryWrapper1 = new QueryWrapper<>(user);
//根据id批量删除
int result = this.userMapper.delete(queryWrapper1);
System.out.println("result = " + result);
}
//根据id查询
@Test
public void selectById() {
//根据id查询
User user = this.userMapper.selectById(1L);
System.out.println("result = " + user);
}
//根据id批量查询
@Test
public void selectBatchIds() {
//根据id批量查询
List userList = this.userMapper.selectBatchIds(Arrays.asList(1L, 3L));
System.out.println("result = " + userList);
}
//查询一条数据
@Test
public void selectOne() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_name","11");
//查询一条,如果多余1条会报异常
User user = this.userMapper.selectOne(queryWrapper);
System.out.println("result = " + user);
}
//查询数据条数
@Test
public void selectCount1() {
QueryWrapper queryWrapper = new QueryWrapper<>();
//查询数据总条数
Integer integer = this.userMapper.selectCount(queryWrapper);
System.out.println("result = " + integer);
}
//根据条件查询数据总条数
@Test
public void selectCount2() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.gt("age",20);//查询年龄大于20的条数
//根据条件查询数据总条数
Integer integer = this.userMapper.selectCount(queryWrapper);
System.out.println("result = " + integer);
}
//根据条件查询集合
@Test
public void selectList() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.gt("age",20);//查询年龄大于20的条数
//根据条件查询集合
List userList = this.userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
创建分页插件
@Configuration
@MapperScan("com.mybatisplus.mapper")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
查询
//分页查询
@Test
public void selectPage() {
//创建分页对象
Page page = new Page<>(2,2);
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.like("email","itcast");//模糊查询
IPage iPage = this.userMapper.selectPage(page, queryWrapper);
System.out.println("数据总条数:" + iPage.getTotal());
System.out.println("数据总页数:" + iPage.getPages());
System.out.println("当前页数:" + iPage.getCurrent());
System.out.println("当前分页总页数:" + iPage.getSize());
System.out.println("分页对象记录列表:" + iPage.getRecords());
List records = iPage.getRecords();
//根据条件查询集合
records.forEach(System.out::println);
}