mybatisplus接口解析

以你之姓@ 2024-02-24 01:14 115阅读 0赞

一、前言

java开发应用程序与数据库交互使用比较多的就是mybatisplus接口。通过mybatisplus接口,我们可以通过程序更好得到对数据库表进行增删查改。

二、mybatisplus Mapper接口源码

  1. /**
  2. * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
  3. * <p>这个 Mapper 支持 id 泛型</p>
  4. *
  5. * @author hubin
  6. * @since 2016-01-23
  7. */
  8. public interface BaseMapper<T> extends Mapper<T> {
  9. /**
  10. * 插入一条记录
  11. *
  12. * @param entity 实体对象
  13. */
  14. int insert(T entity);
  15. /**
  16. * 根据 ID 删除
  17. *
  18. * @param id 主键ID
  19. */
  20. int deleteById(Serializable id);
  21. /**
  22. * 根据实体(ID)删除
  23. *
  24. * @param entity 实体对象
  25. * @since 3.4.4
  26. */
  27. int deleteById(T entity);
  28. /**
  29. * 根据 columnMap 条件,删除记录
  30. *
  31. * @param columnMap 表字段 map 对象
  32. */
  33. int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
  34. /**
  35. * 根据 entity 条件,删除记录
  36. *
  37. * @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
  38. */
  39. int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  40. /**
  41. * 删除(根据ID 批量删除)
  42. *
  43. * @param idList 主键ID列表(不能为 null 以及 empty)
  44. */
  45. int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
  46. /**
  47. * 根据 ID 修改
  48. *
  49. * @param entity 实体对象
  50. */
  51. int updateById(@Param(Constants.ENTITY) T entity);
  52. /**
  53. * 根据 whereEntity 条件,更新记录
  54. *
  55. * @param entity 实体对象 (set 条件值,可以为 null)
  56. * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
  57. */
  58. int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
  59. /**
  60. * 根据 ID 查询
  61. *
  62. * @param id 主键ID
  63. */
  64. T selectById(Serializable id);
  65. /**
  66. * 查询(根据ID 批量查询)
  67. *
  68. * @param idList 主键ID列表(不能为 null 以及 empty)
  69. */
  70. List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
  71. /**
  72. * 查询(根据 columnMap 条件)
  73. *
  74. * @param columnMap 表字段 map 对象
  75. */
  76. List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
  77. /**
  78. * 根据 entity 条件,查询一条记录
  79. * <p>查询一条记录,例如 qw.last("limit 1") 限制取一条记录, 注意:多条数据会报异常</p>
  80. *
  81. * @param queryWrapper 实体对象封装操作类(可以为 null)
  82. */
  83. default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
  84. List<T> ts = this.selectList(queryWrapper);
  85. if (CollectionUtils.isNotEmpty(ts)) {
  86. if (ts.size() != 1) {
  87. throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records");
  88. }
  89. return ts.get(0);
  90. }
  91. return null;
  92. }
  93. /**
  94. * 根据 Wrapper 条件,查询总记录数
  95. *
  96. * @param queryWrapper 实体对象封装操作类(可以为 null)
  97. */
  98. Long selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  99. /**
  100. * 根据 entity 条件,查询全部记录
  101. *
  102. * @param queryWrapper 实体对象封装操作类(可以为 null)
  103. */
  104. List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  105. /**
  106. * 根据 Wrapper 条件,查询全部记录
  107. *
  108. * @param queryWrapper 实体对象封装操作类(可以为 null)
  109. */
  110. List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  111. /**
  112. * 根据 Wrapper 条件,查询全部记录
  113. * <p>注意: 只返回第一个字段的值</p>
  114. *
  115. * @param queryWrapper 实体对象封装操作类(可以为 null)
  116. */
  117. List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  118. /**
  119. * 根据 entity 条件,查询全部记录(并翻页)
  120. *
  121. * @param page 分页查询条件(可以为 RowBounds.DEFAULT)
  122. * @param queryWrapper 实体对象封装操作类(可以为 null)
  123. */
  124. <P extends IPage<T>> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  125. /**
  126. * 根据 Wrapper 条件,查询全部记录(并翻页)
  127. *
  128. * @param page 分页查询条件
  129. * @param queryWrapper 实体对象封装操作类
  130. */
  131. <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  132. }

mybatisplus Mapper 由苞米豆公司开发。

现在对源码进行解析

public interface BaseMapper extends Mapper

BaseMapper 首先这是一个泛型接口,并且这个BaseMapper 继承了 Mapper接口。Mapper也是一个泛型接口。

92b1701804e8481fa7c07054ca896d47.png

1、泛型接口

定义泛型接口与定义普通接口类似,只需要在接口的名称后面加上<T>或其他泛型参数即可。其中<T>表示这是一个泛型参数,可以在实现接口的时候指定具体的类型。

使用泛型接口时,可以指定具体的类型作为泛型参数,也可以使用通配符?表示任意类型。

2、在BaseMapper接口中定义了如下方法:

/**
* 插入一条记录
*
* @param entity 实体对象
*/
int insert(T entity);

/**
* 根据 ID 删除
*
* @param id 主键ID
*/
int deleteById(Serializable id);

/**
* 根据实体(ID)删除
*
* @param entity 实体对象
* @since 3.4.4
*/
int deleteById(T entity);

/**
* 根据 columnMap 条件,删除记录
*
* @param columnMap 表字段 map 对象
*/
int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);

/**
* 根据 entity 条件,删除记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper);

/**
* 删除(根据ID 批量删除)
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

/**
* 根据 ID 修改
*
* @param entity 实体对象
*/
int updateById(@Param(Constants.ENTITY) T entity);

/**
* 根据 whereEntity 条件,更新记录
*
* @param entity 实体对象 (set 条件值,可以为 null)
* @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper);

/**
* 根据 ID 查询
*
* @param id 主键ID
*/
T selectById(Serializable id);

/**
* 查询(根据ID 批量查询)
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
List selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

/**
* 查询(根据 columnMap 条件)
*
* @param columnMap 表字段 map 对象
*/
List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap);

/**
* 根据 entity 条件,查询一条记录
*

查询一条记录,例如 qw.last(“limit 1”) 限制取一条记录, 注意:多条数据会报异常


*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
default T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper) {
List 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”);
}
return ts.get(0);
}
return null;
}

/**
* 根据 Wrapper 条件,查询总记录数
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
Long selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);

/**
* 根据 entity 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);

/**
* 根据 Wrapper 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper);

/**
* 根据 Wrapper 条件,查询全部记录
*

注意: 只返回第一个字段的值


*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper);

/**
* 根据 entity 条件,查询全部记录(并翻页)
*
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/

> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

/**
* 根据 Wrapper 条件,查询全部记录(并翻页)
*
* @param page 分页查询条件
* @param queryWrapper 实体对象封装操作类
*/

>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

三、接口应用

通过源码我们只看到了接口的定义,却看不到接口的是如何连接到数据库和接口的实现。

我们再代码中的引用只需要定义一个接口继承BaseMapper 接口

如:

  1. @Repository
  2. public interface MCouponsMapper extends BaseMapper<MCoupons>{
  3. }

<>符中传入具体的类型

皆可以通过实际的接口MCouponsMapper来调用BaseMapper定义的所以方法。

发表评论

表情:
评论列表 (有 0 条评论,115人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mybatisplus接口

    一、前言 java开发应用程序与数据库交互使用比较多的就是mybatisplus接口。通过mybatisplus接口,我们可以通过程序更好得到对数据库表进行增删查改。

    相关 HandlerAdapter接口源码

    > HandlerAdapter是一个适配器接口类,适配器模式是指两个不兼容接口之间的桥梁,要想让一个接口使用另外一个接口的实现中间可以加一层适配器类;举个例子:笔记本没有网线