add example Logic

This commit is contained in:
Timi
2026-01-15 17:22:47 +08:00
parent fd7bb73f5c
commit 7cd79bff55
9 changed files with 235 additions and 46 deletions

View File

@ -0,0 +1,16 @@
package com.imyeyu.spring.bean;
/**
* 示例连接逻辑
*
* @author 夜雨
* @since 2026-01-15 11:19
*/
public enum Logic {
/** 且 */
AND,
/** 或 */
OR
}

View File

@ -22,6 +22,12 @@ public class Page<T> extends BasePage {
/** 模糊匹配示例 */ /** 模糊匹配示例 */
protected T likesExample; protected T likesExample;
/** 精确匹配连接逻辑 */
protected Logic equalsLogic = Logic.AND;
/** 模糊匹配连接逻辑 */
protected Logic likesLogic = Logic.OR;
/** 排序字段映射 */ /** 排序字段映射 */
protected LinkedHashMap<String, BaseMapper.OrderType> orderMap; protected LinkedHashMap<String, BaseMapper.OrderType> orderMap;
@ -95,6 +101,42 @@ public class Page<T> extends BasePage {
this.likesExample = likesExample; this.likesExample = likesExample;
} }
/**
* 获取精确匹配连接逻辑
*
* @return 连接逻辑
*/
public Logic getEqualsLogic() {
return equalsLogic;
}
/**
* 设置精确匹配连接逻辑
*
* @param equalsLogic 连接逻辑
*/
public void setEqualsLogic(Logic equalsLogic) {
this.equalsLogic = equalsLogic;
}
/**
* 获取模糊匹配连接逻辑
*
* @return 连接逻辑
*/
public Logic getLikesLogic() {
return likesLogic;
}
/**
* 设置模糊匹配连接逻辑
*
* @param likesLogic 连接逻辑
*/
public void setLikesLogic(Logic likesLogic) {
this.likesLogic = likesLogic;
}
/** /**
* 获取排序映射 * 获取排序映射
* *

View File

@ -1,11 +1,13 @@
package com.imyeyu.spring.mapper; package com.imyeyu.spring.mapper;
import com.imyeyu.spring.bean.Logic;
import com.imyeyu.spring.bean.Page; import com.imyeyu.spring.bean.Page;
import com.imyeyu.spring.bean.PageResult; import com.imyeyu.spring.bean.PageResult;
import com.imyeyu.spring.util.SQLProvider; import com.imyeyu.spring.util.SQLProvider;
import org.apache.ibatis.annotations.DeleteProvider; import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider; import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider; import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider; import org.apache.ibatis.annotations.UpdateProvider;
@ -111,8 +113,19 @@ public interface BaseMapper<T, P> {
* @param t 示例对象 * @param t 示例对象
* @return 数据对象 * @return 数据对象
*/ */
default T selectByExample(T t) {
return selectByExample(t, Logic.AND);
}
/**
* 根据示例查询单条数据
*
* @param t 示例对象
* @param logic 条件连接逻辑
* @return 数据对象
*/
@SelectProvider(type = SQLProvider.class, method = "selectByExample") @SelectProvider(type = SQLProvider.class, method = "selectByExample")
T selectByExample(T t); T selectByExample(@Param("entity") T t, @Param("logic") Logic logic);
/** /**
* 根据示例查询全部数据 * 根据示例查询全部数据
@ -120,8 +133,19 @@ public interface BaseMapper<T, P> {
* @param t 示例对象 * @param t 示例对象
* @return 数据列表 * @return 数据列表
*/ */
default List<T> selectAllByExample(T t) {
return selectAllByExample(t, Logic.AND);
}
/**
* 根据示例查询全部数据
*
* @param t 示例对象
* @param logic 条件连接逻辑
* @return 数据列表
*/
@SelectProvider(type = SQLProvider.class, method = "selectAllByExample") @SelectProvider(type = SQLProvider.class, method = "selectAllByExample")
List<T> selectAllByExample(T t); List<T> selectAllByExample(@Param("entity") T t, @Param("logic") Logic logic);
/** /**
* 修改数据 * 修改数据
@ -152,8 +176,18 @@ public interface BaseMapper<T, P> {
* *
* @param t 示例对象 * @param t 示例对象
*/ */
default void deleteAllByExample(T t) {
deleteAllByExample(t, Logic.AND);
}
/**
* 根据示例批量逻辑删除
*
* @param t 示例对象
* @param logic 条件连接逻辑
*/
@UpdateProvider(type = SQLProvider.class, method = "deleteAllByExample") @UpdateProvider(type = SQLProvider.class, method = "deleteAllByExample")
void deleteAllByExample(T t); void deleteAllByExample(@Param("entity") T t, @Param("logic") Logic logic);
/** /**
* 销毁(物理删除) * 销毁(物理删除)
@ -168,6 +202,16 @@ public interface BaseMapper<T, P> {
* *
* @param t 示例对象 * @param t 示例对象
*/ */
@UpdateProvider(type = SQLProvider.class, method = "destroyAllByExample") default void destroyAllByExample(T t) {
void destroyAllByExample(T t); destroyAllByExample(t, Logic.AND);
}
/**
* 根据示例批量销毁(物理删除)
*
* @param t 示例对象
* @param logic 条件连接逻辑
*/
@DeleteProvider(type = SQLProvider.class, method = "destroyAllByExample")
void destroyAllByExample(@Param("entity") T t, @Param("logic") Logic logic);
} }

View File

@ -1,5 +1,6 @@
package com.imyeyu.spring.mapper; package com.imyeyu.spring.mapper;
import com.imyeyu.spring.bean.Logic;
import com.imyeyu.spring.bean.Page; import com.imyeyu.spring.bean.Page;
import com.imyeyu.spring.bean.PageResult; import com.imyeyu.spring.bean.PageResult;
import com.imyeyu.spring.util.DynamicTableSQLProvider; import com.imyeyu.spring.util.DynamicTableSQLProvider;
@ -93,8 +94,20 @@ public interface DynamicTableMapper<T, P> {
* @param entity 示例对象 * @param entity 示例对象
* @return 数据对象 * @return 数据对象
*/ */
default T selectByExample(String tableName, T entity) {
return selectByExample(tableName, entity, Logic.AND);
}
/**
* 根据示例查询单条数据
*
* @param tableName 表名
* @param entity 示例对象
* @param logic 条件连接逻辑
* @return 数据对象
*/
@SelectProvider(type = DynamicTableSQLProvider.class, method = "selectByExample") @SelectProvider(type = DynamicTableSQLProvider.class, method = "selectByExample")
T selectByExample(@Param("tableName") String tableName, @Param("entity") T entity); T selectByExample(@Param("tableName") String tableName, @Param("entity") T entity, @Param("logic") Logic logic);
/** /**
* 根据示例查询全部数据 * 根据示例查询全部数据
@ -103,8 +116,20 @@ public interface DynamicTableMapper<T, P> {
* @param entity 示例对象 * @param entity 示例对象
* @return 数据列表 * @return 数据列表
*/ */
default List<T> selectAllByExample(String tableName, T entity) {
return selectAllByExample(tableName, entity, Logic.AND);
}
/**
* 根据示例查询全部数据
*
* @param tableName 表名
* @param entity 示例对象
* @param logic 条件连接逻辑
* @return 数据列表
*/
@SelectProvider(type = DynamicTableSQLProvider.class, method = "selectAllByExample") @SelectProvider(type = DynamicTableSQLProvider.class, method = "selectAllByExample")
List<T> selectAllByExample(@Param("tableName") String tableName, @Param("entity") T entity); List<T> selectAllByExample(@Param("tableName") String tableName, @Param("entity") T entity, @Param("logic") Logic logic);
/** /**
* 修改数据 * 修改数据
@ -139,8 +164,19 @@ public interface DynamicTableMapper<T, P> {
* @param tableName 表名 * @param tableName 表名
* @param entity 示例对象 * @param entity 示例对象
*/ */
default void deleteAllByExample(String tableName, T entity) {
deleteAllByExample(tableName, entity, Logic.AND);
}
/**
* 根据示例批量逻辑删除
*
* @param tableName 表名
* @param entity 示例对象
* @param logic 条件连接逻辑
*/
@UpdateProvider(type = DynamicTableSQLProvider.class, method = "deleteAllByExample") @UpdateProvider(type = DynamicTableSQLProvider.class, method = "deleteAllByExample")
void deleteAllByExample(@Param("tableName") String tableName, @Param("entity") T entity); void deleteAllByExample(@Param("tableName") String tableName, @Param("entity") T entity, @Param("logic") Logic logic);
/** /**
* 销毁(物理删除) * 销毁(物理删除)
@ -157,6 +193,17 @@ public interface DynamicTableMapper<T, P> {
* @param tableName 表名 * @param tableName 表名
* @param entity 示例对象 * @param entity 示例对象
*/ */
@UpdateProvider(type = DynamicTableSQLProvider.class, method = "destroyAllByExample") default void destroyAllByExample(String tableName, T entity) {
void destroyAllByExample(@Param("tableName") String tableName, @Param("entity") T entity); destroyAllByExample(tableName, entity, Logic.AND);
}
/**
* 根据示例批量销毁(物理删除)
*
* @param tableName 表名
* @param entity 示例对象
* @param logic 条件连接逻辑
*/
@DeleteProvider(type = DynamicTableSQLProvider.class, method = "destroyAllByExample")
void destroyAllByExample(@Param("tableName") String tableName, @Param("entity") T entity, @Param("logic") Logic logic);
} }

View File

@ -1,6 +1,8 @@
package com.imyeyu.spring.mapper; package com.imyeyu.spring.mapper;
import com.imyeyu.spring.bean.Logic;
import com.imyeyu.spring.util.RawSQLProvider; import com.imyeyu.spring.util.RawSQLProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider; import org.apache.ibatis.annotations.SelectProvider;
import java.util.List; import java.util.List;
@ -38,8 +40,19 @@ public interface RawMapper<T, P> {
* @param t 示例对象 * @param t 示例对象
* @return 数据对象 * @return 数据对象
*/ */
default T selectByExampleRaw(T t) {
return selectByExampleRaw(t, Logic.AND);
}
/**
* 根据示例查询单条数据
*
* @param t 示例对象
* @param logic 条件连接逻辑
* @return 数据对象
*/
@SelectProvider(type = RawSQLProvider.class, method = "selectByExample") @SelectProvider(type = RawSQLProvider.class, method = "selectByExample")
T selectByExampleRaw(T t); T selectByExampleRaw(@Param("entity") T t, @Param("logic") Logic logic);
/** /**
* 根据示例查询全部数据 * 根据示例查询全部数据
@ -47,6 +60,17 @@ public interface RawMapper<T, P> {
* @param t 示例对象 * @param t 示例对象
* @return 数据列表 * @return 数据列表
*/ */
@SelectProvider(type = RawSQLProvider.class, method = "selectAllByExample") default List<T> selectAllByExampleRaw(T t) {
List<T> selectAllByExampleRaw(T t); return selectAllByExampleRaw(t, Logic.AND);
}
/**
* 根据示例查询全部数据
*
* @param t 示例对象
* @param logic 条件连接逻辑
* @return 数据列表
*/
@SelectProvider(type = RawSQLProvider.class, method = "selectAllByExample")
List<T> selectAllByExampleRaw(@Param("entity") T t, @Param("logic") Logic logic);
} }

View File

@ -12,6 +12,7 @@ import com.imyeyu.spring.annotation.table.Id;
import com.imyeyu.spring.annotation.table.PageIgnore; import com.imyeyu.spring.annotation.table.PageIgnore;
import com.imyeyu.spring.annotation.table.Table; import com.imyeyu.spring.annotation.table.Table;
import com.imyeyu.spring.annotation.table.Transient; import com.imyeyu.spring.annotation.table.Transient;
import com.imyeyu.spring.bean.Logic;
import com.imyeyu.spring.bean.Page; import com.imyeyu.spring.bean.Page;
import com.imyeyu.spring.entity.Creatable; import com.imyeyu.spring.entity.Creatable;
import com.imyeyu.spring.entity.Deletable; import com.imyeyu.spring.entity.Deletable;
@ -143,12 +144,11 @@ public abstract class BaseSQLProvider {
String conditionClause = metaExample.fieldColumnList.stream() String conditionClause = metaExample.fieldColumnList.stream()
.filter(fc -> fc.isNotEmpty(obj)) .filter(fc -> fc.isNotEmpty(obj))
.map(fc -> "`%s` = '%s'".formatted(fc.columnName, fc.getAsString(obj))) .map(fc -> "`%s` = '%s'".formatted(fc.columnName, fc.getAsString(obj)))
.collect(Collectors.joining(" AND ")); .collect(Collectors.joining(" %s ".formatted(page.getEqualsLogic())));
if (TimiJava.isNotEmpty(conditionClause)) { if (TimiJava.isNotEmpty(conditionClause)) {
sql.append(" AND ").append(conditionClause); sql.append(" AND ").append(conditionClause);
} }
} }
// 模糊查询 // 模糊查询
if (TimiJava.isNotEmpty(page.getLikesExample())) { if (TimiJava.isNotEmpty(page.getLikesExample())) {
Object obj = page.getLikesExample(); Object obj = page.getLikesExample();
@ -156,7 +156,7 @@ public abstract class BaseSQLProvider {
String conditionClause = metaExample.fieldColumnList.stream() String conditionClause = metaExample.fieldColumnList.stream()
.filter(fc -> fc.isNotEmpty(obj)) .filter(fc -> fc.isNotEmpty(obj))
.map(fc -> "`%s` LIKE CONCAT('%%', '%s', '%%')".formatted(fc.columnName, fc.getAsString(obj))) .map(fc -> "`%s` LIKE CONCAT('%%', '%s', '%%')".formatted(fc.columnName, fc.getAsString(obj)))
.collect(Collectors.joining(" OR ")); .collect(Collectors.joining(" %s ".formatted(page.getLikesLogic())));
if (TimiJava.isNotEmpty(conditionClause)) { if (TimiJava.isNotEmpty(conditionClause)) {
sql.append(" AND (").append(conditionClause).append(')'); sql.append(" AND (").append(conditionClause).append(')');
} }
@ -197,13 +197,14 @@ public abstract class BaseSQLProvider {
* @param meta 实体元数据 * @param meta 实体元数据
* @param entity 示例实体 * @param entity 示例实体
* @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀 * @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀
* @param logic 条件连接逻辑
* @return 条件子句 * @return 条件子句
*/ */
protected String buildExampleConditions(EntityMeta meta, Object entity, String paramPrefix) { protected String buildExampleConditions(EntityMeta meta, Object entity, String paramPrefix, Logic logic) {
return meta.fieldColumnList.stream() return meta.fieldColumnList.stream()
.filter(fc -> fc.isNotEmpty(entity)) .filter(fc -> fc.isNotEmpty(entity))
.map(fc -> "`%s` = #{%s%s}".formatted(fc.columnName, paramPrefix, fc.fieldName)) .map(fc -> "`%s` = #{%s%s}".formatted(fc.columnName, paramPrefix, fc.fieldName))
.collect(Collectors.joining(" AND ")); .collect(Collectors.joining(" %s ".formatted(logic)));
} }
/** /**
@ -270,10 +271,11 @@ public abstract class BaseSQLProvider {
* @param tableName 表名 * @param tableName 表名
* @param entity 示例实体 * @param entity 示例实体
* @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀 * @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀
* @param logic 条件连接逻辑
* @return SQL * @return SQL
*/ */
protected String buildSelectAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix) { protected String buildSelectAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix, Logic logic) {
String conditionClause = buildExampleConditions(meta, entity, paramPrefix); String conditionClause = buildExampleConditions(meta, entity, paramPrefix, logic);
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append("SELECT %s FROM `%s` WHERE %s".formatted(meta.selectAllClause, tableName, conditionClause)); sql.append("SELECT %s FROM `%s` WHERE %s".formatted(meta.selectAllClause, tableName, conditionClause));
@ -372,9 +374,10 @@ public abstract class BaseSQLProvider {
* @param tableName 表名 * @param tableName 表名
* @param entity 示例实体 * @param entity 示例实体
* @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀 * @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀
* @param logic 条件连接逻辑
* @return SQL * @return SQL
*/ */
protected String buildDeleteAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix) { protected String buildDeleteAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix, Logic logic) {
TimiException.required(meta.canDelete, "not allow delete for %s".formatted(meta.entityClass)); TimiException.required(meta.canDelete, "not allow delete for %s".formatted(meta.entityClass));
FieldColumn deleteColumn = meta.getFieldColumnList().stream() FieldColumn deleteColumn = meta.getFieldColumnList().stream()
@ -389,7 +392,7 @@ public abstract class BaseSQLProvider {
.filter(FieldColumn::isNotId) .filter(FieldColumn::isNotId)
.filter(fc -> fc.isNotEmpty(entity)) .filter(fc -> fc.isNotEmpty(entity))
.map(fc -> "`%s` = #{%s%s}".formatted(fc.columnName, paramPrefix, fc.fieldName)) .map(fc -> "`%s` = #{%s%s}".formatted(fc.columnName, paramPrefix, fc.fieldName))
.collect(Collectors.joining(" AND ")); .collect(Collectors.joining(" %s ".formatted(logic)));
StringBuilder sql = new StringBuilder("UPDATE `%s` SET `%s` = ".formatted(tableName, deleteColumn.getColumnName())); StringBuilder sql = new StringBuilder("UPDATE `%s` SET `%s` = ".formatted(tableName, deleteColumn.getColumnName()));
sql.append("'").append(switch (deleteColumn.deleteColumnType) { sql.append("'").append(switch (deleteColumn.deleteColumnType) {
case UNIX -> Time.now(); case UNIX -> Time.now();
@ -415,21 +418,22 @@ public abstract class BaseSQLProvider {
} }
/** /**
* 构建批量逻辑删除 SQL * 构建批量物理删除 SQL
* *
* @param meta 实体元数据 * @param meta 实体元数据
* @param tableName 表名 * @param tableName 表名
* @param entity 示例实体 * @param entity 示例实体
* @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀 * @param paramPrefix 参数前缀(如 "entity."),空字符串表示无前缀
* @param logic 条件连接逻辑
* @return SQL * @return SQL
*/ */
protected String buildDestroyAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix) { protected String buildDestroyAllByExampleSQL(EntityMeta meta, String tableName, Object entity, String paramPrefix, Logic logic) {
TimiException.required(meta.canDestroy, "not allow destroy for %s".formatted(meta.entityClass)); TimiException.required(meta.canDestroy, "not allow destroy for %s".formatted(meta.entityClass));
String destroyClause = meta.fieldColumnList.stream() String destroyClause = meta.fieldColumnList.stream()
.filter(FieldColumn::isNotId) .filter(FieldColumn::isNotId)
.filter(fc -> fc.isNotEmpty(entity)) .filter(fc -> fc.isNotEmpty(entity))
.map(fc -> "`%s` = #{%s%s}".formatted(fc.columnName, paramPrefix, fc.fieldName)) .map(fc -> "`%s` = #{%s%s}".formatted(fc.columnName, paramPrefix, fc.fieldName))
.collect(Collectors.joining(" AND ")); .collect(Collectors.joining(" %s ".formatted(logic)));
return "DELETE FROM `%s` WHERE %s".formatted(tableName, destroyClause); return "DELETE FROM `%s` WHERE %s".formatted(tableName, destroyClause);
} }

View File

@ -1,5 +1,6 @@
package com.imyeyu.spring.util; package com.imyeyu.spring.util;
import com.imyeyu.spring.bean.Logic;
import com.imyeyu.spring.bean.Page; import com.imyeyu.spring.bean.Page;
import com.imyeyu.spring.mapper.BaseMapper; import com.imyeyu.spring.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
@ -89,10 +90,11 @@ public class DynamicTableSQLProvider extends BaseSQLProvider {
* @param context 代理器上下文 * @param context 代理器上下文
* @param tableName 表名 * @param tableName 表名
* @param entity 实体 * @param entity 实体
* @param logic 条件连接逻辑
* @return SQL * @return SQL
*/ */
public String selectByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) { public String selectByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
return selectAllByExample(context, tableName, entity) + BaseMapper.LIMIT_1; return selectAllByExample(context, tableName, entity, logic) + BaseMapper.LIMIT_1;
} }
/** /**
@ -101,11 +103,12 @@ public class DynamicTableSQLProvider extends BaseSQLProvider {
* @param context 代理器上下文 * @param context 代理器上下文
* @param tableName 表名 * @param tableName 表名
* @param entity 实体 * @param entity 实体
* @param logic 条件连接逻辑
* @return SQL * @return SQL
*/ */
public String selectAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) { public String selectAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
EntityMeta meta = getEntityMeta(context); EntityMeta meta = getEntityMeta(context);
return buildSelectAllByExampleSQL(meta, tableName, entity, "entity."); return buildSelectAllByExampleSQL(meta, tableName, entity, "entity.", logic);
} }
/** /**
@ -153,11 +156,12 @@ public class DynamicTableSQLProvider extends BaseSQLProvider {
* @param context 代理器上下文 * @param context 代理器上下文
* @param tableName 表名 * @param tableName 表名
* @param entity 实体 * @param entity 实体
* @param logic 条件连接逻辑
* @return SQL * @return SQL
*/ */
public String deleteAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) { public String deleteAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
EntityMeta meta = getEntityMeta(context); EntityMeta meta = getEntityMeta(context);
return buildDeleteAllByExampleSQL(meta, tableName, entity, "entity."); return buildDeleteAllByExampleSQL(meta, tableName, entity, "entity.", logic);
} }
/** /**
@ -174,15 +178,16 @@ public class DynamicTableSQLProvider extends BaseSQLProvider {
} }
/** /**
* 根据示例批量逻辑删除 * 根据示例批量物理删除
* *
* @param context 代理器上下文 * @param context 代理器上下文
* @param tableName 表名 * @param tableName 表名
* @param entity 实体 * @param entity 实体
* @param logic 条件连接逻辑
* @return SQL * @return SQL
*/ */
public String destroyAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) { public String destroyAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
EntityMeta meta = getEntityMeta(context); EntityMeta meta = getEntityMeta(context);
return buildDestroyAllByExampleSQL(meta, tableName, entity, "entity."); return buildDestroyAllByExampleSQL(meta, tableName, entity, "entity.", logic);
} }
} }

View File

@ -1,7 +1,9 @@
package com.imyeyu.spring.util; package com.imyeyu.spring.util;
import com.imyeyu.java.bean.timi.TimiException; import com.imyeyu.java.bean.timi.TimiException;
import com.imyeyu.spring.bean.Logic;
import com.imyeyu.spring.mapper.BaseMapper; import com.imyeyu.spring.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.builder.annotation.ProviderContext; import org.apache.ibatis.builder.annotation.ProviderContext;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -28,17 +30,17 @@ public class RawSQLProvider extends SQLProvider {
} }
@Override @Override
public String selectByExample(Object entity) { public String selectByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
return selectAllByExample(entity) + BaseMapper.LIMIT_1; return selectAllByExample(entity, logic) + BaseMapper.LIMIT_1;
} }
@Override @Override
public String selectAllByExample(Object entity) { public String selectAllByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
EntityMeta meta = getEntityMeta(entity.getClass()); EntityMeta meta = getEntityMeta(entity.getClass());
String conditionClause = meta.fieldColumnList.stream() String conditionClause = meta.fieldColumnList.stream()
.filter(fc -> fc.isNotEmpty(entity)) .filter(fc -> fc.isNotEmpty(entity))
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName)) .map(fc -> "`%s` = #{entity.%s}".formatted(fc.columnName, fc.fieldName))
.collect(Collectors.joining(" AND ")); .collect(Collectors.joining(" %s ".formatted(logic)));
return "SELECT %s FROM `%s` WHERE %s".formatted(meta.selectAllClause, meta.table, conditionClause); return "SELECT %s FROM `%s` WHERE %s".formatted(meta.selectAllClause, meta.table, conditionClause);
} }
} }

View File

@ -1,5 +1,6 @@
package com.imyeyu.spring.util; package com.imyeyu.spring.util;
import com.imyeyu.spring.bean.Logic;
import com.imyeyu.spring.bean.Page; import com.imyeyu.spring.bean.Page;
import com.imyeyu.spring.mapper.BaseMapper; import com.imyeyu.spring.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
@ -89,21 +90,23 @@ public class SQLProvider extends BaseSQLProvider {
* 根据实体非空字段使用等号查询 * 根据实体非空字段使用等号查询
* *
* @param entity 实体 * @param entity 实体
* @param logic 条件连接逻辑
* @return SQL * @return SQL
*/ */
public String selectByExample(Object entity) { public String selectByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
return selectAllByExample(entity) + BaseMapper.LIMIT_1; return selectAllByExample(entity, logic) + BaseMapper.LIMIT_1;
} }
/** /**
* 根据实体非空字段使用等号查询 * 根据实体非空字段使用等号查询
* *
* @param entity 实体 * @param entity 实体
* @param logic 条件连接逻辑
* @return SQL * @return SQL
*/ */
public String selectAllByExample(Object entity) { public String selectAllByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
EntityMeta meta = getEntityMeta(entity.getClass()); EntityMeta meta = getEntityMeta(entity.getClass());
return buildSelectAllByExampleSQL(meta, meta.table, entity, ""); return buildSelectAllByExampleSQL(meta, meta.table, entity, "entity.", logic);
} }
/** /**
@ -144,11 +147,12 @@ public class SQLProvider extends BaseSQLProvider {
* 根据示例批量逻辑删除 * 根据示例批量逻辑删除
* *
* @param entity 实体 * @param entity 实体
* @param logic 条件连接逻辑
* @return SQL * @return SQL
*/ */
public String deleteAllByExample(Object entity) { public String deleteAllByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
EntityMeta meta = getEntityMeta(entity.getClass()); EntityMeta meta = getEntityMeta(entity.getClass());
return buildDeleteAllByExampleSQL(meta, meta.table, entity, ""); return buildDeleteAllByExampleSQL(meta, meta.table, entity, "entity.", logic);
} }
/** /**
@ -167,10 +171,11 @@ public class SQLProvider extends BaseSQLProvider {
* 根据示例批量销毁(物理删除) * 根据示例批量销毁(物理删除)
* *
* @param entity 实体 * @param entity 实体
* @param logic 条件连接逻辑
* @return SQL * @return SQL
*/ */
public String destroyAllByExample(Object entity) { public String destroyAllByExample(@Param("entity") Object entity, @Param("logic") Logic logic) {
EntityMeta meta = getEntityMeta(entity.getClass()); EntityMeta meta = getEntityMeta(entity.getClass());
return buildDestroyAllByExampleSQL(meta, meta.table, entity, ""); return buildDestroyAllByExampleSQL(meta, meta.table, entity, "entity.", logic);
} }
} }