MyBatis-Plus速成指南:基本CURD
-
BaseMapper:
- MyBatis-Plus 中的基本 CURD 在内置的 BaseMapper 中都已得到了实现,我们可以直接使用接口,接口如下:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package com.baomidou.mybatisplus.core.mapper; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils; import java.io.Serializable; import java.util.Collection; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Param; public interface BaseMapper<T> extends Mapper<T> { int insert(T entity); int deleteById(Serializable id); int deleteById(T entity); int deleteByMap(@Param("cm") Map<String, Object> columnMap); int delete(@Param("ew") Wrapper<T> queryWrapper); int deleteBatchIds(@Param("coll") Collection<?> idList); int updateById(@Param("et") T entity); int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper); T selectById(Serializable id); List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList); List<T> selectByMap(@Param("cm") Map<String, Object> columnMap); default T selectOne(@Param("ew") Wrapper<T> queryWrapper) { List<T> ts = this.selectList(queryWrapper); if (CollectionUtils.isNotEmpty(ts)) { if (ts.size() != 1) { throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records", new Object[0]); } else { return ts.get(0); } } else { return null; } } default boolean exists(Wrapper<T> queryWrapper) { Long count = this.selectCount(queryWrapper); return null != count && count > 0L; } Long selectCount(@Param("ew") Wrapper<T> queryWrapper); List<T> selectList(@Param("ew") Wrapper<T> queryWrapper); List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper); List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper); <P extends IPage<T>> P selectPage(P page, @Param("ew") Wrapper<T> queryWrapper); <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param("ew") Wrapper<T> queryWrapper); }
- MyBatis-Plus 中的基本 CURD 在内置的 BaseMapper 中都已得到了实现,我们可以直接使用接口,接口如下:
-
插入:
@Test public void contextLoads() { User user = new User(null,"张三",22,"zhangsan@163.com"); int result = userMapper.insert(user); System.out.println("自动生成 id 为:" + user.getId()); } }
- 最终获取的 id 为:1829897454010204162
- 这是因为 MyBatis-Plus 在实现插入数据时,会默认基于雪花算法的策略生成 id 值
-
删除:
- 通过 id 删除:
@Test public void delete(){ //通过 id 删除用户信息 int result = userMapper.deleteById(1829897454010204162L); System.out.println(result); }
- 通过 id 批量删除:
@Test public void delete(){ //根据 id 批量删除 List<Long> list = Arrays.asList(1L,2L); int result = userMapper.deleteBatchIds(list); System.out.println(result); }
- 通过 map 条件删除记录:
@Test public void delete(){ Map<String,Object> map = new HashMap<>(); map.put("age",23); map.put("name","张三"); int result = userMapper.deleteByMap(map); System.out.println(result); }
- 通过 id 删除:
-
修改:
@Test public void Update(){ User user = new User(6L,"张思思",20,null); int result = userMapper.updateById(user); System.out.println(result); }
-
查询:
- 根据 id 查询用户信息
@Test public void select(){ //通过 id 进行查询 User user = userMapper.selectById(3L); System.out.println(user); }
- 根据多个 id 查询多个用户信息
@Test public void select(){ //通过多个 id 进行查询 List<Long> list = Arrays.asList(3L,4L); List<User> listUser = userMapper.selectBatchIds(list); listUser.forEach(System.out::println); }
- 通过 map 条件查询用户信息
@Test public void select(){ //通过 map 条件查询所有数据 Map<String,Object> map = new HashMap<>(); map.put("age",28); map.put("name","Tom"); List<User> list = userMapper.selectByMap(map); list.forEach(System.out::println); }
- 查询所有数据:
@Test public void select(){ //查询所有数据 List<User> list = userMapper.selectList(null); }
- 根据 id 查询用户信息
通用 Service:
-
说明:
- 通用 Service CURD 封装 IService 接口,进一步封装 CURD 采用:
- get:查询单行
- remove:删除
- list:查询集合
- page:分页
- 前缀名方式区分 Mapper 层避免混淆
- 泛型 T 为任意实体对象
- 建议:如果存在自定义通用 Service 方法的可能,创建自己的 IBaseService 集成 MyBatis-Plus 提供的基类
- 官网地址:https://baomidou.com/pages/49cc81/#service-crud-%E6%8E%A5%E5%8F% A3
- 通用 Service CURD 封装 IService 接口,进一步封装 CURD 采用:
-
IService:
- MyBatis-Plus 有一个接口 IService 和其实现类 ServiceImpl,封装了常见的业务层逻辑
- 详情查看源码:IService 和 ServiceImpl
-
创建 Service 接口和实现类:
- 接口:
//UserService 继承 IService 模板提供的基础功能 public interface UerService extends IService<User> { }
- 实现类:
/** * ServiceImpl 实现了 IService,提供了IService 中基础功能的实现 * 若 ServiceImpl 无法满足业务需求,则可以使用自定义的 UserService 定义方法,并在实现类中实现 */ @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UerService { }
- 接口:
-
测试查询记录数:
@Autowired private UserService userService; @Test public void TestService(){ Long count = userService.count(); System.out.println("总数为:" + count); }
-
测试批量插入:
ArrayList<User> list = new ArrayList<>(); for (int i = 0;i < 5;i++){ User user = new User(); user.setName("章"+i); user.setAge(20 + i); user.setEmail(i + "@163.com"); list.add(user); } //批量加入 userService.saveBatch(list);