- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *
- * http://www.apache.org/licenses/LICENSE-2.0 - *
- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*/
package com.mybatisflex.core.service;
import com.mybatisflex.core.BaseMapper;
+import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
-import com.mybatisflex.core.row.RowMapper;
import com.mybatisflex.core.util.ClassUtil;
import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.SqlUtil;
@@ -36,20 +36,22 @@ import java.util.*;
* @author 王帅
* @since 2023-05-01
*/
-@SuppressWarnings("unused")
+@SuppressWarnings({"unused", "unchecked"})
public interface IService 获取对应实体类(Entity)的基础映射类(BaseMapper)。
*
* @return 基础映射类(BaseMapper)
*/
BaseMapper 保存实体类对象数据。
*
* @param entity 实体类对象
* @return {@code true} 保存成功,{@code false} 保存失败。
@@ -60,9 +62,8 @@ public interface IService 保存或者更新实体类对象数据。
*
* @param entity 实体类对象
* @return {@code true} 保存或更新成功,{@code false} 保存或更新失败。
@@ -73,30 +74,30 @@ public interface IService 批量保存实体类对象数据。
*
* @param entities 实体类对象
* @return {@code true} 保存成功,{@code false} 保存失败。
*/
default boolean saveBatch(Collection 批量保存实体类对象数据。
*
- * @param entities 实体类对象
- * @param size 每次保存切分的数量
+ * @param entities 实体类对象
+ * @param batchSize 每次保存切分的数量
* @return {@code true} 保存成功,{@code false} 保存失败。
*/
- default boolean saveBatch(Collection 根据查询条件删除数据。
*
* @param query 查询条件
* @return {@code true} 删除成功,{@code false} 删除失败。
@@ -106,17 +107,17 @@ public interface IService 根据查询条件删除数据。
*
* @param condition 查询条件
* @return {@code true} 删除成功,{@code false} 删除失败。
*/
default boolean remove(QueryCondition condition) {
- return SqlUtil.toBool(getMapper().deleteByCondition(condition));
+ return remove(query().where(condition));
}
/**
- * 根据数据主键删除数据。
+ * 根据数据主键删除数据。
*
* @param id 数据主键
* @return {@code true} 删除成功,{@code false} 删除失败。
@@ -126,7 +127,7 @@ public interface IService 根据数据主键批量删除数据。
*
* @param ids 数据主键
* @return {@code true} 删除成功,{@code false} 删除失败。
@@ -141,40 +142,21 @@ public interface IService 根据 {@link Map} 构建查询条件删除数据。
*
* @param query 查询条件
* @return {@code true} 删除成功,{@code false} 删除失败。
*/
default boolean removeByMap(Map 根据查询条件更新数据。
*
* @param entity 实体类对象
* @param query 查询条件
@@ -184,62 +166,69 @@ public interface IService 根据查询条件更新数据。
*
* @param entity 实体类对象
* @param condition 查询条件
* @return {@code true} 更新成功,{@code false} 更新失败。
*/
default boolean update(T entity, QueryCondition condition) {
- return SqlUtil.toBool(getMapper().updateByCondition(entity, condition));
+ return update(entity, query().where(condition));
}
/**
- * 根据 id 批量更新数据
+ * 根据数据主键批量更新数据
*
* @param entities 实体类对象集合
* @return boolean {@code true} 更新成功,{@code false} 更新失败。
*/
default boolean updateBatch(Collection 根据数据主键批量更新数据
*
* @param entities 实体类对象集合
* @param batchSize 每批次更新数量
- * @return boolean {@code true} 更新成功,{@code false} 更新失败。
+ * @return {@code true} 更新成功,{@code false} 更新失败。
*/
@SuppressWarnings("unchecked")
default boolean updateBatch(Collection 根据数据主键更新数据。
+ *
+ * @param entity 实体类对象
+ * @return {@code true} 更新成功,{@code false} 更新失败。
+ */
+ default boolean updateById(T entity) {
+ return SqlUtil.toBool(getMapper().update(entity));
+ }
+
+ /**
+ * 根据 {@link Map} 构建查询条件更新数据。
+ *
+ * @param entity 实体类对象
+ * @param query 查询条件
+ * @return {@code true} 更新成功,{@code false} 更新失败。
+ */
+ default boolean updateByMap(T entity, Map 根据数据主键查询一条数据。
*
* @param id 数据主键
* @return 查询结果数据
@@ -249,7 +238,7 @@ public interface IService 根据数据主键查询一条数据。
*
* @param id 数据主键
* @return 查询结果数据
@@ -260,7 +249,7 @@ public interface IService 根据查询条件查询一条数据。
*
* @param query 查询条件
* @return 查询结果数据
@@ -269,9 +258,19 @@ public interface IService 根据查询条件查询一条数据。
+ *
+ * @param query 查询条件
+ * @return 查询结果数据
+ * @apiNote 该方法会将查询结果封装为 {@link Optional} 类进行返回,方便链式操作。
+ */
+ default Optional 根据查询条件查询一条数据,并通过 asType 进行接收
*
* @param query 查询条件
* @param asType 接收的数据类型
@@ -282,41 +281,29 @@ public interface IService 根据查询条件查询一条数据。
*
* @param query 查询条件
* @param asType 接收的数据类型
* @return 查询结果数据
* @apiNote 该方法会将查询结果封装为 {@link Optional} 类进行返回,方便链式操作。
*/
- default 根据查询条件查询一条数据。
*
* @param condition 查询条件
* @return 查询结果数据
*/
default T getOne(QueryCondition condition) {
- return getMapper().selectOneByCondition(condition);
+ return getOne(query().where(condition));
}
/**
- * 根据查询条件查询一条数据。
+ * 根据查询条件查询一条数据。
*
* @param condition 查询条件
* @return 查询结果数据
@@ -327,37 +314,16 @@ public interface IService 查询所有数据。
*
* @return 所有数据
*/
default List 根据查询条件查询数据集合。
*
* @param query 查询条件
* @return 数据集合
@@ -367,7 +333,17 @@ public interface IService 根据查询条件查询数据集合。
+ *
+ * @param condition 查询条件
+ * @return 数据集合
+ */
+ default List 根据查询条件查询数据集合,并通过 asType 进行接收
*
* @param query 查询条件
* @param asType 接收的数据类型
@@ -377,9 +353,8 @@ public interface IService 根据数据主键查询数据集合。
*
* @param ids 数据主键
* @return 数据集合
@@ -389,19 +364,19 @@ public interface IService 根据 {@link Map} 构建查询条件查询数据集合。
*
* @param query 查询条件
* @return 数据集合
*/
default List 根据查询条件判断数据是否存在。
*
* @param query 查询条件
* @return {@code true} 数据存在,{@code false} 数据不存在。
@@ -411,7 +386,7 @@ public interface IService 根据查询条件判断数据是否存在。
*
* @param condition 查询条件
* @return {@code true} 数据存在,{@code false} 数据不存在。
@@ -421,16 +396,16 @@ public interface IService 查询所有数据数量。
*
* @return 所有数据数量
*/
default long count() {
- return getMapper().selectCountByQuery(QueryWrapper.create());
+ return count(query());
}
/**
- * 根据查询条件查询数据数量。
+ * 根据查询条件查询数据数量。
*
* @param query 查询条件
* @return 数据数量
@@ -440,29 +415,29 @@ public interface IService 根据查询条件查询数据数量。
*
* @param condition 查询条件
* @return 数据数量
*/
default long count(QueryCondition condition) {
- return getMapper().selectCountByCondition(condition);
+ return count(query().where(condition));
}
// ===== 分页查询操作 =====
/**
- * 分页查询所有数据。
+ * 分页查询所有数据。
*
* @param page 分页对象
* @return 分页对象
*/
default Page 根据查询条件分页查询数据。
*
* @param page 分页对象
* @param query 查询条件
@@ -473,14 +448,18 @@ public interface IService 根据查询条件分页查询数据。
*
* @param page 分页对象
* @param condition 查询条件
* @return 分页对象
*/
default Page
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.mybatisflex.spring.service.impl;
+
+import com.mybatisflex.core.BaseMapper;
+import com.mybatisflex.core.exception.FlexExceptions;
+import com.mybatisflex.core.query.QueryWrapper;
+import com.mybatisflex.core.service.IService;
+import com.mybatisflex.core.table.TableInfo;
+import com.mybatisflex.core.table.TableInfoFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.Collection;
+
+/**
+ * 可缓存数据的 Service 实现类。
+ *
+ * 该实现类对缓存做了以下处理:
+ *
+ * 不支持批量更新操作。
+ */
+ @Override
+ public boolean updateBatch(Collection 获取默认的 {@link QueryWrapper}。
+ *
+ * 使用 {@link QueryWrapper#create()} 构建默认查询条件的时候,
+ * 要使用 {@link QueryWrapper#from(String...)} 方法指定从哪个表
+ * 查询数据,不然使用 {@link QueryWrapper#toDebugSQL()} 生成的
+ * SQL 语句就是 {@code "SELECT * FROM"},没有表名信息。
+ *
+ * 默认通过反射获取表名,建议重写,根据情况设置默认表名,以提升效率。
+ *
+ * 例如:
+ *
+ *
+ *
+ *
+ * @author 王帅
+ * @since 2023-05-30
+ */
+public class CacheableServiceImpl{@code
+ * @Override
+ * public QueryWrapper query() {
+ * return QueryWrapper.create().from(ACCOUNT);
+ * }
+ * }
+ *
+ * @return 默认的 {@link QueryWrapper}
+ */
+ @Override
+ public QueryWrapper query() {
+ String tableName = TableInfoFactory.ofMapperClass(getMapper().getClass()).getTableName();
+ return QueryWrapper.create().from(tableName);
+ }
+
+}
\ No newline at end of file