前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通用Mapper各种方法及说明

通用Mapper各种方法及说明

作者头像
芈亓
发布2023-04-01 10:17:39
5780
发布2023-04-01 10:17:39
举报
文章被收录于专栏:笔记2022笔记2022

基础接口 Select

接口: SelectMapper

方法: List select(T record); 解释: 根据实体中的属性值进行查询,查询条件使用等号

接口: SelectByPrimaryKeyMapper

方法: T selectByPrimaryKey(Object key); 解释: 根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号 接口: SelectAllMapper

方法: List selectAll(); 解释: 查询全部结果,select(null)方法能达到同样的效果 接口: selectOneMapper

方法: T selectOne(T record); 解释: 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号 接口: selectCountMapper

方法: int selectCount(T record); 解释: 根据实体中的属性查询总数,查询条件使用等号

基础接口 Insert

接口: InsertMapper

方法: int insert(T record); 解释: 保存一个实体,null的属性也会保存,不会使用数据库默认值 接口: InsertSelectiveMapper();

方法: int insertSelective(T record); 解释: 保存一个实体,null的属性不会保存,会使用数据库默认值

基础接口 Update

接口: UpdateByPrimaryKeyMapeer

方法: int updateByPrimaryKey(T record); 解释: 根据主键更新实体全部字段,null值会被更新 接口: UpdateByPrimaryKeySelectiveMapper

方法: int updateByPrimaryKeySelective(T record); 解释: 根据主键更新属性不为null的值

基础接口 Delete

接口 DeleteMapper

方法: int delete(T record); 解释: 根据实体属性作为条件进行删除,查询条件使用等号 接口: DeleteByPrimartKeyMapper

方法: int deleteByPrimartKey(object key); 解释: 根据主键字段进行删除,方法参数必须包含完整的主键属性

Base组合接口

接口:BaseSelectMapper 方法:包含上面Select的4个方法 接口:BaseInsertMapper 方法:包含上面Insert的2个方法 接口:BaseUpdateMapper 方法:包含上面Update的2个方法 接口:BaseDeleteMapper 方法:包含上面Delete的2个方法

CRUD组合接口

接口: BaseMapper

方法: 继承了base组合接口中的4个组合接口,包含完整的CRUD方法 Example方法

先创建Example对象

Example example = new Example(需要CRUD的类(与数据库对应实体类).calss); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo(“id”,“id”); // id已知,RUD的判断条件 接口:SelectByExampleMapper

方法:List selectByExample(Object example); 说明:根据Example条件进行查询 重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列 接口:SelectCountByExampleMapper

方法:int selectCountByExample(Object example); 说明:根据Example条件进行查询总数 接口:UpdateByExampleMapper

方法:int updateByExample(@Param(“record”) T record,@Param(“example”) Object example); 说明:根据Example条件更新实体record包含的全部属性,null值会被更新 接口:UpdateByExampleSelectiveMapper

方法:int updateByExampleSelective(@Param(“record”) T record, @Param(“example”) Object example); 说明:根据Example条件更新实体record包含的不是null的属性值 接口:DeleteByExampleMapper

方法:int deleteByExample(Object example); 说明:根据Example条件删除数据

Example组合接口

接口:ExampleMapper

方法:包含上面Example中的5个方法 Condition方法 Condition方法和Example方法作用完全一样,只是为了避免Example带来的歧义,提供的的Condition方法

接口:SelectByConditionMapper

方法:List selectByCondition(Object condition); 说明:根据Condition条件进行查询 接口:SelectCountByConditionMapper

方法:int selectCountByCondition(Object condition); 说明:根据Condition条件进行查询总数 接口:UpdateByConditionMapper

方法:int updateByCondition(@Param(“record”) T record, @Param(“example”) Object condition); 说明:根据Condition条件更新实体record包含的全部属性,null值会被更新 接口:UpdateByConditionSelectiveMapper

方法:int updateByConditionSelective(@Param(“record”) T record, @Param(“example”) Object condition); 说明:根据Condition条件更新实体record包含的不是null的属性值 接口:DeleteByConditionMapper

方法:int deleteByCondition(Object condition); 说明:根据Condition条件删除数据 Condition组合接口 接口:ConditionMapper 方法:包含上面Condition中的5个方法

RowBounds 默认为内存分页,可以配合PageHelper实现物理分页 接口:SelectRowBoundsMapper

方法:List selectByRowBounds(T record, RowBounds rowBounds); 说明:根据实体属性和RowBounds进行分页查询 接口:SelectByExampleRowBoundsMapper

方法:List selectByExampleAndRowBounds(Object example, RowBounds rowBounds); 说明:根据example条件和RowBounds进行分页查询 接口:SelectByConditionRowBoundsMapper

方法:List selectByConditionAndRowBounds(Object condition, RowBounds rowBounds); 说明:根据example条件和RowBounds进行分页查询,该方法和selectByExampleAndRowBounds完全一样,只是名字改成了Condition RowBounds组合接口 接口:RowBoundsMapper

方法:包含上面RowBounds中的前两个方法,不包含selectByConditionAndRowBounds 这些接口针对部分数据库设计,不是所有数据库都支持 接口:InsertListMapper

方法:int insertList(List recordList); 说明:批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等,另外该接口限制实体包含id属性并且必须为自增列

接口:InsertUseGeneratedKeysMapper

方法:int insertUseGeneratedKeys(T record); 说明:插入数据,限制为实体包含id属性并且必须为自增列,实体配置的主键策略无效

MySQL专用

接口:MySqlMapper

继承方法:int insertList(List recordList); 继承方法:int insertUseGeneratedKeys(T record); 说明:该接口不包含方法,继承了special中的InsertListMapper和InsertUseGeneratedKeysMapper

SQLServer专用

由于sqlserver中插入自增主键时,不能使用null插入,不能在insert语句中出现id。 注意SqlServer的两个特有插入方法都使用了 @Options(useGeneratedKeys = true, keyProperty = “id”) 这就要求表的主键为id,且为自增,如果主键不叫id可以看高级教程中的解决 方法。 另外这俩方法和base中的插入方法重名,不能同时存在! 如果某种数据库和SqlServer这里类似,也可以使用这些

接口(需要先测试)。 经测试,PostgreSQL可以采用。

接口:InsertMapper

方法:int insert(T record); 说明:插入数据库,null值也会插入,不会使用列的默认值 接口:InsertSelectiveMapper

方法:int insertSelective(T record); 说明:插入数据库,null的属性不会保存,会使用数据库默认值 接口:SqlServerMapper

说明:这是上面两个接口的组合接口。

Mapper接口

接口:Mapper 该接口兼容Mapper2.x版本,继承了BaseMapper, ExampleMapper, RowBoundsMapper三个组合接口。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-03-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基础接口 Select
  • 基础接口 Insert
  • 基础接口 Update
  • 基础接口 Delete
  • Base组合接口
  • CRUD组合接口
  • Example组合接口
  • MySQL专用
  • SQLServer专用
  • Mapper接口
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档