packagecn.itcast.mp;importcn.itcast.mp.mapper.UserMapper;importcn.itcast.mp.pojo.User;importcom.baomidou.mybatisplus.core.conditions.query.QueryWrapper;importcom.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;importcom.baomidou.mybatisplus.core.metadata.IPage;importcom.baomidou.mybatisplus.extension.plugins.pagination.Page;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;importjava.util.Arrays;importjava.util.HashMap;importjava.util.List;importjava.util.Map;@RunWith(SpringRunner.class)@SpringBootTestpublicclassTestUserMapper{@AutowiredprivateUserMapper userMapper;@TestpublicvoidtestInsert(){User user =newUser();
user.setMail("2@itcast.cn");
user.setAge(301);
user.setUserName("caocao1");
user.setName("曹操1");
user.setPassword("123456");
user.setAddress("北京");int result =this.userMapper.insert(user);//result数据库受影响的行数System.out.println("result => "+ result);//获取自增长后的id值, 自增长后的id值会回填到user对象中System.out.println("id => "+ user.getId());}@TestpublicvoidtestSelectById(){User user =this.userMapper.selectById(2L);User byId = userMapper.findById(2l);System.out.println(byId);}@TestpublicvoidtestUpdateById(){User user =newUser();
user.setId(1L);//条件,根据id更新
user.setAge(19);//更新的字段
user.setPassword("666666");int result =this.userMapper.updateById(user);System.out.println("result => "+ result);}@TestpublicvoidtestUpdate(){User user =newUser();
user.setAge(20);//更新的字段
user.setPassword("8888888");QueryWrapper<User> wrapper =newQueryWrapper<>();
wrapper.eq("user_name","zhangsan");//匹配user_name = zhangsan 的用户数据//根据条件做更新int result =this.userMapper.update(user, wrapper);System.out.println("result => "+ result);}@TestpublicvoidtestUpdate2(){UpdateWrapper<User> wrapper =newUpdateWrapper<>();
wrapper.set("age",21).set("password","999999")//更新的字段.eq("user_name","zhangsan");//更新的条件//根据条件做更新int result =this.userMapper.update(null, wrapper);System.out.println("result => "+ result);}@TestpublicvoidtestDeleteById(){// 根据id删除数据int result =this.userMapper.deleteById(9L);System.out.println("result => "+ result);}@TestpublicvoidtestDeleteByMap(){Map<String,Object> map =newHashMap<>();
map.put("user_name","zhangsan");
map.put("password","999999");// 根据map删除数据,多条件之间是and关系int result =this.userMapper.deleteByMap(map);System.out.println("result => "+ result);}@TestpublicvoidtestDelete(){//用法一:// QueryWrapper<User> wrapper = new QueryWrapper<>();// wrapper.eq("user_name", "caocao1")// .eq("password", "123456");//用法二:User user =newUser();
user.setPassword("123456");
user.setUserName("caocao");QueryWrapper<User> wrapper =newQueryWrapper<>(user);// 根据包装条件做删除int result =this.userMapper.delete(wrapper);System.out.println("result => "+ result);}@TestpublicvoidtestDeleteBatchIds(){// 根据id批量删除数据String[] str=newString[2];
str[0]="6";
str[1]="7";int result =this.userMapper.deleteBatchIds(Arrays.asList(str));int result2 =this.userMapper.deleteBatchIds(Arrays.asList(9L,10L));System.out.println("result => "+ result);}@TestpublicvoidtestSelectBatchIds(){// 根据id批量查询数据List<User> users =this.userMapper.selectBatchIds(Arrays.asList(2L,3L,4L,100L));for(User user : users){System.out.println(user);}}@TestpublicvoidtestSelectOne(){QueryWrapper<User> wrapper =newQueryWrapper<>();//查询条件
wrapper.eq("password","123456");// 查询的数据超过一条时,会抛出异常User user =this.userMapper.selectOne(wrapper);System.out.println(user);}@TestpublicvoidtestSelectCount(){QueryWrapper<User> wrapper =newQueryWrapper<>();// wrapper.gt("age", 20); // 条件:年龄大于20岁的用户
wrapper.lt("age",21);//条件:年龄小于21岁的用户// 根据条件查询数据条数Integer count =this.userMapper.selectCount(wrapper);//根据条件查询数据集合List<User> users =this.userMapper.selectList(wrapper);System.out.println("count => "+ count);System.out.println(users);}@TestpublicvoidtestSelectList(){QueryWrapper<User> wrapper =newQueryWrapper<>();//设置查询条件
wrapper.like("email","itcast");List<User> users =this.userMapper.selectList(wrapper);for(User user : users){System.out.println(user);}}// 测试分页查询@TestpublicvoidtestSelectPage(){Page<User> page =newPage<>(1,3);//查询第一页,查询1条数据QueryWrapper<User> wrapper =newQueryWrapper<>();//设置查询条件
wrapper.like("email","itcast");IPage<User> iPage =this.userMapper.selectPage(page, wrapper);System.out.println("数据总条数: "+ iPage.getTotal());System.out.println("数据总页数: "+ iPage.getPages());System.out.println("当前页数: "+ iPage.getCurrent());List<User> records = iPage.getRecords();// for (User record : records) {// System.out.println(record);// }System.out.println(records);}/**
* 自定义的方法
*/@TestpublicvoidtestFindById(){User user =this.userMapper.findById(2L);System.out.println(user);}@TestpublicvoidtestAllEq(){Map<String,Object> params =newHashMap<>();
params.put("name","李四");
params.put("age","20");
params.put("password",null);QueryWrapper<User> wrapper =newQueryWrapper<>();//SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE password IS NULL AND name = ? AND age = ?// wrapper.allEq(params);//SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE name = ? AND age = ?// wrapper.allEq(params, false);//SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE age = ?// wrapper.allEq((k, v) -> (k.equals("age") || k.equals("id")) , params);//SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE name = ? AND age = ?
wrapper.allEq((k, v)->(k.equals("age")|| k.equals("id")|| k.equals("name")), params);List<User> users =this.userMapper.selectList(wrapper);for(User user : users){System.out.println(user);}}@TestpublicvoidtestEq(){QueryWrapper<User> wrapper =newQueryWrapper<>();//SELECT id,user_name,password,name,age,email FROM tb_user WHERE password = ? AND age >= ? AND name IN (?,?,?)
wrapper.eq("password","123456").ge("age",20).in("name","李四","王五","赵六");List<User> users =this.userMapper.selectList(wrapper);for(User user : users){System.out.println(user);}}@TestpublicvoidtestLike(){QueryWrapper<User> wrapper =newQueryWrapper<>();// SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE name LIKE ?// 参数:%五(String)
wrapper.likeLeft("name","五");List<User> users =this.userMapper.selectList(wrapper);for(User user : users){System.out.println(user);}}@TestpublicvoidtestOrderByAgeDesc(){QueryWrapper<User> wrapper =newQueryWrapper<>();//按照年龄倒序排序// SELECT id,user_name,name,age,email AS mail FROM tb_user ORDER BY age DESC
wrapper.orderByDesc("age");List<User> users =this.userMapper.selectList(wrapper);for(User user : users){System.out.println(user);}}@TestpublicvoidtestOr(){QueryWrapper<User> wrapper =newQueryWrapper<>();// SELECT id,user_name,name,age,email AS mail FROM tb_user WHERE name = ? OR age = ?
wrapper.eq("name","王五").or().eq("age",21);List<User> users =this.userMapper.selectList(wrapper);for(User user : users){System.out.println(user);}}@TestpublicvoidtestSelect(){QueryWrapper<User> wrapper =newQueryWrapper<>();//SELECT id,name,age FROM tb_user WHERE name = ? OR age = ?
wrapper.eq("name","王五").or().eq("age",21).select("id","name","age");//指定查询的字段List<User> users =this.userMapper.selectList(wrapper);for(User user : users){System.out.println(user);}}}