一、源碼解析:
/** * Mapper 繼承該接口后,無需編寫 mapper.xml 文件,即可獲得CRUD功能 * 這個 Mapper 支持 id 泛型*/ public interface BaseMapper<T> { /** * 插入一條記錄 * @param entity * 實體對象 * @return int */ Integer insert(T entity); /** * 根據 ID 刪除 * @param id * 主鍵ID * @return int */ Integer deleteById(Serializable id); /** * 根據 columnMap 條件,刪除記錄 * @param columnMap * 表字段 map 對象 * @return int */ Integer deleteByMap(@Param("cm") Map<String, Object> columnMap); /** * 根據 entity 條件,刪除記錄 * @param wrapper * 實體對象封裝操作類(可以為 null) * @return int */ Integer delete(@Param("ew") Wrapper<T> wrapper); /** * 刪除(根據ID 批量刪除) * @param idList * 主鍵ID列表 * @return int */ Integer deleteBatchIds(List<? extends Serializable> idList); /** * 根據 ID 修改 * @param entity * 實體對象 * @return int */ Integer updateById(T entity); /** * 根據 whereEntity 條件,更新記錄 * @param entity * 實體對象 * @param wrapper * 實體對象封裝操作類(可以為 null) * @return */ Integer update(@Param("et") T entity, @Param("ew") Wrapper<T> wrapper); /** * 根據 ID 查詢 * @param id * 主鍵ID * @return T */ T selectById(Serializable id); /** * 查詢(根據ID 批量查詢) * @param idList * 主鍵ID列表 * @return List<T> */ List<T> selectBatchIds(List<? extends Serializable> idList); /** * 查詢(根據 columnMap 條件) * @param columnMap * 表字段 map 對象 * @return List<T> */ List<T> selectByMap(@Param("cm") Map<String, Object> columnMap); /** * 根據 entity 條件,查詢一條記錄 * @param entity * 實體對象 * @return T */ T selectOne(@Param("ew") T entity); /** * 根據 Wrapper 條件,查詢總記錄數 * @param wrapper * 實體對象 * @return int */ Integer selectCount(@Param("ew") Wrapper<T> wrapper); /** * 根據 entity 條件,查詢全部記錄 * @param wrapper * 實體對象封裝操作類(可以為 null) * @return List<T> */ List<T> selectList(@Param("ew") Wrapper<T> wrapper); /** * 根據 Wrapper 條件,查詢全部記錄 * @param wrapper * 實體對象封裝操作類(可以為 null) * @return List<T> */ List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> wrapper); /** * 根據 Wrapper 條件,查詢全部記錄 * @param wrapper * 實體對象封裝操作類(可以為 null) * @return List<Object> */ List<Object> selectObjs(@Param("ew") Wrapper<T> wrapper); /** * 用法:(new RowBounds(offset, limit), ew); * 根據 entity 條件,查詢全部記錄(並翻頁) * @param rowBounds * 分頁查詢條件(可以為 RowBounds.DEFAULT) * @param wrapper * 實體對象封裝操作類(可以為 null) * @return List<T> */ List<T> selectPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper); /** -- 不常用, * 根據 Wrapper 條件,查詢全部記錄(並翻頁) * @param rowBounds * 分頁查詢條件(可以為 RowBounds.DEFAULT) * @param wrapper * 實體對象封裝操作類 * @return List<Map<String, Object>> */ List<Map<String, Object>> selectMapsPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper); }
二、方法應用:
public class TestMapper { @Resource private UserMapper userMapper; /** * 獲取所有用戶 */ @Test public void selectList(){ List<User> userList = userMapper.selectList(null); userList.forEach(System.out::println); } /** * 根據指定條件 查詢符合條件的用戶 (selectList傳參數的查詢形式) */ @Test public void selectAllList(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.ge("age",23); List<User> userList = userMapper.selectList(queryWrapper); userList.forEach(System.out::println); } /** * 根據 id 查詢指定用戶 */ @Test public void selectById(){ User user = userMapper.selectById(1); System.out.println(user); } /** * 根據 ID 批量查詢用戶 */ @Test public void selectBatchIds(){ List<Integer> integers = Arrays.asList(1,2,3); List<User> userList = userMapper.selectBatchIds(integers); userList.forEach(System.out::println); } /** * 根據 Map搜索條件查詢指定條件下的用戶 */ @Test public void selectByMap(){ Map<String,Object> map = new HashMap<>(); map.put("name","Tom"); List<User> userList = userMapper.selectByMap(map); userList.forEach(System.out::println); } /** * wrapper 查詢一條數據 */ @Test public void selectOne(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("name","Tom"); User user = userMapper.selectOne(queryWrapper); System.out.println(user); } /** * 根據指定條件查詢符合條件的記錄數 */ @Test public void selectCount(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.gt("age",20); Integer count = userMapper.selectCount(queryWrapper); System.out.println(count); } /** * 根據指定條件查詢用戶 */ @Test public void selectMaps(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.ge("age",23); List<Map<String, Object>> mapList = userMapper.selectMaps(queryWrapper); mapList.forEach(System.out::println); } /** * 根據指定條件查詢符合條件的用戶 (只返回第一個字段值) */ @Test public void selectObjs(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.ge("age",23); List<Object> list = userMapper.selectObjs(queryWrapper); list.forEach(System.out::println); } /** * 查詢所有用戶並分頁 */ @Test public void selectPage(){ Page<User> page = new Page<>(1,2); IPage<User> userIPage = userMapper.selectPage(page, null); System.out.println("總記錄數:"+userIPage.getTotal()); System.out.println("總頁數:"+userIPage.getPages()); List<User> userList = userIPage.getRecords(); userList.forEach(System.out::println); } /** * 查詢所有用戶並分頁 */ @Test public void selectMapsPage(){ Page<User> page = new Page<>(1,2); IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(page, null); System.out.println("總記錄數:"+mapIPage.getTotal()); System.out.println("總頁數:"+mapIPage.getPages()); List<Map<String, Object>> records = mapIPage.getRecords(); records.forEach(System.out::println); } /** * 插入一條記錄 */ @Test public void insert(){ User user = new User(); user.setUsername("zhangsan"); user.setEmail("777@qq.com"); user.setAge(33); int i = userMapper.insert(user); System.out.println(i); } /** * 根據ID刪除一條記錄 */ @Test public void deleteById(){ int i = userMapper.deleteById(8); System.out.println(i); } /** * 根據指定 Map 條件刪除記錄 */ @Test public void deleteByMap(){ Map<String,Object> map = new HashMap<>(); map.put("age",23); int i = userMapper.deleteByMap(map); System.out.println(i); } /** * 根據指定 Wrapper 條件刪除記錄 */ @Test public void delete(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.likeRight("name","B"); int i = userMapper.delete(queryWrapper); System.out.println(i); } /** * 根據指定ID批量刪除記錄 */ @Test public void deleteBatchIds(){ List<Integer> integers = Arrays.asList(1, 2); int i = userMapper.deleteBatchIds(integers); System.out.println(i); } /** * 根據指定條件更新記錄 */ @Test public void update(){ User user = new User(); user.setEmail("888@qq.com"); UpdateWrapper<User> updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("name","Jack"); int update = userMapper.update(user, updateWrapper); System.out.println(update); } /** * 根據 指定ID 更新記錄 */ @Test public void updateById(){ User user = new User(); user.setId(1L); user.setEmail("666@qq.com"); int i = userMapper.updateById(user); System.out.println(i); }