本文主要内容是 Mapper接口的使用 示例。
注意:我们需要方法 执行的SQL 是什么样的,以及返回值。
Mapper CRUD 接口
新增
insert方法
==> Preparing: INSERT INTO user ( user_id, user_name, create_time ) VALUES ( ?, ?, ? )
==> Parameters: halo(String), 哈喽啊(String), 2023-07-11 14:01:59.864(Timestamp)
<== Updates: 1
删除
delete
根据 entity 条件,删除记录
注意:queryWrapper后面可以跟条件构造器
@Test
public void delete() {QueryWrapper queryWrapper = new QueryWrapper<>();System.out.println(userDao.delete(queryWrapper));
}
==> Preparing: DELETE FROM user
==> Parameters:
<== Updates: 1
deleteBatchIds
删除(根据ID 批量删除)
@Test
public void deleteBatchIds() {List<Integer> deleteIds = new ArrayList<>();deleteIds.add(1);deleteIds.add(2);deleteIds.add(3);System.out.println(userDao.deleteBatchIds(deleteIds));
}
==> Preparing: DELETE FROM user WHERE id IN ( ? , ? , ? )
==> Parameters: 1(Integer), 2(Integer), 3(Integer)
<== Updates: 1
deleteById
根据 ID 删除
@Test
public void deleteById(){Integer id=1;System.out.println(userDao.deleteById(id));
}
执行的SQL语句
==> Preparing: DELETE FROM user WHERE id=?
==> Parameters: 1(Integer)
<== Updates: 0
deleteByMap
根据 columnMap 条件,删除记录
@Testpublic void deleteByMap() {Map<String,Object> params = new HashMap<>();params.put("user_id","halo");System.out.println(userDao.deleteByMap(params));}
==> Preparing: DELETE FROM user WHERE user_id = ?
==> Parameters: halo(String)
<== Updates: 0
修改
update
根据 whereWrapper 条件,更新记录
@Test
public void update() {User user = new User();user.setUserId("tony");QueryWrapper queryWrapper = new QueryWrapper();System.out.println(userDao.update(user,queryWrapper));
}
==> Preparing: UPDATE user SET user_id=?
==> Parameters: tony(String)
<== Updates: 1
updateById
根据 ID 修改
@Testpublic void updateById() {User user = new User();user.setUserId("halo");user.setUserName("哈喽啊");user.setCreateTime(new Date());user.setId(1);System.out.println(userDao.updateById(user));}
==> Preparing: UPDATE user SET user_id=?, user_name=?, create_time=? WHERE id=?
==> Parameters: halo(String), 哈喽啊(String), 2023-07-11 14:17:01.184(Timestamp), 1(Integer)
<== Updates: 0
查询
selectById
根据 ID 查询
@Testpublic void selectById() {Integer i = 1;System.out.println(userDao.selectById(i));}
==> Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE id=?
==> Parameters: 1(Integer)
<== Total: 0
selectOne
根据 entity 条件,查询一条记录
注意:如果 查询出来的记录是多条的话,会报错的哦
@Test
public void selectOne() {System.out.println(userDao.selectOne(new QueryWrapper<User>().eq("user_id","halo")));
}
==> Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE (user_id = ?)
==> Parameters: halo(String)
<== Total: 0
selectBatchIds
查询(根据ID 批量查询)
@Test
public void selectBatchIds() {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);userDao.selectBatchIds(list);
}
==> Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE id IN ( ? , ? )
==> Parameters: 1(Integer), 2(Integer)
<== Total: 0
selectList
根据 entity 条件,查询全部记录
@Test
public void selectList() {System.out.println(userDao.selectList(new QueryWrapper<>()));
}
==> Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user
==> Parameters:
<== Columns: id, user_id, user_name, create_time, dept_id
<== Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<== Row: 6, halo1, 哈喽啊2, 2023-07-11 14:35:42.0, null
<== Row: 7, halo3, 哈喽啊24, 2023-07-11 14:35:51.0, null
<== Total: 3
selectByMap
查询(根据 columnMap 条件)
注意:这里的返回值与selectMaps 返回值的区别哦
@Test
public void selectByMap() {Map<String,Object> params = new HashMap();params.put("user_id","halo");System.out.println(userDao.selectByMap(params));
}
==> Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user
==> Parameters:
<== Columns: id, user_id, user_name, create_time, dept_id
<== Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<== Row: 6, halo1, 哈喽啊2, 2023-07-11 14:35:42.0, null
<== Row: 7, halo3, 哈喽啊24, 2023-07-11 14:35:51.0, null
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@a451491]
[User(id=5, userId=tony, userName=哈喽啊, createTime=Tue Jul 11 14:11:20 CST 2023, deptId=null),
User(id=6, userId=halo1, userName=哈喽啊2, createTime=Tue Jul 11 14:35:42 CST 2023, deptId=null),
User(id=7, userId=halo3, userName=哈喽啊24, createTime=Tue Jul 11 14:35:51 CST 2023, deptId=null)]
selectMaps
查询(根据 columnMap 条件)
注意返回值哦
@Test
public void selectMaps() {List<Map<String, Object>> list = userDao.selectMaps(new QueryWrapper<>());System.out.println(list);
}
==> Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user
==> Parameters:
<== Columns: id, user_id, user_name, create_time, dept_id
<== Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<== Row: 6, halo1, 哈喽啊2, 2023-07-11 14:35:42.0, null
<== Row: 7, halo3, 哈喽啊24, 2023-07-11 14:35:51.0, null
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@a451491]
[{create_time=2023-07-11 14:11:20.0, user_id=tony, user_name=哈喽啊, id=5},{create_time=2023-07-11 14:35:42.0, user_id=halo1, user_name=哈喽啊2, id=6}, {create_time=2023-07-11 14:35:51.0, user_id=halo3, user_name=哈喽啊24, id=7}]
selectObjs
根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
@Test
public void selectObjs() {List<Object> list = userDao.selectObjs(new QueryWrapper<>());System.out.println(list);
}
==> Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user
==> Parameters:
<== Columns: id, user_id, user_name, create_time, dept_id
<== Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<== Row: 6, halo1, 哈喽啊2, 2023-07-11 14:35:42.0, null
<== Row: 7, halo3, 哈喽啊24, 2023-07-11 14:35:51.0, null
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7ceb4478]
[5, 6, 7]
selectPage
根据 entity 条件,查询全部记录(并翻页)
注意;这里我们需要配置一下分页哦
@Configuration
public class MyBatisPlusConfig {/*** 分页插件*/@Beanpublic PaginationInterceptor paginationInterceptor() {return new PaginationInterceptor();}
}
@Test
public void selectPage() {Page page = new Page();page.setCurrent(1);page.setPages(10);QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.eq("user_id","tony");IPage<Map<String, Object>> iPage = userDao.selectPage(page,queryWrapper);System.out.println(iPage.toString());}
JsqlParserCountOptimize sql=SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE (user_id = ?)
==> Preparing: SELECT COUNT(1) FROM user WHERE (user_id = ?)
==> Parameters: tony(String)
<== Columns: COUNT(1)
<== Row: 1
==> Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE (user_id = ?) LIMIT ?,?
==> Parameters: tony(String), 0(Long), 10(Long)
<== Columns: id, user_id, user_name, create_time, dept_id
<== Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<== Total: 1
selectMapsPage
根据 Wrapper 条件,查询全部记录(并翻页)
@Testpublic void selectMapsPage() {Page page = new Page();page.setCurrent(1);page.setPages(10);QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.eq("user_id","tony");IPage<Map<String, Object>> iPage = userDao.selectMapsPage(page,queryWrapper);System.out.println(iPage);}
JsqlParserCountOptimize sql=SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE (user_id = ?)
==> Preparing: SELECT COUNT(1) FROM user WHERE (user_id = ?)
==> Parameters: tony(String)
<== Columns: COUNT(1)
<== Row: 1
==> Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE (user_id = ?) LIMIT ?,?
==> Parameters: tony(String), 0(Long), 10(Long)
<== Columns: id, user_id, user_name, create_time, dept_id
<== Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<== Total: 1
selectCount
根据 Wrapper 条件,查询总记录数
@Test
public void selectCount() {System.out.println(userDao.selectCount(new QueryWrapper<>()));
}
==> Preparing: SELECT COUNT( 1 ) FROM user
==> Parameters:
<== Columns: COUNT( 1 )
<== Row: 3
<== Total: 1