通用mapper接口说明
这里只介绍常用的一些操作,如果需要更详细的可以去官网进行了解。https://github.com/abel533/Mapper/wiki
通用接口方法
当继承通用mapper后,会提供默认的一些方法供我们使用。
select
- 方法:
List<T> select(T record)
- 说明:根据实体中的属性值进行查询,查询条件使用等号
selectByPrimaryKey
- 方法:
T selectByPrimaryKey(Object key)
- 说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
selectAll
- 方法:
List<T> selectAll()
- 说明:查询全部结果,
select(null)
方法能达到同样的效果
selectOne
- 方法:
T selectOne(T record)
- 说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
selectCount
- 方法:
int selectCount(T record)
- 说明:根据实体中的属性查询总数,查询条件使用等号
selectByExample
- 方法:
List<T> selectByExample(Example example)
- 说明:自定义复杂查询
insert
- 方法:
int insert(T record)
- 说明:保存一个实体,null的属性也会保存,不会使用数据库默认值
insertSelective
- 方法:
int insertSelective(T record)
- 说明:保存一个实体,null的属性不会保存,会使用数据库默认值
updateByPrimaryKey
- 方法:
int updateByPrimaryKey(T record)
- 说明:根据主键更新实体全部字段,null值会被更新
updateByPrimaryKeySelective
- 方法:
int updateByPrimaryKeySelective(T record)
- 说明:根据主键更新属性不为null的值
delete
- 方法:
int delete(T record)
- 说明:根据实体属性作为条件进行删除,查询条件使用等号
deleteByPrimaryKey
- 方法:
int deleteByPrimaryKey(Object key)
- 说明:根据主键字段进行删除,方法参数必须包含完整的主键属性
注意
一般情况下,实体中的字段和数据库表中的字段是一一对应的,但是也有很多情况我们会在实体中增加一些额外的属性,这种情况下,就需要使用 @Transient
注解来告诉通用 Mapper
这不是表中的字段。
配置示例:
@Transient
private String otherThings; //非数据库表中字段