一、基础接口
1、Insert(2个)
接口:InsertMapper<T>
方法:int insert(T record);
说明:保存一个实体,null的属性也会保存,不会使用数据库默认值
接口:InsertSelectiveMapper<T>
方法:int insertSelective(T record);
说明:保存一个实体,null的属性不会保存,会使用数据库默认值
2、Delete(2个)
接口:DeleteMapper<T>
方法:int delete(T record);
说明:根据实体属性作为条件进行删除,查询条件使用等号
接口:DeleteByPrimaryKeyMapper<T>
方法:int deleteByPrimaryKey(Object key);
说明:根据主键字段进行删除,方法参数必须包含完整的主键属性
3、Update(2个)
接口:UpdateByPrimaryKeyMapper<T>
方法:int updateByPrimaryKey(T record);
说明:根据主键更新实体全部字段,null值会被更新
接口:UpdateByPrimaryKeySelectiveMapper<T>
方法:int updateByPrimaryKeySelective(T record);
说明:根据主键更新属性不为null的值
4、Select(5个)
接口:SelectMapper<T>
方法:List<T> select(T record);
说明:根据实体中的属性值进行查询,查询条件使用等号
接口:SelectByPrimaryKeyMapper<T>
方法:T selectByPrimaryKey(Object key);
说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
接口:SelectAllMapper<T>
方法:List<T> selectAll();
说明:查询全部结果,select(null)方法能达到同样的效果
接口:SelectOneMapper<T>
方法:T selectOne(T record);
说明:根据实体中的属性进行查询,一个返回值,当多个结果会抛出异常,查询条件使用等号
接口:SelectCountMapper<T>
方法:int selectCount(T record);
说明:根据实体中的属性查询总数,查询条件使用等号
二、扩展方法
1、Example方法(6个)
// Example示例
Example example = new Example(JavaBean.class);
example.setOrderByClause(“字段名 ASC”); // 添加升序排列条件,DESC为降序
example.setDistinct(false); // 去除重复,boolean型,true为选择不重复的记录
接口:SelectByExampleMapper<T>
方法:List selectByExample(Object example);
说明:根据Example条件进行查询
重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列
接口:SelectCountByExampleMapper<T>
方法:int selectCountByExample(Object example);
说明:根据Example条件进行查询总数
接口:UpdateByExampleMapper<T>
方法:int updateByExample(T record, Object example);
说明:根据Example条件更新实体record包含的全部属性,null值会被更新
接口:UpdateByExampleSelectiveMapper<T>
方法:int updateByExampleSelective(T record, Object example);
说明:根据Example条件更新实体record包含的不是null的属性值
接口:DeleteByExampleMapper<T>
方法:int deleteByExample(Object example);
说明:根据Example条件删除数据
方法:int countByExample(Object example);
说明:根据Example条件计数
2、Condition方法(5个)
Condition方法和Example方法作用完全一样,只是为了避免Example带来的歧义,提供的的Condition方法
// Criteria示例
Example.Criteria criteria = example.createCriteria();
criteria.andXxxIsNull; // 添加字段xxx为null的条件
criteria.andXxxIsNotNull; // 添加字段xxx不为null的条件
criteria.andXxxEqualTo(value); // 添加xxx字段等于value条件
criteria.andXxxNotEqualTo(value); // 添加xxx字段不等于value条件
criteria.andXxxGreaterThan(value); // 添加xxx字段大于value条件
criteria.andXxxGreaterThanOrEqualTo(value); // 添加xxx字段大于等于value条件
criteria.andXxxLessThan(value); // 添加xxx字段小于value条件
criteria.andXxxLessThanOrEqualTo(value); // 添加xxx字段小于等于value条件
criteria.andXxxIn(List<>); // 添加xxx字段值在List<?>条件
criteria.andXxxNotIn(List<>); // 添加xxx字段值不在List<?>条件
criteria.andXxxLike(“%”+value+”%”); // 添加xxx字段值为value的模糊查询条件
criteria.andXxxNotLike(“%”+value+”%”); // 添加xxx字段值不为value的模糊查询条件
criteria.andXxxBetween(value1,value2); // 添加xxx字段值在value1和value2之间条件
criteria.andXxxNotBetween(value1,value2); // 添加xxx字段值不在value1和value2之间条件
接口:SelectByConditionMapper<T>
方法:List<T> selectByCondition(Object condition);
说明:根据Condition条件进行查询
接口:SelectCountByConditionMapper<T>
方法:int selectCountByCondition(Object condition);
说明:根据Condition条件进行查询总数
接口:UpdateByConditionMapper<T>
方法:int updateByCondition(T record,Object condition);
说明:根据Condition条件更新实体record包含的全部属性,null值会被更新
接口:UpdateByConditionSelectiveMapper
方法:int updateByConditionSelective(T record, Object condition);
说明:根据Condition条件更新实体record包含的不是null的属性值
接口:DeleteByConditionMapper
方法:int deleteByCondition(Object condition);
说明:根据Condition条件删除数据
3、RowBounds(3个)
默认为内存分页,可以配合PageHelper实现物理分页
接口:SelectRowBoundsMapper<T>
方法:List<T> selectByRowBounds(T record, RowBounds rowBounds);
说明:根据实体属性和RowBounds进行分页查询
接口:SelectByExampleRowBoundsMapper<T>
方法:List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds);
说明:根据example条件和RowBounds进行分页查询
接口:SelectByConditionRowBoundsMapper<T>
方法:List<T> selectByConditionAndRowBounds(Object condition, RowBounds rowBounds);
说明:Condition方法和Example方法作用完全一样,只是名字改成了Condition
4、Special(2个)
这些接口针对部分数据库设计,不是所有数据库都支持
接口:InsertListMapper<T>
方法:int insertList(List<T> recordList);
说明:批量插入,支持批量插入的数据库可以使用,例如MySQL、H2等,另外该接口限制实体包含id属性并且必须为自增列
接口:InsertUseGeneratedKeysMapper<T>
方法:int insertUseGeneratedKeys(T record);
说明:插入数据,限制为实体包含id属性并且必须为自增列,实体配置的主键策略无效
5、Ids(2个)
通过操作ids字符串进行操作,ids 如 “1,2,3” 这种形式的字符串,这个方法要求实体类中有且只有一个带有@Id
注解的字段,否则会抛出异常。
接口:SelectByIdsMapper
方法:List<T> selectByIds(String ids)
说明:根据主键字符串进行查询,类中只有存在一个带有@Id注解的字段
接口:DeleteByIdsMapper
方法:int deleteByIds(String ids)
说明:根据主键字符串进行删除,类中只有存在一个带有@Id注解的字段