/**
* 插入一条记录
* int insert(T entity);
*/
@Test
void insert() {
System.out.println(("----- insert method test ------"));
User user = new User();
user.setAge(18);
user.setName("嘻哈");
user.setEmail("[email protected]");
int i = userMapper.insert(user);
}
- 删除
/**
*根据 ID 删除
* int deleteById(Serializable id);
*/
@Test
void deleteById() {
System.out.println(("----- deleteById method test ------"));
userMapper.deleteById("1");
}
/**
* 删除(根据ID 批量删除)
* int deleteBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList);
*/
@Test
void deleteBatchIds() {
System.out.println(("----- deleteBatchIds method test ------"));
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(3);
userMapper.deleteBatchIds(list);
}
/**
* 根据 columnMap 条件,删除记录
* int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);
*/
@Test
void deleteByMap() {
System.out.println(("----- deleteByMap method test ------"));
Map<String,Object> map = new HashMap();
map.put("name","Jone");
userMapper.deleteByMap(map);
}
/**
* 根据 entity 条件,删除记录
* int delete(@Param(Constants.WRAPPER) Wrapper wrapper);
*/
@Test
void delete() {
System.out.println(("----- delete method test ------"));
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","Jack");
userMapper.delete(wrapper);
}
- 修改
/**
*根据 whereEntity 条件,更新记录
* int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper);
*/
@Test
void update() {
System.out.println(("----- update method test ------"));
User user = new User();
user.setName("赵云");
UpdateWrapper<User> wrapper = new UpdateWrapper<>();
wrapper.eq("age","30");
userMapper.update(user,wrapper);
}
/**
*根据 ID 修改
*int updateById(@Param(Constants.ENTITY) T entity);
*/
@Test
void updateById() {
System.out.println(("----- updateById method test ------"));
User user = new User();
user.setName("黄忠");
user.setAge(60);
user.setId(4L);
userMapper.updateById(user);
}
- 查询
/**
* 根据 ID 查询
* T selectById(Serializable id);
*/
@Test
void selectById(){
System.out.println(("----- selectById method test ------"));
User user = userMapper.selectById(4);
System.out.println(user);
}
/**
* 根据 entity 条件,查询一条记录
* T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);
*/
@Test
void selectOne(){
System.out.println(("----- selectOne method test ------"));
QueryWrapper<User> wrapper = new QueryWrapper<>();
QueryWrapper<User> age = wrapper.eq("age", "60");//查询年龄为60的一个人
User user = userMapper.selectOne(age);
System.out.println(user);
}
/**
* // 查询(根据ID 批量查询)
* List selectBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList);
*/
@Test
void selectBatchIds(){
System.out.println(("----- selectBatchIds method test ------"));
List<Integer> list = new ArrayList<>();
list.add(4);
list.add(8);
list.add(5);
List<User> userList = userMapper.selectBatchIds(list);
System.out.println(userList);
}
/**
* // 根据 entity 条件,查询全部记录
* List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);
*/
@Test
void selectList(){
System.out.println(("----- selectList method test ------"));
QueryWrapper<User> wrapper = new QueryWrapper<>();
QueryWrapper<User> age = wrapper.ge("age", "24");
List<User> userList = userMapper.selectList(age);//传null查全部
System.out.println(userList);
}
/**
* // 查询(根据 columnMap 条件)
* List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap);
*/
@Test
void selectByMap() {
System.out.println(("----- selectByMap method test ------"));
Map<String,Object> map = new HashMap<>();
map.put("name","黄忠");
List<User> userList = userMapper.selectByMap(map);
System.out.println(userList);
}
/**
* // 根据 Wrapper 条件,查询全部记录
* List
@Test
void selectMaps(){
System.out.println(("----- selectMaps method test ------"));
QueryWrapper<User> wrapper = new QueryWrapper<>();
QueryWrapper<User> age = wrapper.ge("age", "24");
List<User> userList = userMapper.selectList(age);//传null查全部
System.out.println(userList);
}
/**
* // 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
* List
@Test
void selectObjs(){
System.out.println(("----- selectObjs method test ------"));
QueryWrapper<User> wrapper = new QueryWrapper<>();
QueryWrapper<User> name = wrapper.eq("name", "黄忠");
List<Object> objectList = userMapper.selectObjs(name);
System.out.println(objectList);//第一个字段是id
}
/**
* // 根据 entity 条件,查询全部记录(并翻页)
* IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);
*/
@Test
void selectPage(){
System.out.println(("----- selectPage method test ------"));
Page<User> page = userMapper.selectPage(new Page<>(1, 2), null);
System.out.println("当前页数:" + page.getCurrent());
System.out.println("每页条数:" + page.getSize());
System.out.println("总条数:" + page.getTotal());
System.out.println("查询数据:" + page.getRecords().size());
}
/**
* // 根据 Wrapper 条件,查询全部记录(并翻页)
* IPage
@Test
void selectMapsPage() {
System.out.println(("----- selectMapsPage method test ------"));
Page<Map<String, Object>> page = userMapper.selectMapsPage(new Page<>(1, 2), null);
System.out.println("当前页数:" + page.getCurrent());
System.out.println("每页条数:" + page.getSize());
System.out.println("总条数:" + page.getTotal());
System.out.println("查询数据:" + page.getRecords().size());
}
/**
* // 根据 Wrapper 条件,查询总记录数
* Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);
*/
@Test
void selectCount() {
System.out.println(("----- selectCount method test ------"));
QueryWrapper<User> wrapper = new QueryWrapper<>();
QueryWrapper<User> age = wrapper.eq("age", 24);
Integer integer = userMapper.selectCount(age);
System.out.println(integer);
System.out.println("全部数据 : " + userMapper.selectCount(null));
}