CRUD 接口
本文通过介绍 BaseMapper
的 CRUD
接口,让你继续体验 Mybatis-Kits 的操作之简单,功能之强大。
public interface BaseMapper<M> {}
M 指的数据模型的泛型,需要根据不同的 Mapper 注入不同的模型,比如:
public interface UserMapper extends BaseMapper<User> {}
getNewId
/**
* 生成一个分布式唯一 ID
*
* @return
*/
Long getNewId();
String getNewIdStr();
String getNewIdHex();
add
/**
*
* 添加一条记录
*
* @param model 数据模型
* @return int 返回影响记录条数
*/
int add(M model);
update
/**
*
* 根据 id 更新对应的model(更新非null 字段)
*
* @param model 要更新的数据模型
* @return 返回影响记录条数
*/
int update(M model);
updateAll
/**
*
* 根据 id 更新对应的 model(更新所有字段,包括null的)
* @param model 要更新的数据模型
* @return 返回影响记录条数
*/
int updateAll(M model);
delete
/**
*
* 根据模型删除一条数据
*
* @param model 数据模型
* @return 返回影响记录条数
*/
int delete(M model);
/**
*
* 根据 id 删除一条数据
*
* @param id 记录ID
* @return 返回影响记录条数
*/
int delete(Serializable id);
deleteByConditions
/**
* 根据条件批量删除
* @param conditions 查询条件
* @return 返回影响记录条数
*/
int deleteByConditions(Conditions conditions);
get
/**
*
* 根据 id 查询
*
* @param id ID主键
* @return 返回查询到的记录
*/
M get(Serializable id);
getByMap
/**
* 根据map 获取一条数据
* @param map 查询条件 Map
* @return 返回查询到的记录
*/
M getByMap(Map<String, Object> map);
getByCondition
/**
*
* 根据查询条件获取一个对象
* @param conditions 查询条件
* @return 返回查询到的数据记录
*/
M getByConditions(Conditions conditions);
search
/**
*
* 取出全部数据
* @return 返回所有数据
*/
List<M> search();
/**
* 分页搜索
* @param page 分页对象
* @return 返回填充数据后的分页对象
*/
Page<M> search(Page<M> page);
searchByMap
/**
* 根据map查询
* @param map 查询条件 Map
* @return 返回数据列表
*/
List<M> searchByMap(Map<String, Object> map);
/**
* 分页搜索
*
* @param page 查询分页对象
* @param map 查询条件 Map
* @return 返回填充数据后的 Page 对象
*/
Page<M> searchByMap(Page<M> page, Map<String, Object> map);
getCount
/**
* 获取总记录数
* @return 返回记录总条数
*/
long getCount();
getCountByMap
/**
* 根据map条件查询总记录数
* @param map 查询 Map
* @return 返回符合条件的记录总数
*/
long getCountByMap(Map<String, Object> map);
getCountByConditions
/**
* 根据 Conditions 条件查询总记录数
* @param conditions 查询条件
* @return 返回符合条件的记录总数
*/
long getCountByConditions(Conditions conditions);
searchByConditions
/**
*
* 根据指定条件 查询对应的列表数据
* @param conditions 查询条件
* @return 返回符合条件的数据列表
*/
List<M> searchByConditions(Conditions conditions);
/**
*
* 根据指定条件 分页查询对应的列表数据
* @param page 分页对象
* @param conditions 查询条件
* @return 返回填充数据后的 Page 对象
*/
Page<M> searchByConditions(Page<M> page,Conditions conditions);
下面我们接着 快速开始工程 继续演示 BaseMapper 的 CRUD
接口。
添加数据
// 影响行数
int affactRows = 0;
// 初始化 User 实体对象, 如果是非自增ID需要初始化ID
// 系统有内置的分布式 ID 生成工具
User user = new User(IdUtil.getNewId());
// 插入 User (如果是自增ID的话,插入成功会自动回写ID到实体类)
user.setName("Rock");
affactRows = userMapper.add(user);
查询数据
// 根据 ID 查询
User user = userMapper.get(user.getId());
// 查询ID > 100 所有用户列表
List<User> userList = userMapper.searchByConditions(
new Conditions.gt("id", 100)
);
// 如果只想取列表中的一条
User u = userMapper.getByCondition(new Conditions.gt("id", 100));
更新数据
// 更新 User
String userId = 1;
User user = new User(userId);
user.setAge(18);
affactRows = userMapper.update(user);
// 你也可以这样
affactRows = userMapper.updateAll(user);
/*
注意:update() 方法只更新 User 实体的非 null 字段,是增量更新
而 updateAll() 会更新所有字段,包括值为 null 的字段
应用需求:比如你知道用户ID,只想更新 Name 字段,常规方法是下面这样的
*/
String userId = 1;
User user = userMapper.get(userId);
user.setName("New name");
userMapper.updateAll(user);
// 或则你也可以这样实现
User user = new User(userId);
user.setName("New name");
userMapper.update(user); // 这样可以少一次查询
特别注意:
如果你需要要把某个字段更新成 null, 请使用 updateAll()
删除数据
// 根据 ID 删除 User
affactRows = userMapper.delete(user.getId());
// 删除 id > 100 and id < 200 的所有用户
Conditions condi = new Conditions();
condi.gt("id", 100).lt("id", 200);
affactRows = UserMapper.deleteByConditions(condi);