MyBatis-Plus 中的基本 CRUD 在内置的 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);
}
@Test
public void testInsert(){
User user = new User();
user.setId(null);
user.setName("张三");
user.setAge(23);
user.setEmail("[email protected]");
int result = userMapper.insert(user);
System.out.println("result:" + result);
System.out.println("id:" + user.getId());
}
不仅返回受影响的行数,也将自动生成的 id 赋值给了 user 对象 这里的 id不是自增生成的,是按照雪花算法生成的(雪花算法:一种随机数算法)
@Test
public void testDeleteById(){
//通过id删除用户信息
//DELETE FROM user WHERE id=?
int result = userMapper.deleteById(1577302903974060034L);
System.out.println(result);
}
@Test
public void testDeleteBatchIds(){
//通过多个id批量删除
//DELETE FROM user WHERE id IN ( ? , ? , ? )
List<Long> list = Arrays.asList(1L, 2L, 3l);
int result = userMapper.deleteBatchIds(list);
System.out.println(result);
}
@Test
public void testDeleteByMap(){
//根据map集合中所设置的条件删除记录
//DELETE FROM user WHERE name = ? AND age = ?
HashMap<String, Object> map = new HashMap<>();
map.put("age", 23);
map.put("name", "张三");
int result = userMapper.deleteByMap(map);
System.out.println(result);
}
map 中放的就是删除的条件
@Test
public void testUpdateById(){
User user = new User();
user.setId(4L);
user.setName("admin");
user.setAge(22);
user.setEmail(null);
//根据id修改用户信息
//UPDATE user SET name=?, age=? WHERE id=?
int result = userMapper.updateById(user);
System.out.println(result);
}
@Test
public void testSelectById(){
//根据id查询用户信息
//SELECT id,name,age,email FROM user WHERE id=?
User user = userMapper.selectById(4L);
System.out.println(user);
}
@Test
public void testSelectBatchIds(){
//根据多个id查询多个用户信息
//SELECT id,name,age,email FROM user WHERE id IN ( ? , ? )
List<Long> list = Arrays.asList(4L, 5L);
List<User> users = userMapper.selectBatchIds(list);
users.forEach(user -> System.out.println(user));
}
@Test
public void testSelectByMap(){
//通过map条件查询用户信息
//SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
HashMap<String, Object> map = new HashMap<>();
map.put("age", 22);
map.put("name", "admin");
List<User> users = userMapper.selectByMap(map);
users.forEach(user -> System.out.println(user));
}
@Test
public void testSelectList(){
//查询所有用户信息
//SELECT id,name,age,email FROM user
List<User> users = userMapper.selectList(null);
users.forEach(user -> System.out.println(user));
}
通过观察 BaseMapper 中的方法,大多方法中都有 Wrapper 类型的形参,此为条件构造器,可针对与 SQL 语句设置不同的条件,若没有条件,则可以为该形参赋值 null,即查询(删除/修改)所有数据
说明:
- 通用 Service CRUD 封装 IService 接口,进一步封装 CRUD 采用
get 查询单行
remove 删除
list 查询集合
page 分页
前缀命名方式区分Mapper
层避免混淆,- 泛型
T
为任意实体对象- 建议如果存在自定义通用 Service 方法的可能,请创建自己的
IBaseService
继承Mybatis-Plus
通过的基类
MyBatis-Plus 中有一个接口 IService 和其实现类 ServiceImpl,封装了常见的业务层逻辑
详情查询源码 IService 和 ServiceImpl
package com.fickler.mybatisplus.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.fickler.mybatisplus.pojo.User;
/**
* @author dell
* @version 1.0
*/
public interface UserService extends IService<User> {
}
package com.fickler.mybatisplus.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fickler.mybatisplus.mapper.UserMapper;
import com.fickler.mybatisplus.pojo.User;
import com.fickler.mybatisplus.service.UserService;
/**
* @author dell
* @version 1.0
*/
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
@Test
public void testGetCount(){
long count = userService.count();
System.out.println(count);
}
@Test
public void testSaveBatch(){
//SQL长度有限制,海量数量插入单条SQL无法实现
//因此MP将批量插入放在了通用Service实现,而不是通用Mapper
ArrayList<User> users = new ArrayList<>();
for (int i = 0; i < 5; i ++ ){
User user = new User();
user.setName("ybc" + i);
user.setAge(20 + i);
users.add(user);
}
//INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? )
userService.saveBatch(users);
}