feat: 代码暂存,合并master分支
This commit is contained in:
commit
4cceda5734
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,6 +4,8 @@
|
||||
**/*.iml
|
||||
**/.idea/**
|
||||
|
||||
.flattened-pom.xml
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>flyfish-framework</artifactId>
|
||||
<groupId>com.flyfish.framework</groupId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@ -17,14 +17,20 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-relational</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-mongodb</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.flyfish.framework</groupId>
|
||||
<artifactId>flyfish-web</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -4,8 +4,8 @@ import com.flyfish.framework.annotations.EnumValue;
|
||||
import com.flyfish.framework.annotations.Order;
|
||||
import com.flyfish.framework.annotations.Property;
|
||||
import com.flyfish.framework.approval.enums.ApproveStatus;
|
||||
import com.flyfish.framework.builder.CriteriaBuilder;
|
||||
import com.flyfish.framework.domain.authorized.AuthorizedQo;
|
||||
import com.flyfish.framework.query.QueryDefinition;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@ -24,7 +24,7 @@ public class ApprovalDomainQo<T extends ApprovalDomain> extends AuthorizedQo<T>
|
||||
private String status;
|
||||
|
||||
@Override
|
||||
public CriteriaBuilder<T> criteriaBuilder() {
|
||||
return super.criteriaBuilder().with("status", "approveStatus");
|
||||
public QueryDefinition queryBuilder() {
|
||||
return super.queryBuilder().mutate().and(T::getApproveStatus).eq(status);
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,9 @@ import com.flyfish.framework.domain.base.AuditDomain;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
import org.springframework.data.mongodb.core.mapping.Field;
|
||||
import org.springframework.data.relational.core.mapping.Column;
|
||||
import org.springframework.data.relational.core.mapping.Table;
|
||||
|
||||
/**
|
||||
* 审批记录
|
||||
@ -12,11 +15,13 @@ import org.springframework.data.mongodb.core.mapping.Document;
|
||||
* @author wangyu
|
||||
*/
|
||||
@Document(collection = "approve-records")
|
||||
@Table("approve_records")
|
||||
@Getter
|
||||
@Setter
|
||||
public class ApproveRecord extends AuditDomain {
|
||||
|
||||
// 是否已审批
|
||||
@Column
|
||||
private Boolean approved;
|
||||
|
||||
// 模块
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.flyfish.framework.approval.domain.record;
|
||||
|
||||
import com.flyfish.framework.builder.CriteriaBuilder;
|
||||
import com.flyfish.framework.domain.base.NameLikeQo;
|
||||
import com.flyfish.framework.query.QueryDefinition;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@ -27,7 +27,12 @@ public class ApproveRecordQo extends NameLikeQo<ApproveRecord> {
|
||||
private String approver;
|
||||
|
||||
@Override
|
||||
public CriteriaBuilder<ApproveRecord> criteriaBuilder() {
|
||||
return super.criteriaBuilder().with("module", "dataId", "approved", "approver");
|
||||
public QueryDefinition queryBuilder() {
|
||||
return super.queryBuilder()
|
||||
.mutate()
|
||||
.and(ApproveRecord::getModule).eq(module)
|
||||
.and(ApproveRecord::getDataId).eq(dataId)
|
||||
.and(ApproveRecord::getApproved).eq(approved)
|
||||
.and(ApproveRecord::getApprover).eq(approver);
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>flyfish-framework</artifactId>
|
||||
<groupId>com.flyfish.framework</groupId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@ -17,10 +17,20 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-relational</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-mongodb</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.flyfish.framework</groupId>
|
||||
<artifactId>flyfish-web</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -6,6 +6,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
import org.springframework.data.relational.core.mapping.Table;
|
||||
|
||||
/**
|
||||
* 系统备份
|
||||
@ -15,6 +16,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
|
||||
@Getter
|
||||
@Setter
|
||||
@Document(collection = "backups")
|
||||
@Table("backups")
|
||||
public class Backup extends AuditDomain {
|
||||
|
||||
// 文件路径
|
||||
|
@ -7,6 +7,7 @@ import java.util.Date;
|
||||
|
||||
/**
|
||||
* 版本号实体
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
@Data
|
||||
|
@ -3,6 +3,8 @@ package com.flyfish.framework.backup.scheduler;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.flyfish.framework.backup.domain.Backup;
|
||||
import com.flyfish.framework.domain.base.DomainService;
|
||||
import com.flyfish.framework.query.Query;
|
||||
import com.flyfish.framework.repository.ReactiveEntityOperations;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -12,8 +14,8 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
|
||||
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.util.CastUtils;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
@ -22,7 +24,6 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.AsynchronousFileChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@ -45,9 +46,9 @@ public class BackupScheduler {
|
||||
// data buffer 工厂
|
||||
private final DataBufferFactory factory = new DefaultDataBufferFactory();
|
||||
// 用于注入所有集合名称
|
||||
private List<MongoEntityInformation<?, String>> collections;
|
||||
private List<EntityInformation<?, String>> collections;
|
||||
// 异步的mongo操作,可以快速备份
|
||||
private ReactiveMongoOperations operations;
|
||||
private ReactiveEntityOperations operations;
|
||||
// 备份路径
|
||||
private String backupPath = "/opt/flyfish/backup";
|
||||
|
||||
@ -57,8 +58,8 @@ public class BackupScheduler {
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setOperations(ReactiveMongoOperations reactiveMongoOperations) {
|
||||
this.operations = reactiveMongoOperations;
|
||||
public void setOperations(ReactiveEntityOperations reactiveEntityOperations) {
|
||||
this.operations = reactiveEntityOperations;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
@ -92,8 +93,8 @@ public class BackupScheduler {
|
||||
operations.save(backup)
|
||||
.thenMany(Flux.fromIterable(this.collections))
|
||||
.flatMap(info -> operations
|
||||
.findAll(info.getJavaType(), info.getCollectionName()).collectList()
|
||||
.map(list -> new BackupIndex.BackupContent(info.getCollectionName(), JSON.toJSONBytes(list)))
|
||||
.findAll(Query.empty(), CastUtils.cast(info)).collectList()
|
||||
.map(list -> new BackupIndex.BackupContent(info.getJavaType().getSimpleName(), JSON.toJSONBytes(list)))
|
||||
)
|
||||
.flatMap(content -> writeContents(content, parent))
|
||||
.collectList()
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>flyfish-framework</artifactId>
|
||||
<groupId>com.flyfish.framework</groupId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -0,0 +1,36 @@
|
||||
package com.flyfish.framework.utils;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* LRU (least recently used)最近最久未使用缓存<br>
|
||||
* 根据使用时间来判定对象是否被持续缓存<br>
|
||||
* 当对象被访问时放入缓存,当缓存满了,最久未被使用的对象将被移除。<br>
|
||||
* 此缓存基于LinkedHashMap,因此当被缓存的对象每被访问一次,这个对象的key就到链表头部。<br>
|
||||
* 这个算法简单并且非常快,他比FIFO有一个显著优势是经常使用的对象不太可能被移除缓存。<br>
|
||||
* 缺点是当缓存满时,不能被很快的访问。
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @author wangyu
|
||||
*/
|
||||
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private final int maxSize;
|
||||
|
||||
public LRUCache(int maxSize) {
|
||||
this(maxSize, 16, 0.75f, false);
|
||||
}
|
||||
|
||||
public LRUCache(int maxSize, int initialCapacity, float loadFactor, boolean accessOrder) {
|
||||
super(initialCapacity, loadFactor, accessOrder);
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
||||
return this.size() > this.maxSize;
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.flyfish.framework.utils;
|
||||
|
||||
public interface Supportable<T> {
|
||||
|
||||
/**
|
||||
* 判断是否支持实体
|
||||
*
|
||||
* @param entity 实体信息
|
||||
* @return 结果
|
||||
*/
|
||||
boolean supports(T entity);
|
||||
}
|
16
flyfish-data/README.md
Normal file
16
flyfish-data/README.md
Normal file
@ -0,0 +1,16 @@
|
||||
# 核心数据框架
|
||||
|
||||
本模块是flyfish framework的最核心能力
|
||||
其提供无感知的多种数据源支持,并完整适配了mongodb和rdbms的查询表现,
|
||||
用户只需要用一套api即可完成数据的查询或修改,无需为了底层实现而大费周章重新开发。
|
||||
|
||||
## 核心架构
|
||||
1. 基于Spring SPI模式,声明式注入查询实现工厂,动态替换查询构建逻辑
|
||||
2. 按需引入,不浪费任何依赖,节省空间。使用maven的按需引入模式,让打包后的结果不必因为重量级的框架而变得冗余
|
||||
3. 指哪打哪,稳定快速。集成最新版本的spring mongo引擎和spring data r2dbc
|
||||
4. 核心框架广泛使用,保证"0bug"。
|
||||
|
||||
## 无限扩展
|
||||
1. 根据实际业务需求,采用mongo、jpa、r2dbc的方式,动态选择实现,而业务和底层存储实现无关,完全解耦
|
||||
2. 健壮的架构,能够支撑99%的业务
|
||||
3. 完善的关联机制,多表关联做到全自动化
|
47
flyfish-data/flyfish-data-common/pom.xml
Normal file
47
flyfish-data/flyfish-data-common/pom.xml
Normal file
@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>flyfish-data</artifactId>
|
||||
<groupId>com.flyfish.framework</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>flyfish-data-common</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.flyfish.framework</groupId>
|
||||
<artifactId>flyfish-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.flyfish.framework</groupId>
|
||||
<artifactId>flyfish-data-domain</artifactId>
|
||||
<version>${revision}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.ulisesbocchio</groupId>
|
||||
<artifactId>jasypt-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,10 @@
|
||||
package com.flyfish.framework.builder;
|
||||
|
||||
/**
|
||||
* 查询构建器提供者
|
||||
* @author wangyu-
|
||||
*/
|
||||
public interface CriteriaBuilderProvider {
|
||||
|
||||
|
||||
}
|
@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
@ -17,7 +18,7 @@ import org.springframework.context.annotation.Bean;
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
@AutoConfigureBefore(MongoDataAutoConfiguration.class)
|
||||
@AutoConfigureBefore({R2dbcDataAutoConfiguration.class, MongoDataAutoConfiguration.class})
|
||||
@ConditionalOnProperty("flyfish.jasypt.key")
|
||||
@EnableEncryptableProperties
|
||||
public class DecryptConfig {
|
||||
@ -34,7 +35,6 @@ public class DecryptConfig {
|
||||
return new AESEncryptablePropertyResolver(password);
|
||||
}
|
||||
|
||||
|
||||
@Bean(name = "encryptablePropertyFilter")
|
||||
public EncryptablePropertyFilter encryptablePropertyFilter() {
|
||||
return (source, name) -> StringUtils.endsWithIgnoreCase(name, PASSWORD_KEY);
|
@ -1,11 +1,11 @@
|
||||
package com.flyfish.framework.domain.authorized;
|
||||
|
||||
import com.flyfish.framework.builder.CriteriaBuilder;
|
||||
import com.flyfish.framework.domain.base.NameLikeQo;
|
||||
import com.flyfish.framework.domain.po.Department;
|
||||
import com.flyfish.framework.query.LambdaQueryChain;
|
||||
import com.flyfish.framework.query.Queries;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
@ -38,20 +38,17 @@ public abstract class AbstractAuthorizedQo<T extends AuthorizedDomain> extends N
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
protected Criteria withPublished() {
|
||||
protected LambdaQueryChain<T> withPublished() {
|
||||
if (null != published) {
|
||||
if (BooleanUtils.isTrue(published)) {
|
||||
return Criteria.where("published").is(true);
|
||||
return Queries.where(T::getPublished).eq(true);
|
||||
}
|
||||
return Criteria.where("$or").is(
|
||||
CriteriaBuilder.createCriteriaList(
|
||||
Criteria.where("published").isNull(),
|
||||
Criteria.where("published").is(false)
|
||||
)
|
||||
return Queries.within(
|
||||
Queries.where(T::getPublished).isNull().or(T::getPublished).eq(false)
|
||||
);
|
||||
} else {
|
||||
// 未指定发布状态,查询已发布内容
|
||||
return Criteria.where("published").is(true);
|
||||
return Queries.where(T::getPublished).eq(true);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
package com.flyfish.framework.domain.authorized;
|
||||
|
||||
import com.flyfish.framework.enums.UserType;
|
||||
import com.flyfish.framework.query.QueryDefinition;
|
||||
import com.flyfish.framework.query.Queries;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
|
||||
/**
|
||||
* 带鉴权的查询实体,主要以部门隔绝
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class AuthorizedQo<T extends AuthorizedDomain> extends AbstractAuthorizedQo<T> {
|
||||
|
||||
@Override
|
||||
public QueryDefinition queryBuilder() {
|
||||
// 超级管理员拥有查看所有草稿的权限
|
||||
if (user.getType() == UserType.SUPER_ADMIN) {
|
||||
return super.queryBuilder().mutate().and(this.withPublished());
|
||||
}
|
||||
// 查询草稿,只查询自己的
|
||||
if (BooleanUtils.isFalse(published)) {
|
||||
return super.queryBuilder()
|
||||
.mutate()
|
||||
.and(this.withPublished())
|
||||
.and(Queries.where(T::getCreatorId).eq(user.getId()));
|
||||
}
|
||||
// 普通查询,根据权限配置查询
|
||||
return super.queryBuilder()
|
||||
.mutate()
|
||||
.and(this.withPublished())
|
||||
.and(Queries.where(T::getAuthorizeId).in(authorizeIds())
|
||||
.or(Queries.where(T::getCreatorId).eq(user.getId())
|
||||
.and(T::getAuthorizeId).in(((AuthorizedUserDetails) user).getVisibleDeparts()))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.flyfish.framework.domain.authorized.advanced;
|
||||
|
||||
import com.flyfish.framework.domain.authorized.AbstractAuthorizedQo;
|
||||
import com.flyfish.framework.domain.authorized.AuthorizedUserDetails;
|
||||
import com.flyfish.framework.enums.UserType;
|
||||
import com.flyfish.framework.query.QueryDefinition;
|
||||
import com.flyfish.framework.query.Queries;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
|
||||
/**
|
||||
* 拥有归属权的授权查询
|
||||
*
|
||||
* @param <T> 泛型
|
||||
* @author wangyu
|
||||
* 将会额外查询归属者的内容
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class OwnedAuthorizedQo<T extends OwnedAuthorizedDomain> extends AbstractAuthorizedQo<T> {
|
||||
|
||||
@Override
|
||||
public QueryDefinition queryBuilder() {
|
||||
// 超级管理员拥有查看所有草稿的权限
|
||||
if (user.getType() == UserType.SUPER_ADMIN) {
|
||||
return super.queryBuilder().mutate().and(this.withPublished());
|
||||
}
|
||||
// 查询草稿,只查询自己的
|
||||
if (BooleanUtils.isFalse(published)) {
|
||||
return super.queryBuilder()
|
||||
.mutate()
|
||||
.and(this.withPublished())
|
||||
// 此处原意为使用用户id匹配owners列中的id,修改后框架会自动判定并解包,存在耦合性
|
||||
.and(Queries
|
||||
.where(T::getCreatorId).eq(user.getId())
|
||||
.or(T::getOwners).hasId(user.getId()));
|
||||
}
|
||||
// 普通查询,根据权限配置查询
|
||||
return super.queryBuilder()
|
||||
.mutate()
|
||||
.and(this.withPublished())
|
||||
.and(() -> Queries.where(T::getAuthorizeId).in(authorizeIds())
|
||||
.or(Queries.where(T::getCreatorId).eq(user.getId())
|
||||
.and(T::getAuthorizeId).in(((AuthorizedUserDetails) user).getVisibleDeparts()))
|
||||
.or(Queries.where(T::getOwners).hasId(user.getId()))
|
||||
);
|
||||
}
|
||||
}
|
@ -1,20 +1,28 @@
|
||||
package com.flyfish.framework.domain.base;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.flyfish.framework.builder.CriteriaBuilder;
|
||||
import com.flyfish.framework.query.Queries;
|
||||
import com.flyfish.framework.query.Query;
|
||||
import com.flyfish.framework.query.QueryDefinition;
|
||||
import com.flyfish.framework.utils.CopyUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
|
||||
|
||||
/**
|
||||
* 基本的查询实体
|
||||
*
|
||||
* @author Mr.Wang
|
||||
*/
|
||||
@Slf4j
|
||||
public class BaseQo<T extends Domain> implements Qo<T> {
|
||||
|
||||
protected Pageable pageable;
|
||||
@ -100,12 +108,20 @@ public class BaseQo<T extends Domain> implements Qo<T> {
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public Criteria getCriteria() {
|
||||
CriteriaBuilder<T> criteriaBuilder = criteriaBuilder();
|
||||
if (null != criteriaBuilder) {
|
||||
return criteriaBuilder.build();
|
||||
public <C> Optional<C> getQuery(EntityInformation<T, String> entityInformation) {
|
||||
Query query = Query.empty();
|
||||
QueryDefinition definition = queryBuilder();
|
||||
if (null != definition) {
|
||||
// 查询定义存在时,使用查询定义
|
||||
query.setDefinition(definition);
|
||||
} else {
|
||||
// 不存在查询定义,取得example,若不存在example,则使用probe
|
||||
query.setExample(defaultIfNull(getExample(), getProbe()));
|
||||
}
|
||||
return null;
|
||||
query.setSort(sorts());
|
||||
query.setPageable(getPageable());
|
||||
query.setFields(getFields());
|
||||
return Optional.of(Queries.convert(query, entityInformation));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,7 +131,7 @@ public class BaseQo<T extends Domain> implements Qo<T> {
|
||||
*/
|
||||
@Override
|
||||
public List<String> getFields() {
|
||||
return null;
|
||||
return fields;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,7 +139,7 @@ public class BaseQo<T extends Domain> implements Qo<T> {
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public CriteriaBuilder<T> criteriaBuilder() {
|
||||
public QueryDefinition queryBuilder() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -144,4 +160,29 @@ public class BaseQo<T extends Domain> implements Qo<T> {
|
||||
public Sort sorts() {
|
||||
return Sort.by(Sort.Order.desc("modifyTime"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前查询是否为空
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
QueryDefinition definition = queryBuilder();
|
||||
Example<T> example = getExample();
|
||||
return null == example && (null == definition || definition.isEmpty());
|
||||
}
|
||||
|
||||
private Example<T> getProbe() {
|
||||
Class<T> type = pojoType();
|
||||
if (null != type && !Object.class.equals(type)) {
|
||||
try {
|
||||
T pojo = CopyUtils.copyQueryProps(this, type.newInstance());
|
||||
return Example.of(pojo);
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
log.warn("尝试实例化bean失败!", e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
package com.flyfish.framework.domain.base;
|
||||
|
||||
import com.flyfish.framework.builder.CriteriaBuilder;
|
||||
import com.flyfish.framework.query.QueryDefinition;
|
||||
import com.flyfish.framework.query.Queries;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -46,22 +46,24 @@ public class NameLikeQo<T extends Domain> extends BaseQo<T> {
|
||||
private String distinct;
|
||||
|
||||
@Override
|
||||
public CriteriaBuilder<T> criteriaBuilder() {
|
||||
return CriteriaBuilder.accept(this)
|
||||
.with("name", CriteriaBuilder.Builders.LIKE)
|
||||
.with("enable", "code", "creatorId", "modifierId")
|
||||
.with("ids", "id", CriteriaBuilder.Builders.IN)
|
||||
.with("excludeId", "id", Criteria::ne)
|
||||
.with("createTimeRange", "createTime", CriteriaBuilder.Builders.DATE_RANGE)
|
||||
.with("modifyTimeRange", "modifyTime", CriteriaBuilder.Builders.DATE_RANGE);
|
||||
public QueryDefinition queryBuilder() {
|
||||
return Queries.where("name").like(name)
|
||||
.and("enable").eq(enable)
|
||||
.and("code").eq(code)
|
||||
.and("creatorId").eq(creatorId)
|
||||
.and("modifierId").eq(modifierId)
|
||||
.and("id").in(ids)
|
||||
.and("id").ne(excludeId)
|
||||
.and("createTime").between(createTimeRange)
|
||||
.and("modifyTime").between(modifyTimeRange);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Sort sorts() {
|
||||
if (StringUtils.isEmpty(sort)) {
|
||||
return super.sorts();
|
||||
if (StringUtils.hasText(sort)) {
|
||||
return Sort.by(Sort.Order.desc(sort));
|
||||
}
|
||||
return Sort.by(Sort.Order.desc(sort));
|
||||
return super.sorts();
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.flyfish.framework.domain;
|
||||
/**
|
||||
* 提供应用级别的实体
|
||||
*/
|
@ -0,0 +1,37 @@
|
||||
package com.flyfish.framework.domain.tree;
|
||||
|
||||
import com.flyfish.framework.domain.base.NameLikeQo;
|
||||
import com.flyfish.framework.query.QueryDefinition;
|
||||
import com.flyfish.framework.query.LambdaQueryChain;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 属性菜单的qo
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class TreeQo<T extends TreeDomain<T>> extends NameLikeQo<T> {
|
||||
|
||||
private Integer depth;
|
||||
|
||||
private String parentId;
|
||||
|
||||
private List<String> parentIds;
|
||||
|
||||
private Boolean recursive;
|
||||
|
||||
@Override
|
||||
public QueryDefinition queryBuilder() {
|
||||
LambdaQueryChain<T> chain = super.queryBuilder().mutate().and(T::getDepth).eq(depth);
|
||||
if (BooleanUtils.isTrue(recursive)) {
|
||||
chain.and(T::getParentIds).has(parentId).and(T::getParentIds).in(parentId);
|
||||
} else {
|
||||
chain.and(T::getParentId).eq(parentId).and(T::getParentId).in(parentIds);
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
package com.flyfish.framework.query;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.query.Queries.Combinator;
|
||||
import com.flyfish.framework.query.support.DomainFunction;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* lambda查询条件
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface LambdaQueryChain<T extends Domain> extends QueryChain<LambdaQueryChain<T>, DomainFunction<T>> {
|
||||
|
||||
/**
|
||||
* 以且连接下一个字段
|
||||
*
|
||||
* @param column 列
|
||||
* @return 查询条件
|
||||
*/
|
||||
@Override
|
||||
QueryCondition<LambdaQueryChain<T>> and(DomainFunction<T> column);
|
||||
|
||||
/**
|
||||
* 直接拼接提供者,此处懒加载,最终build才会执行
|
||||
*
|
||||
* @param supplier 提供者
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> LambdaQueryChain<T> and(Supplier<QueryChain<V, ?>> supplier);
|
||||
|
||||
/**
|
||||
* 条件列表
|
||||
*
|
||||
* @param chain 多个条件们
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> LambdaQueryChain<T> and(QueryChain<V, ?> chain);
|
||||
|
||||
/**
|
||||
* 多个嵌套子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
default <V extends QueryChain<V, ?>> LambdaQueryChain<T> and(List<QueryChain<V, ?>> chains) {
|
||||
return QueryChain.super.and(chains);
|
||||
}
|
||||
|
||||
/**
|
||||
* 多个嵌套子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @param combinator 各个链条之间的连接方式
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> LambdaQueryChain<T> and(List<QueryChain<V, ?>> chains, Combinator combinator);
|
||||
|
||||
/**
|
||||
* 以或连接下一个字段
|
||||
*
|
||||
* @param column 列
|
||||
* @return 查询条件
|
||||
*/
|
||||
@Override
|
||||
QueryCondition<LambdaQueryChain<T>> or(DomainFunction<T> column);
|
||||
|
||||
/**
|
||||
* 以或连接条件列表
|
||||
*
|
||||
* @param chain 多个条件们
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> LambdaQueryChain<T> or(QueryChain<V, ?> chain);
|
||||
|
||||
/**
|
||||
* 以或连接嵌套多个子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @param combinator 各个链条之间的连接方式
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> LambdaQueryChain<T> or(List<QueryChain<V, ?>> chains, Combinator combinator);
|
||||
|
||||
/**
|
||||
* 直接拼接提供者,此处懒加载,最终build才会执行
|
||||
*
|
||||
* @param supplier 提供者
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> LambdaQueryChain<T> or(Supplier<QueryChain<V, ?>> supplier);
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
package com.flyfish.framework.query;
|
||||
|
||||
import com.flyfish.framework.query.Queries.Combinator;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 查询链条
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface NamedQueryChain extends QueryChain<NamedQueryChain, String> {
|
||||
|
||||
/**
|
||||
* 以且连接下一个字段
|
||||
*
|
||||
* @param column 列
|
||||
* @return 查询条件
|
||||
*/
|
||||
@Override
|
||||
QueryCondition<NamedQueryChain> and(String column);
|
||||
|
||||
/**
|
||||
* 直接拼接提供者,此处懒加载,最终build才会执行
|
||||
*
|
||||
* @param supplier 提供者
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> NamedQueryChain and(Supplier<QueryChain<V, ?>> supplier);
|
||||
|
||||
/**
|
||||
* 条件列表
|
||||
*
|
||||
* @param chain 多个条件们
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> NamedQueryChain and(QueryChain<V, ?> chain);
|
||||
|
||||
/**
|
||||
* 多个嵌套子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
default <V extends QueryChain<V, ?>> NamedQueryChain and(List<QueryChain<V, ?>> chains) {
|
||||
return QueryChain.super.and(chains);
|
||||
}
|
||||
|
||||
/**
|
||||
* 多个嵌套子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @param combinator 各个链条之间的连接方式
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> NamedQueryChain and(List<QueryChain<V, ?>> chains, Combinator combinator);
|
||||
|
||||
/**
|
||||
* 以或连接下一个字段
|
||||
*
|
||||
* @param column 列
|
||||
* @return 查询条件
|
||||
*/
|
||||
@Override
|
||||
QueryCondition<NamedQueryChain> or(String column);
|
||||
|
||||
/**
|
||||
* 以或连接条件列表
|
||||
*
|
||||
* @param chain 多个条件们
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> NamedQueryChain or(QueryChain<V, ?> chain);
|
||||
|
||||
/**
|
||||
* 直接拼接提供者,此处懒加载,最终build才会执行
|
||||
*
|
||||
* @param supplier 提供者
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> NamedQueryChain or(Supplier<QueryChain<V, ?>> supplier);
|
||||
|
||||
/**
|
||||
* 以或连接嵌套多个子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @param combinator 各个链条之间的连接方式
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
<V extends QueryChain<V, ?>> NamedQueryChain or(List<QueryChain<V, ?>> chains, Combinator combinator);
|
||||
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
package com.flyfish.framework.query;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.query.chain.DefaultLambdaQueryChain;
|
||||
import com.flyfish.framework.query.chain.DefaultNamedQueryChain;
|
||||
import com.flyfish.framework.query.support.DomainFunction;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
|
||||
/**
|
||||
* 查询工具类
|
||||
* 基于Fluent API风格实现
|
||||
* 底层基于适配器兼容各种数据库,包括关系型数据库和
|
||||
* 自动判定空值,空值默认情况不会进行查询
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public final class Queries {
|
||||
|
||||
/**
|
||||
* 查询转换
|
||||
*
|
||||
* @param query 通用查询
|
||||
* @param entityInformation 实体信息
|
||||
* @param <T> 具体查询泛型
|
||||
* @return 转换结果
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T convert(Query query, EntityInformation<? extends Domain, String> entityInformation) {
|
||||
return QueryFactories.getFactory(entityInformation)
|
||||
.map(factory -> (T) factory.getConverter().convert(query))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建基于字符串字段名的查询
|
||||
*
|
||||
* @param column 列名
|
||||
* @return 结果
|
||||
*/
|
||||
public static QueryCondition<NamedQueryChain> where(String column) {
|
||||
NamedQueryChain chain = new DefaultNamedQueryChain();
|
||||
return chain.and(column);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建基于lambda字段引用的查询
|
||||
*
|
||||
* @param getter 列引用
|
||||
* @return 结果
|
||||
*/
|
||||
public static <T extends Domain> QueryCondition<LambdaQueryChain<T>> where(DomainFunction<T> getter) {
|
||||
LambdaQueryChain<T> chain = new DefaultLambdaQueryChain<>();
|
||||
return chain.and(getter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以嵌套条件开始
|
||||
*
|
||||
* @param chain 要嵌套的条件
|
||||
* @return 结果
|
||||
*/
|
||||
public static NamedQueryChain within(NamedQueryChain chain) {
|
||||
NamedQueryChain created = new DefaultNamedQueryChain();
|
||||
return created.and(chain);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以嵌套条件开始
|
||||
*
|
||||
* @param chain 要嵌套的条件
|
||||
* @return 结果
|
||||
*/
|
||||
public static <T extends Domain> LambdaQueryChain<T> within(LambdaQueryChain<T> chain) {
|
||||
LambdaQueryChain<T> created = new DefaultLambdaQueryChain<>();
|
||||
return created.and(chain);
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入多个查询列表时的连接方式
|
||||
*/
|
||||
public enum Combinator {
|
||||
|
||||
AND, OR
|
||||
}
|
||||
|
||||
/**
|
||||
* 方向,用于描述查询和部分语法
|
||||
*/
|
||||
public enum Direction {
|
||||
|
||||
LEFT, RIGHT, ALL
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package com.flyfish.framework.query;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import lombok.Data;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 抽象查询实体
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
@Data
|
||||
public class Query {
|
||||
|
||||
// 查询定义
|
||||
private QueryDefinition definition;
|
||||
// 查询用例
|
||||
private Example<?> example;
|
||||
// 排序
|
||||
private Sort sort;
|
||||
// 分页
|
||||
private Pageable pageable;
|
||||
// 字段列表
|
||||
private List<String> fields;
|
||||
|
||||
/**
|
||||
* 初始化一个查询
|
||||
*
|
||||
* @param definition 查询定义
|
||||
* @return 结果
|
||||
*/
|
||||
public static Query query(QueryDefinition definition) {
|
||||
Query query = new Query();
|
||||
query.setDefinition(definition);
|
||||
return query;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个空查询
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Query empty() {
|
||||
return new Query();
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定选择的字段
|
||||
*
|
||||
* @param columns 字段们
|
||||
* @return 当前自身
|
||||
*/
|
||||
public Query select(String... columns) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定选择的字段
|
||||
*
|
||||
* @param getters 字段们
|
||||
* @return 当前自身
|
||||
*/
|
||||
public <T extends Domain> Query select(Function<T, ?>... getters) {
|
||||
return this;
|
||||
}
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
package com.flyfish.framework.query;
|
||||
|
||||
import com.flyfish.framework.query.Queries.Combinator;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 查询链
|
||||
* 此处为高度抽象,便于上层调用
|
||||
* 下层重写方法实现
|
||||
*
|
||||
* @param <C> 本类型泛型
|
||||
* @param <P> 参数泛型
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface QueryChain<C extends QueryChain<C, P>, P> extends QueryDefinition {
|
||||
|
||||
/**
|
||||
* 以且连接下一个字段
|
||||
*
|
||||
* @param column 列
|
||||
* @return 查询条件
|
||||
*/
|
||||
QueryCondition<C> and(P column);
|
||||
|
||||
/**
|
||||
* 直接拼接提供者,此处懒加载,最终build才会执行
|
||||
*
|
||||
* @param supplier 提供者
|
||||
* @param <V> 泛型,支持其他类型的链
|
||||
* @return 结果
|
||||
*/
|
||||
<V extends QueryChain<V, ?>> C and(Supplier<QueryChain<V, ?>> supplier);
|
||||
|
||||
/**
|
||||
* 条件列表
|
||||
*
|
||||
* @param chain 多个条件们
|
||||
* @return 结果
|
||||
*/
|
||||
<V extends QueryChain<V, ?>> C and(QueryChain<V, ?> chain);
|
||||
|
||||
/**
|
||||
* 多个嵌套子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @return 结果
|
||||
*/
|
||||
default <V extends QueryChain<V, ?>> C and(List<QueryChain<V, ?>> chains) {
|
||||
return and(chains, Combinator.AND);
|
||||
}
|
||||
|
||||
/**
|
||||
* 多个嵌套子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @param combinator 各个链条之间的连接方式
|
||||
* @return 结果
|
||||
*/
|
||||
<V extends QueryChain<V, ?>> C and(List<QueryChain<V, ?>> chains, Combinator combinator);
|
||||
|
||||
/**
|
||||
* 以或连接下一个字段
|
||||
*
|
||||
* @param column 列
|
||||
* @return 查询条件
|
||||
*/
|
||||
QueryCondition<C> or(P column);
|
||||
|
||||
/**
|
||||
* 以或连接条件列表
|
||||
*
|
||||
* @param chain 多个条件们
|
||||
* @return 结果
|
||||
*/
|
||||
<V extends QueryChain<V, ?>> C or(QueryChain<V, ?> chain);
|
||||
|
||||
/**
|
||||
* 以或连接嵌套多个子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @return 结果
|
||||
*/
|
||||
default <V extends QueryChain<V, ?>> C or(List<QueryChain<V, ?>> chains) {
|
||||
return and(chains, Combinator.AND);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以或连接嵌套多个子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @param combinator 各个链条之间的连接方式
|
||||
* @return 结果
|
||||
*/
|
||||
<V extends QueryChain<V, ?>> C or(List<QueryChain<V, ?>> chains, Combinator combinator);
|
||||
|
||||
/**
|
||||
* 直接拼接提供者,此处懒加载,最终build才会执行
|
||||
*
|
||||
* @param supplier 提供者
|
||||
* @param <V> 泛型,支持其他类型的链
|
||||
* @return 结果
|
||||
*/
|
||||
<V extends QueryChain<V, ?>> C or(Supplier<QueryChain<V, ?>> supplier);
|
||||
}
|
@ -0,0 +1,152 @@
|
||||
package com.flyfish.framework.query;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 查询条件
|
||||
*
|
||||
* @param <C> 条件本类型
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface QueryCondition<C extends QueryChain<C, ?>> {
|
||||
|
||||
/**
|
||||
* 相等判定
|
||||
*
|
||||
* @param value 值
|
||||
* @return 结果
|
||||
*/
|
||||
C eq(Object value);
|
||||
|
||||
/**
|
||||
* 判定某列的值中存在指定值,特指json array数据类型且子类型中带有id的场景
|
||||
* 用于兼容mongodb查询,mysql查询使用JSON_CONTAINS进行判定。
|
||||
* 当且仅当mongodb会拼接.$id,mysql一律匹配id字段
|
||||
* <p>
|
||||
* 等价于 .eq(value)
|
||||
*
|
||||
* @param value 值
|
||||
* @return 结果
|
||||
*/
|
||||
C hasId(Object value);
|
||||
|
||||
/**
|
||||
* 对于mongodb,自动处理。对于关系型数据库,代表json array中是否包含对应值,值仅支持基本数据类型
|
||||
* <p>
|
||||
* 等价于 .eq(value)
|
||||
*
|
||||
* @param value 基本数据类型的值
|
||||
* @return 结果
|
||||
*/
|
||||
C has(Object value);
|
||||
|
||||
/**
|
||||
* 不等判定
|
||||
*
|
||||
* @param value 值
|
||||
* @return 结果
|
||||
*/
|
||||
C ne(Object value);
|
||||
|
||||
/**
|
||||
* 值介于两者之间
|
||||
*
|
||||
* @param items 双值列表
|
||||
* @return 结果
|
||||
*/
|
||||
C between(List<?> items);
|
||||
|
||||
/**
|
||||
* 模糊匹配,这里是全模糊
|
||||
*
|
||||
* @param keyword 查询关键字
|
||||
* @return 结果
|
||||
*/
|
||||
C like(String keyword);
|
||||
|
||||
/**
|
||||
* 根据指定的方向进行模糊查询
|
||||
*
|
||||
* @param keyword 关键字
|
||||
* @param direction 方向,可以匹配开头和结尾
|
||||
* @return 结果
|
||||
*/
|
||||
C like(String keyword, Queries.Direction direction);
|
||||
|
||||
/**
|
||||
* 判定为空
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
C isNull();
|
||||
|
||||
/**
|
||||
* 包含在内
|
||||
*
|
||||
* @param values 集合
|
||||
* @return 结果
|
||||
*/
|
||||
C in(Collection<?> values);
|
||||
|
||||
/**
|
||||
* 包含在内
|
||||
* 注意,需要根据字段类型推断。
|
||||
* 如果是json数组,需要进行双向匹配
|
||||
*
|
||||
* @param values 值们
|
||||
* @return 结果
|
||||
*/
|
||||
C in(Object... values);
|
||||
|
||||
/**
|
||||
* 不包含在内
|
||||
* 注意,需要根据字段类型推断。
|
||||
* 如果是json数组,需要进行双向匹配
|
||||
*
|
||||
* @param values 值们
|
||||
* @return 结果
|
||||
*/
|
||||
C nin(Object... values);
|
||||
|
||||
/**
|
||||
* 不包含在内
|
||||
*
|
||||
* @param values 集合
|
||||
* @return 结果
|
||||
*/
|
||||
C nin(Collection<?> values);
|
||||
|
||||
/**
|
||||
* 小于指定的值
|
||||
*
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
C lt(Object value);
|
||||
|
||||
/**
|
||||
* 小于等于指定的值
|
||||
*
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
C lte(Object value);
|
||||
|
||||
/**
|
||||
* 大于指定的值
|
||||
*
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
C gt(Object value);
|
||||
|
||||
/**
|
||||
* 大于等于指定的值
|
||||
*
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
C gte(Object value);
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.flyfish.framework.query;
|
||||
|
||||
import com.flyfish.framework.query.holder.QueryChainHolder;
|
||||
import com.flyfish.framework.query.spi.adaptor.CriteriaAdaptor;
|
||||
|
||||
/**
|
||||
* 可构建的
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface QueryDefinition {
|
||||
|
||||
/**
|
||||
* 构建结果
|
||||
*
|
||||
* @param adaptor 查询适配器
|
||||
* @param <T> 泛型
|
||||
* @return 构建结果
|
||||
*/
|
||||
<T> T build(CriteriaAdaptor<T> adaptor);
|
||||
|
||||
/**
|
||||
* 修改,此修改会直接接着查询条件进行拼接
|
||||
*
|
||||
* @return 修改句柄
|
||||
*/
|
||||
QueryMutation mutate();
|
||||
|
||||
/**
|
||||
* 会直接添加嵌套查询到当前查询末尾
|
||||
*
|
||||
* @return 修改句柄
|
||||
*/
|
||||
QueryMutation within();
|
||||
|
||||
/**
|
||||
* 判断查询条件是否为空
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
boolean isEmpty();
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.flyfish.framework.query;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.query.spi.QueryFactory;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 查询工厂持有者
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
class QueryFactories {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static final List<QueryFactory> FACTORIES =
|
||||
SpringFactoriesLoader.loadFactories(QueryFactory.class, null);
|
||||
|
||||
/**
|
||||
* 获取目标查询工厂
|
||||
*
|
||||
* @param entityInformation 实体信息
|
||||
* @return 结果
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static Optional<QueryFactory> getFactory(EntityInformation<? extends Domain, String> entityInformation) {
|
||||
return FACTORIES.stream().filter(factory -> factory.supports(entityInformation))
|
||||
.findFirst();
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package com.flyfish.framework.query;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.query.support.DomainFunction;
|
||||
|
||||
/**
|
||||
* 查询修改逻辑
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface QueryMutation {
|
||||
|
||||
/**
|
||||
* 以lambda的形式修改
|
||||
* 注意,如果之前用过lambda的形式,此处应该校验类型
|
||||
*
|
||||
* @param getter 方法引用
|
||||
* @param <T> 实体泛型
|
||||
* @return 结果
|
||||
*/
|
||||
<T extends Domain> QueryCondition<LambdaQueryChain<T>> and(DomainFunction<T> getter);
|
||||
|
||||
/**
|
||||
* 以column的形式修改
|
||||
*
|
||||
* @param column 列名
|
||||
* @return 结果
|
||||
*/
|
||||
QueryCondition<NamedQueryChain> and(String column);
|
||||
|
||||
/**
|
||||
* 直接以and拼接其他的所有条件
|
||||
*
|
||||
* @param chain 传入的查询
|
||||
* @return 结果
|
||||
*/
|
||||
<T extends Domain> LambdaQueryChain<T> and(LambdaQueryChain<T> chain);
|
||||
|
||||
/**
|
||||
* 直接以and拼接其他的所有条件
|
||||
*
|
||||
* @param chain 传入的查询
|
||||
* @return 结果
|
||||
*/
|
||||
NamedQueryChain and(NamedQueryChain chain);
|
||||
|
||||
/**
|
||||
* 以lambda的形式修改
|
||||
* 注意,如果之前用过lambda的形式,此处应该校验类型
|
||||
*
|
||||
* @param getter 方法引用
|
||||
* @param <T> 实体泛型
|
||||
* @return 结果
|
||||
*/
|
||||
<T extends Domain> QueryCondition<LambdaQueryChain<T>> or(DomainFunction<T> getter);
|
||||
|
||||
/**
|
||||
* 以column的形式修改
|
||||
*
|
||||
* @param column 列名
|
||||
* @return 结果
|
||||
*/
|
||||
QueryCondition<NamedQueryChain> or(String column);
|
||||
|
||||
/**
|
||||
* 直接以or拼接其他的所有条件
|
||||
*
|
||||
* @param chain 传入的查询
|
||||
* @return 结果
|
||||
*/
|
||||
<T extends Domain> LambdaQueryChain<T> or(LambdaQueryChain<T> chain);
|
||||
|
||||
/**
|
||||
* 直接以or拼接其他的所有条件
|
||||
*
|
||||
* @param chain 传入的查询
|
||||
* @return 结果
|
||||
*/
|
||||
NamedQueryChain or(NamedQueryChain chain);
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
# 查询构建Fluent Api适配器
|
||||
同时适配关系型数据库mysql和非关系型数据库mongodb
|
||||
|
||||
未来将会支持更多,采用覆盖的方式进行对象组合
|
@ -0,0 +1,42 @@
|
||||
package com.flyfish.framework.query.chain;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.query.LambdaQueryChain;
|
||||
import com.flyfish.framework.query.QueryCondition;
|
||||
import com.flyfish.framework.query.support.DomainFunction;
|
||||
|
||||
/**
|
||||
* 默认按lambda的查询链实现
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public class DefaultLambdaQueryChain<T extends Domain> extends DefaultQueryChain<LambdaQueryChain<T>, DomainFunction<T>> implements LambdaQueryChain<T> {
|
||||
|
||||
private QueryCondition<LambdaQueryChain<T>> createCondition(DomainFunction<T> column) {
|
||||
return createCondition(column.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 以且连接下一个字段
|
||||
*
|
||||
* @param column 列
|
||||
* @return 查询条件
|
||||
*/
|
||||
@Override
|
||||
public QueryCondition<LambdaQueryChain<T>> and(DomainFunction<T> column) {
|
||||
and();
|
||||
return createCondition(column);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以或连接下一个字段
|
||||
*
|
||||
* @param column 列
|
||||
* @return 查询条件
|
||||
*/
|
||||
@Override
|
||||
public QueryCondition<LambdaQueryChain<T>> or(DomainFunction<T> column) {
|
||||
or();
|
||||
return createCondition(column);
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.flyfish.framework.query.chain;
|
||||
|
||||
import com.flyfish.framework.query.NamedQueryChain;
|
||||
import com.flyfish.framework.query.QueryCondition;
|
||||
|
||||
/**
|
||||
* 默认按名称的查询链实现
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public class DefaultNamedQueryChain extends DefaultQueryChain<NamedQueryChain, String> implements NamedQueryChain {
|
||||
|
||||
/**
|
||||
* 以且连接下一个字段
|
||||
*
|
||||
* @param column 列
|
||||
* @return 查询条件
|
||||
*/
|
||||
@Override
|
||||
public QueryCondition<NamedQueryChain> and(String column) {
|
||||
and();
|
||||
return createCondition(column);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以或连接下一个字段
|
||||
*
|
||||
* @param column 列
|
||||
* @return 查询条件
|
||||
*/
|
||||
@Override
|
||||
public QueryCondition<NamedQueryChain> or(String column) {
|
||||
or();
|
||||
return createCondition(column);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,190 @@
|
||||
package com.flyfish.framework.query.chain;
|
||||
|
||||
import com.flyfish.framework.query.Queries;
|
||||
import com.flyfish.framework.query.QueryChain;
|
||||
import com.flyfish.framework.query.QueryCondition;
|
||||
import com.flyfish.framework.query.QueryMutation;
|
||||
import com.flyfish.framework.query.holder.QueryChainHolder;
|
||||
import com.flyfish.framework.query.spi.adaptor.CriteriaAdaptor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.data.util.CastUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 默认的查询定义,交给holder提供构建
|
||||
* 在执行时,我们并不知道adaptor是谁,只有最后构建阶段才能明确
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
@Getter
|
||||
abstract class DefaultQueryChain<C extends QueryChain<C, P>, P> implements QueryChain<C, P> {
|
||||
|
||||
protected final DefaultQueryChainHolder<?> holder = new DefaultQueryChainHolder<>();
|
||||
|
||||
/**
|
||||
* 以且的关系进行拼接
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
protected QueryChainHolder<?> and() {
|
||||
return holder.link(adaptor -> adaptor::and);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以或的关系进行拼接
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
protected QueryChainHolder<?> or() {
|
||||
return holder.link(adaptor -> adaptor::or);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建条件,使用默认实现的条件
|
||||
*
|
||||
* @param column 列名
|
||||
* @return 结果
|
||||
*/
|
||||
protected QueryCondition<C> createCondition(String column) {
|
||||
return new DefaultQueryCondition<>(self(), holder, column);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回自身真实类型
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
private C self() {
|
||||
return CastUtils.cast(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建结果
|
||||
*
|
||||
* @param adaptor 查询适配器
|
||||
* @return 构建结果
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T build(CriteriaAdaptor<T> adaptor) {
|
||||
QueryChainHolder<T> casted = (QueryChainHolder<T>) holder;
|
||||
return casted.get(adaptor);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改,此修改会直接接着查询条件进行拼接
|
||||
*
|
||||
* @return 修改句柄
|
||||
*/
|
||||
@Override
|
||||
public QueryMutation mutate() {
|
||||
return new DefaultQueryMutation(this, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会直接添加嵌套查询到当前查询末尾
|
||||
*
|
||||
* @return 修改句柄
|
||||
*/
|
||||
@Override
|
||||
public QueryMutation within() {
|
||||
return new DefaultQueryMutation(this, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断查询条件是否为空
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return holder.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接拼接提供者,此处懒加载,最终build才会执行
|
||||
*
|
||||
* @param supplier 提供者
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public <V extends QueryChain<V, ?>> C and(Supplier<QueryChain<V, ?>> supplier) {
|
||||
and().with(adaptor -> supplier.get().build(adaptor));
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 条件列表
|
||||
*
|
||||
* @param chain 多个条件们
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public <V extends QueryChain<V, ?>> C and(QueryChain<V, ?> chain) {
|
||||
and().with(chain::build);
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 多个嵌套子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @param combinator 各个链条之间的连接方式
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public <V extends QueryChain<V, ?>> C and(List<QueryChain<V, ?>> chains, Queries.Combinator combinator) {
|
||||
return and(combine(chains, combinator));
|
||||
}
|
||||
|
||||
/**
|
||||
* 以或连接条件列表
|
||||
*
|
||||
* @param chain 多个条件们
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public <V extends QueryChain<V, ?>> C or(QueryChain<V, ?> chain) {
|
||||
or().with(chain::build);
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 以或连接嵌套多个子条件列表
|
||||
*
|
||||
* @param chains 多条链
|
||||
* @param combinator 各个链条之间的连接方式
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public <V extends QueryChain<V, ?>> C or(List<QueryChain<V, ?>> chains, Queries.Combinator combinator) {
|
||||
return or(combine(chains, combinator));
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接拼接提供者,此处懒加载,最终build才会执行
|
||||
*
|
||||
* @param supplier 提供者
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public <V extends QueryChain<V, ?>> C or(Supplier<QueryChain<V, ?>> supplier) {
|
||||
or().with(adaptor -> supplier.get().build(adaptor));
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* 结合多个查询链,并根据组合方式进行处理
|
||||
*
|
||||
* @param chains 查询链集合
|
||||
* @param combinator 组合方式
|
||||
* @return 结果
|
||||
*/
|
||||
private <V extends QueryChain<V, ?>> C combine(List<QueryChain<V, ?>> chains, Queries.Combinator combinator) {
|
||||
return chains.stream().reduce((left, right) -> combinator == Queries.Combinator.OR ? left.or(right) : left.and(right))
|
||||
.map(this::and)
|
||||
.orElse(self());
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.flyfish.framework.query.chain;
|
||||
|
||||
import com.flyfish.framework.query.holder.QueryChainHolder;
|
||||
import com.flyfish.framework.query.spi.adaptor.CriteriaAdaptor;
|
||||
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 默认的查询保持器
|
||||
*
|
||||
* @param <C> 泛型,交给适配器处理
|
||||
*/
|
||||
class DefaultQueryChainHolder<C> implements QueryChainHolder<C> {
|
||||
|
||||
/**
|
||||
* 等待下一步消费
|
||||
* 如果不调用with,该状态将一直保持,可被其他link调用替换
|
||||
* 在调用with后,自动消费,并完成拼接
|
||||
*
|
||||
* @param operator 操作方法
|
||||
* @return 本身
|
||||
*/
|
||||
@Override
|
||||
public QueryChainHolder<C> link(Function<CriteriaAdaptor<C>, BinaryOperator<C>> operator) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接条件
|
||||
*
|
||||
* @param criteria 条件
|
||||
*/
|
||||
@Override
|
||||
public void with(Function<CriteriaAdaptor<C>, C> criteria) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 触发动作,得到最终的
|
||||
*
|
||||
* @return 本身
|
||||
*/
|
||||
@Override
|
||||
public C get(CriteriaAdaptor<C> adaptor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断条件是否为空
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,258 @@
|
||||
package com.flyfish.framework.query.chain;
|
||||
|
||||
import com.flyfish.framework.query.Queries;
|
||||
import com.flyfish.framework.query.QueryChain;
|
||||
import com.flyfish.framework.query.QueryCondition;
|
||||
import com.flyfish.framework.query.holder.QueryChainHolder;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 条件操作实现
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
class DefaultQueryCondition<C extends QueryChain<C, ?>> implements QueryCondition<C> {
|
||||
|
||||
private final C chain;
|
||||
private final QueryChainHolder<?> holder;
|
||||
private final String column;
|
||||
|
||||
/**
|
||||
* 相等判定
|
||||
*
|
||||
* @param value 值
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C eq(Object value) {
|
||||
if (null != value) {
|
||||
holder.with(adaptor -> adaptor.eq(column, value));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判定某列的值中存在指定值,特指json array数据类型且子类型中带有id的场景
|
||||
* 用于兼容mongodb查询,mysql查询使用JSON_CONTAINS进行判定。
|
||||
* 当且仅当mongodb会拼接.$id,mysql一律匹配id字段
|
||||
* <p>
|
||||
* 等价于 .eq(value)
|
||||
*
|
||||
* @param value 值
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C hasId(Object value) {
|
||||
if (null != value) {
|
||||
holder.with(adaptor -> adaptor.hasId(column, value));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对于mongodb,自动处理。对于关系型数据库,代表json array中是否包含对应值,值仅支持基本数据类型
|
||||
* <p>
|
||||
* 等价于 .eq(value)
|
||||
*
|
||||
* @param value 基本数据类型的值
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C has(Object value) {
|
||||
if (null != value) {
|
||||
holder.with(adaptor -> adaptor.has(column, value));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不等判定
|
||||
*
|
||||
* @param value 值
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C ne(Object value) {
|
||||
if (null != value) {
|
||||
holder.with(adaptor -> adaptor.ne(column, value));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 值介于两者之间
|
||||
*
|
||||
* @param items 双值列表
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C between(List<?> items) {
|
||||
if (CollectionUtils.size(items) == 2) {
|
||||
holder.with(adaptor -> adaptor.between(column, items));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊匹配,这里是全模糊
|
||||
*
|
||||
* @param keyword 查询关键字
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C like(String keyword) {
|
||||
if (StringUtils.isNotBlank(keyword)) {
|
||||
holder.with(adaptor -> adaptor.like(column, keyword, Queries.Direction.ALL));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据指定的方向进行模糊查询
|
||||
*
|
||||
* @param keyword 关键字
|
||||
* @param direction 方向,可以匹配开头和结尾
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C like(String keyword, Queries.Direction direction) {
|
||||
if (StringUtils.isNotBlank(keyword)) {
|
||||
holder.with(adaptor -> adaptor.like(column, keyword, direction));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判定为空
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C isNull() {
|
||||
holder.with(adaptor -> adaptor.isNull(column));
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 包含在内
|
||||
*
|
||||
* @param list 集合
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C in(Collection<?> list) {
|
||||
if (CollectionUtils.isNotEmpty(list)) {
|
||||
holder.with(adaptor -> adaptor.in(column, list));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 包含在内
|
||||
* 注意,需要根据字段类型推断。
|
||||
* 如果是json数组,需要进行双向匹配
|
||||
*
|
||||
* @param values 值们
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C in(Object... values) {
|
||||
if (ArrayUtils.isNotEmpty(values)) {
|
||||
holder.with(adaptor -> adaptor.in(column, values));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不包含在内
|
||||
* 注意,需要根据字段类型推断。
|
||||
* 如果是json数组,需要进行双向匹配
|
||||
*
|
||||
* @param values 值们
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C nin(Object... values) {
|
||||
if (ArrayUtils.isNotEmpty(values)) {
|
||||
holder.with(adaptor -> adaptor.nin(column, values));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不包含在内
|
||||
*
|
||||
* @param values 集合
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C nin(Collection<?> values) {
|
||||
if (CollectionUtils.isNotEmpty(values)) {
|
||||
holder.with(adaptor -> adaptor.nin(column, values));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于指定的值
|
||||
*
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C lt(Object value) {
|
||||
if (null != value) {
|
||||
holder.with(adaptor -> adaptor.lt(column, value));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于等于指定的值
|
||||
*
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C lte(Object value) {
|
||||
if (null != value) {
|
||||
holder.with(adaptor -> adaptor.lte(column, value));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于指定的值
|
||||
*
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C gt(Object value) {
|
||||
if (null != value) {
|
||||
holder.with(adaptor -> adaptor.gt(column, value));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于等于指定的值
|
||||
*
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public C gte(Object value) {
|
||||
if (null != value) {
|
||||
holder.with(adaptor -> adaptor.gte(column, value));
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
package com.flyfish.framework.query.chain;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.query.*;
|
||||
import com.flyfish.framework.query.support.DomainFunction;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 默认的查询修改逻辑
|
||||
*
|
||||
* @author wangyu
|
||||
* <p>
|
||||
* 不修改原条件,而是在此基础上新增
|
||||
* 理论上查询不允许直接修改,而是通过该类做一个桥接
|
||||
* 也可以修改链的形式
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class DefaultQueryMutation implements QueryMutation {
|
||||
|
||||
private final QueryChain<?, ?> previous;
|
||||
|
||||
private final boolean within;
|
||||
|
||||
/**
|
||||
* 以lambda的形式修改
|
||||
* 注意,如果之前用过lambda的形式,此处应该校验类型
|
||||
*
|
||||
* @param getter 方法引用
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public <T extends Domain> QueryCondition<LambdaQueryChain<T>> and(DomainFunction<T> getter) {
|
||||
LambdaQueryChain<T> newChain = new DefaultLambdaQueryChain<>();
|
||||
QueryCondition<LambdaQueryChain<T>> condition = newChain.and(getter);
|
||||
List<QueryChain<?, ?>> chains = Arrays.asList(previous, newChain);
|
||||
previous.and(chains);
|
||||
return condition;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以column的形式修改
|
||||
*
|
||||
* @param column 列名
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public QueryCondition<NamedQueryChain> and(String column) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接以and拼接其他的所有条件
|
||||
*
|
||||
* @param chain 传入的查询
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public <T extends Domain> LambdaQueryChain<T> and(LambdaQueryChain<T> chain) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接以and拼接其他的所有条件
|
||||
*
|
||||
* @param chain 传入的查询
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public NamedQueryChain and(NamedQueryChain chain) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以lambda的形式修改
|
||||
* 注意,如果之前用过lambda的形式,此处应该校验类型
|
||||
*
|
||||
* @param getter 方法引用
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public <T extends Domain> QueryCondition<LambdaQueryChain<T>> or(DomainFunction<T> getter) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以column的形式修改
|
||||
*
|
||||
* @param column 列名
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public QueryCondition<NamedQueryChain> or(String column) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接以or拼接其他的所有条件
|
||||
*
|
||||
* @param chain 传入的查询
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public <T extends Domain> LambdaQueryChain<T> or(LambdaQueryChain<T> chain) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接以or拼接其他的所有条件
|
||||
*
|
||||
* @param chain 传入的查询
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public NamedQueryChain or(NamedQueryChain chain) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.flyfish.framework.query.holder;
|
||||
|
||||
import com.flyfish.framework.query.spi.adaptor.CriteriaAdaptor;
|
||||
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 查询链保持器,将所有拼接工作后置,可以做检查和处理
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface QueryChainHolder<C> {
|
||||
|
||||
/**
|
||||
* 等待下一步消费
|
||||
* 如果不调用with,该状态将一直保持,可被其他link调用替换
|
||||
* 在调用with后,自动消费,并完成拼接
|
||||
*
|
||||
* @param operator 操作方法
|
||||
* @return 本身
|
||||
*/
|
||||
QueryChainHolder<C> link(Function<CriteriaAdaptor<C>, BinaryOperator<C>> operator);
|
||||
|
||||
/**
|
||||
* 拼接条件
|
||||
*
|
||||
* @param criteria 条件
|
||||
*/
|
||||
void with(Function<CriteriaAdaptor<C>, C> criteria);
|
||||
|
||||
/**
|
||||
* 触发动作,得到最终的
|
||||
*
|
||||
* @return 本身
|
||||
*/
|
||||
C get(CriteriaAdaptor<C> adaptor);
|
||||
|
||||
/**
|
||||
* 判断条件是否为空
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
boolean isEmpty();
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.flyfish.framework.query.spi;
|
||||
|
||||
import com.flyfish.framework.query.QueryChain;
|
||||
import com.flyfish.framework.utils.Supportable;
|
||||
|
||||
/**
|
||||
* 查询链创建抽象泛型工厂
|
||||
* 基于spi提供实现类,最终返回真正实例化的内容
|
||||
* 根据引入的jar包自动加载,无需手动处理
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface QueryChainFactory<C extends QueryChain<C, ?>> extends Supportable<Class<?>> {
|
||||
|
||||
/**
|
||||
* 生产适配器
|
||||
* 此接口为高度抽象接口,目的是兼容更多的查询链类型,便于后续扩展
|
||||
*
|
||||
* @return 生产结果
|
||||
*/
|
||||
C produce();
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.flyfish.framework.query.spi;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.query.spi.adaptor.CriteriaAdaptor;
|
||||
import com.flyfish.framework.query.spi.converter.QueryConverter;
|
||||
import com.flyfish.framework.utils.Supportable;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
|
||||
/**
|
||||
* 查询工厂,提供统一的实例
|
||||
*
|
||||
* @param <C> 查询条件对象泛型
|
||||
* @param <Q> 查询对象泛型
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface QueryFactory<C, Q> extends Supportable<EntityInformation<? extends Domain, String>> {
|
||||
|
||||
/**
|
||||
* 获取查询转换器
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
QueryConverter<Q> getConverter();
|
||||
|
||||
/**
|
||||
* 获取查询适配器
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
CriteriaAdaptor<C> getAdaptor();
|
||||
}
|
@ -0,0 +1,180 @@
|
||||
package com.flyfish.framework.query.spi.adaptor;
|
||||
|
||||
import com.flyfish.framework.query.Queries;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 查询适配器
|
||||
* 直接根据条件操作得到查询条件片段
|
||||
* 适配不同的数据库抽取通用操作
|
||||
*
|
||||
* @author wangyu
|
||||
* 由工厂进行实例化,并输出
|
||||
*/
|
||||
public interface CriteriaAdaptor<C> {
|
||||
|
||||
/**
|
||||
* 且的关系进行拼接
|
||||
*
|
||||
* @param first 起始条件
|
||||
* @param other 其他条件
|
||||
* @return 结果
|
||||
*/
|
||||
C and(C first, C other);
|
||||
|
||||
/**
|
||||
* 或的关系进行拼接
|
||||
*
|
||||
* @param first 起始条件
|
||||
* @param other 其他条件
|
||||
* @return 结果
|
||||
*/
|
||||
C or(C first, C other);
|
||||
|
||||
/**
|
||||
* 相等判定
|
||||
*
|
||||
* @param column 列名
|
||||
* @param value 值
|
||||
* @return 结果
|
||||
*/
|
||||
C eq(String column, Object value);
|
||||
|
||||
/**
|
||||
* 判定某列的值中存在指定值,特指json array数据类型且子类型中带有id的场景
|
||||
* 用于兼容mongodb查询,mysql查询使用JSON_CONTAINS进行判定。
|
||||
* 当且仅当mongodb会拼接.$id,mysql一律匹配id字段
|
||||
* <p>
|
||||
* 等价于 .eq(value)
|
||||
*
|
||||
* @param column 列名
|
||||
* @param value 值
|
||||
* @return 结果
|
||||
*/
|
||||
C hasId(String column, Object value);
|
||||
|
||||
/**
|
||||
* 对于mongodb,自动处理。对于关系型数据库,代表json array中是否包含对应值,值仅支持基本数据类型
|
||||
* <p>
|
||||
* 等价于 .eq(value)
|
||||
*
|
||||
* @param column 列名
|
||||
* @param value 基本数据类型的值
|
||||
* @return 结果
|
||||
*/
|
||||
C has(String column, Object value);
|
||||
|
||||
/**
|
||||
* 不等判定
|
||||
*
|
||||
* @param column 列名
|
||||
* @param value 值
|
||||
* @return 结果
|
||||
*/
|
||||
C ne(String column, Object value);
|
||||
|
||||
/**
|
||||
* 值介于两者之间
|
||||
*
|
||||
* @param column 列名
|
||||
* @param items 双值列表
|
||||
* @return 结果
|
||||
*/
|
||||
C between(String column, List<?> items);
|
||||
|
||||
/**
|
||||
* 根据指定的方向进行模糊查询
|
||||
*
|
||||
* @param column 列名
|
||||
* @param keyword 关键字
|
||||
* @param direction 方向,可以匹配开头和结尾
|
||||
* @return 结果
|
||||
*/
|
||||
C like(String column, String keyword, Queries.Direction direction);
|
||||
|
||||
/**
|
||||
* 判定为空
|
||||
*
|
||||
* @param column 列名
|
||||
* @return 结果
|
||||
*/
|
||||
C isNull(String column);
|
||||
|
||||
/**
|
||||
* 小于指定的值
|
||||
*
|
||||
* @param column 列名
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
C lt(String column, Object value);
|
||||
|
||||
/**
|
||||
* 小于等于指定的值
|
||||
*
|
||||
* @param column 列名
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
C lte(String column, Object value);
|
||||
|
||||
/**
|
||||
* 大于指定的值
|
||||
*
|
||||
* @param column 列名
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
C gt(String column, Object value);
|
||||
|
||||
/**
|
||||
* 大于等于指定的值
|
||||
*
|
||||
* @param column 列名
|
||||
* @param value 类型
|
||||
* @return 结果
|
||||
*/
|
||||
C gte(String column, Object value);
|
||||
|
||||
/**
|
||||
* 包含在内
|
||||
*
|
||||
* @param column 列名
|
||||
* @param values 集合
|
||||
* @return 结果
|
||||
*/
|
||||
C in(String column, Collection<?> values);
|
||||
|
||||
/**
|
||||
* 包含在内
|
||||
* 注意,需要根据字段类型推断。
|
||||
* 如果是json数组,需要进行双向匹配
|
||||
*
|
||||
* @param column 列名
|
||||
* @param values 值们
|
||||
* @return 结果
|
||||
*/
|
||||
C in(String column, Object... values);
|
||||
|
||||
/**
|
||||
* 不包含在内
|
||||
* 注意,需要根据字段类型推断。
|
||||
* 如果是json数组,需要进行双向匹配
|
||||
*
|
||||
* @param column 列名
|
||||
* @param values 值们
|
||||
* @return 结果
|
||||
*/
|
||||
C nin(String column, Object... values);
|
||||
|
||||
/**
|
||||
* 不包含在内
|
||||
*
|
||||
* @param column 列名
|
||||
* @param values 集合
|
||||
* @return 结果
|
||||
*/
|
||||
C nin(String column, Collection<?> values);
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.flyfish.framework.query.spi.converter;
|
||||
|
||||
import com.flyfish.framework.query.Query;
|
||||
|
||||
/**
|
||||
* 原生查询转换器
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface QueryConverter<T> {
|
||||
|
||||
/**
|
||||
* 转换查询
|
||||
*
|
||||
* @param query 封装的查询
|
||||
* @return 本地查询
|
||||
*/
|
||||
T convert(Query query);
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.flyfish.framework.query.support;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
|
||||
/**
|
||||
* 支持序列化的实体方法
|
||||
*
|
||||
* @param <T> 泛型,父类为实体
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface DomainFunction<T extends Domain> extends SFunction<T, Object> {
|
||||
|
||||
/**
|
||||
* 快速获取名称
|
||||
*
|
||||
* @return 序列化后的名称
|
||||
*/
|
||||
default String getName() {
|
||||
return EntityNameUtils.getName(this);
|
||||
}
|
||||
}
|
@ -0,0 +1,170 @@
|
||||
package com.flyfish.framework.query.support;
|
||||
|
||||
|
||||
import com.flyfish.framework.annotations.Property;
|
||||
import com.flyfish.framework.utils.LRUCache;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.Field;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class EntityNameUtils {
|
||||
|
||||
// SerializedLambda 反序列化缓存
|
||||
private static final Map<String, WeakReference<SerializedLambda>> FUNC_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
// 列别名缓存
|
||||
private static final Map<Class<?>, Map<String, String>> COLUMN_CACHE = new LRUCache<>(5);
|
||||
|
||||
private static final String MONGO_FIELD = "org.springframework.data.mongodb.core.mapping.Field";
|
||||
|
||||
private static final String RELATIONAL_COLUMN = "org.springframework.data.relational.core.mapping.Column";
|
||||
|
||||
/**
|
||||
* 方法转为属性
|
||||
*
|
||||
* @param name 方法名称
|
||||
* @return 最终属性
|
||||
*/
|
||||
public static String methodToProperty(String name) {
|
||||
if (name.startsWith("is")) {
|
||||
name = name.substring(2);
|
||||
} else {
|
||||
if (!name.startsWith("get") && !name.startsWith("set")) {
|
||||
throw new InvalidParameterException("不是正确的getter或setter");
|
||||
}
|
||||
name = name.substring(3);
|
||||
}
|
||||
if (name.length() == 1 || name.length() > 1 && !Character.isUpperCase(name.charAt(1))) {
|
||||
name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将驼峰命名转化成下划线
|
||||
*
|
||||
* @param param 参数
|
||||
* @return 结果
|
||||
*/
|
||||
public static String camelToUnderline(String param) {
|
||||
if (param.length() < 3) {
|
||||
return param.toLowerCase();
|
||||
}
|
||||
StringBuilder sb = new StringBuilder(param);
|
||||
int temp = 0;//定位
|
||||
//从第三个字符开始 避免命名不规范
|
||||
for (int i = 2; i < param.length(); i++) {
|
||||
if (Character.isUpperCase(param.charAt(i))) {
|
||||
sb.insert(i + temp, "_");
|
||||
temp += 1;
|
||||
}
|
||||
}
|
||||
return sb.toString().toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析列并使用处理函数处理
|
||||
*
|
||||
* @param func 方法引用
|
||||
* @param <T> 实体泛型
|
||||
* @return 处理结果
|
||||
*/
|
||||
public static <T> String getName(SFunction<T, ?> func) {
|
||||
SerializedLambda lambda = resolve(func);
|
||||
String property = methodToProperty(lambda.getImplMethodName());
|
||||
Class<?> beanClass = resolveEntityClass(lambda);
|
||||
return tryCache(beanClass).getOrDefault(property, property);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析获得实体类
|
||||
*
|
||||
* @param lambda 序列化的lambda
|
||||
* @return 最终获取的类
|
||||
*/
|
||||
private static Class<?> resolveEntityClass(SerializedLambda lambda) {
|
||||
Class<?> type = lambda.getInstantiatedType();
|
||||
tryCache(type);
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试缓存
|
||||
*
|
||||
* @param entityClass bean的类型
|
||||
*/
|
||||
private static Map<String, String> tryCache(Class<?> entityClass) {
|
||||
return COLUMN_CACHE.computeIfAbsent(entityClass, EntityNameUtils::buildFieldsCache);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建字段缓存
|
||||
*
|
||||
* @param type 类型
|
||||
* @return 构建后的缓存
|
||||
*/
|
||||
private static Map<String, String> buildFieldsCache(Class<?> type) {
|
||||
Map<String, String> fields = new HashMap<>();
|
||||
ReflectionUtils.doWithFields(type, field -> fields.put(field.getName(), resolveFinalName(field)));
|
||||
return fields;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解析字段注解或直接取用下划线逻辑
|
||||
*
|
||||
* @return 解析结果
|
||||
*/
|
||||
private static String resolveFinalName(Field field) {
|
||||
MergedAnnotations annotations = MergedAnnotations.from(field);
|
||||
if (annotations.isPresent(Property.class)) {
|
||||
String key = annotations.get(Property.class).getString("key");
|
||||
if (StringUtils.hasText(key)) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
if (annotations.isPresent(MONGO_FIELD)) {
|
||||
String name = annotations.get(MONGO_FIELD).getString("name");
|
||||
if (StringUtils.hasText(name)) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
if (annotations.isPresent(RELATIONAL_COLUMN)) {
|
||||
String name = annotations.get(RELATIONAL_COLUMN).getString("value");
|
||||
if (StringUtils.hasText(name)) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
return field.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析方法引用为序列化lambda实例
|
||||
* 该方式会使用缓存
|
||||
*
|
||||
* @param func 方法引用
|
||||
* @param <T> 泛型
|
||||
* @return 解析结果
|
||||
*/
|
||||
private static <T> SerializedLambda resolve(SFunction<T, ?> func) {
|
||||
Class<?> clazz = func.getClass();
|
||||
String name = clazz.getName();
|
||||
return Optional.ofNullable(FUNC_CACHE.get(name))
|
||||
.map(WeakReference::get)
|
||||
.orElseGet(() -> {
|
||||
SerializedLambda lambda = SerializedLambda.resolve(func);
|
||||
FUNC_CACHE.put(name, new WeakReference<>(lambda));
|
||||
return lambda;
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.flyfish.framework.query.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 支持序列化的function
|
||||
*
|
||||
* @param <T> 泛型入参
|
||||
* @param <R> 泛型返回值
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface SFunction<T, R> extends Function<T, R>, Serializable {
|
||||
}
|
@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2021, baomidou (jobob@qq.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.flyfish.framework.query.support;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.SerializationUtils;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* 这个类是从 {@link java.lang.invoke.SerializedLambda} 里面 copy 过来的,
|
||||
* 字段信息完全一样
|
||||
* <p>负责将一个支持序列的 Function 序列化为 SerializedLambda</p>
|
||||
*
|
||||
* @author mbp
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
class SerializedLambda implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 8025925345765570181L;
|
||||
|
||||
private Class<?> capturingClass;
|
||||
private String functionalInterfaceClass;
|
||||
private String functionalInterfaceMethodName;
|
||||
private String functionalInterfaceMethodSignature;
|
||||
private String implClass;
|
||||
/**
|
||||
* -- GETTER --
|
||||
* 获取实现者的方法名称
|
||||
*
|
||||
* @return 方法名称
|
||||
*/
|
||||
@Getter
|
||||
private String implMethodName;
|
||||
private String implMethodSignature;
|
||||
private int implMethodKind;
|
||||
private String instantiatedMethodType;
|
||||
private Object[] capturedArgs;
|
||||
|
||||
/**
|
||||
* 通过反序列化转换 lambda 表达式,该方法只能序列化 lambda 表达式,不能序列化接口实现或者正常非 lambda 写法的对象
|
||||
*
|
||||
* @param lambda lambda对象
|
||||
* @return 返回解析后的 SerializedLambda
|
||||
*/
|
||||
public static SerializedLambda resolve(SFunction<?, ?> lambda) {
|
||||
Assert.isTrue(lambda.getClass().isSynthetic(), "该方法仅能传入 lambda 表达式产生的合成类");
|
||||
byte[] stream = SerializationUtils.serialize(lambda);
|
||||
Assert.notNull(stream, "序列化类失败!");
|
||||
try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(stream)) {
|
||||
@Override
|
||||
protected Class<?> resolveClass(ObjectStreamClass objectStreamClass) throws IOException, ClassNotFoundException {
|
||||
Class<?> clazz;
|
||||
try {
|
||||
clazz = forName(objectStreamClass.getName());
|
||||
} catch (Exception ex) {
|
||||
clazz = super.resolveClass(objectStreamClass);
|
||||
}
|
||||
return clazz == java.lang.invoke.SerializedLambda.class ? SerializedLambda.class : clazz;
|
||||
}
|
||||
}) {
|
||||
return (SerializedLambda) objIn.readObject();
|
||||
} catch (ClassNotFoundException | IOException e) {
|
||||
throw new NullPointerException("不应该发生这件事");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 以类名实例化类,会隐藏报错,需要在确定有该类的情况下调用
|
||||
*
|
||||
* @param name 类型
|
||||
* @return 实例
|
||||
*/
|
||||
private static Class<?> forName(String name) {
|
||||
try {
|
||||
return ClassUtils.forName(name, null);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取接口 class
|
||||
*
|
||||
* @return 返回 class 名称
|
||||
*/
|
||||
public String getFunctionalInterfaceClassName() {
|
||||
return normalizedName(functionalInterfaceClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实现的 class
|
||||
*
|
||||
* @return 实现类
|
||||
*/
|
||||
public Class<?> getImplClass() {
|
||||
return forName(getImplClassName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 class 的名称
|
||||
*
|
||||
* @return 类名
|
||||
*/
|
||||
public String getImplClassName() {
|
||||
return normalizedName(implClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 正常化类名称,将类名称中的 / 替换为 .
|
||||
*
|
||||
* @param name 名称
|
||||
* @return 正常的类名
|
||||
*/
|
||||
private String normalizedName(String name) {
|
||||
return name.replace('/', '.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 获取实例化方法的类型
|
||||
*/
|
||||
public Class<?> getInstantiatedType() {
|
||||
String instantiatedTypeName = normalizedName(instantiatedMethodType.substring(2, instantiatedMethodType.indexOf(';')));
|
||||
return forName(instantiatedTypeName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 字符串形式
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
String interfaceName = getFunctionalInterfaceClassName();
|
||||
String implName = getImplClassName();
|
||||
return String.format("%s -> %s::%s",
|
||||
interfaceName.substring(interfaceName.lastIndexOf('.') + 1),
|
||||
implName.substring(implName.lastIndexOf('.') + 1),
|
||||
implMethodName);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.flyfish.framework.repository;
|
||||
|
||||
import com.flyfish.framework.repository.base.DomainRepository;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
|
||||
import org.springframework.lang.NonNull;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
|
||||
/**
|
||||
* 默认的持久层dao
|
||||
*
|
||||
* @param <T> 泛型
|
||||
*/
|
||||
@NoRepositoryBean
|
||||
public interface DefaultReactiveRepository<T> extends ReactiveCrudRepository<T, String>, ReactiveQueryModelExecutor<T>, DomainRepository<T> {
|
||||
|
||||
/**
|
||||
* 插入给定的实体. 假设该实体是新的,以便应用插入优化。
|
||||
* 使用返回的实例进行后续操作,因为保存操作可能会完全改变实体实例。
|
||||
* 建议使用 {@link #save(Object)} 来避免使用特定存储的 API。
|
||||
*
|
||||
* @param entity 要插入的实体,不能为空
|
||||
* @return 保存后的实体
|
||||
*/
|
||||
@NonNull
|
||||
<S extends T> Mono<S> insert(@NonNull S entity);
|
||||
|
||||
/**
|
||||
* 插入给定的实体列表. 假设这些实体是新的,以便应用插入优化。
|
||||
* 使用返回的实例进行后续操作,因为保存操作可能会完全改变实体实例。
|
||||
* 建议使用 {@link #save(Object)} 来避免使用特定存储的 API。
|
||||
*
|
||||
* @param entities 实体列表,不能为空
|
||||
* @return 保存后的实体列表
|
||||
*/
|
||||
<S extends T> Flux<S> insert(Iterable<S> entities);
|
||||
|
||||
/**
|
||||
* 插入给定的实体发布者发布的实体. 假设这些实体是新的,以便应用插入优化。
|
||||
* 使用返回的实例进行后续操作,因为保存操作可能会完全改变实体实例。
|
||||
* 建议使用 {@link #save(Object)} 来避免使用特定存储的 API。
|
||||
*
|
||||
* @param entities 发布的实体列表,不能为空
|
||||
* @return 保存后的实体列表
|
||||
*/
|
||||
<S extends T> Flux<S> insert(Publisher<S> entities);
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.flyfish.framework.repository;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.repository.base.DomainRepository;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 默认的仓库
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
@NoRepositoryBean
|
||||
public interface DefaultRepository<T extends Domain> extends CrudRepository<T, String>, PagingAndSortingRepository<T, String>,
|
||||
QueryModelExecutor<T>,
|
||||
DomainRepository<T> {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#saveAll(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
<S extends T> List<S> saveAll(Iterable<S> entities);
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findAll()
|
||||
*/
|
||||
@Override
|
||||
List<T> findAll();
|
||||
|
||||
/**
|
||||
* 插入新对象
|
||||
*
|
||||
* @param entity 实体
|
||||
* @param <S> 泛型
|
||||
* @return 插入的实体,带id
|
||||
*/
|
||||
<S extends T> S insert(S entity);
|
||||
|
||||
/**
|
||||
* 批量插入对象
|
||||
*
|
||||
* @param entities 对象列表
|
||||
* @param <S> 泛型
|
||||
* @return 结果列表
|
||||
*/
|
||||
<S extends T> List<S> insert(Iterable<S> entities);
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.flyfish.framework.repository;
|
||||
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.query.Queries;
|
||||
import com.flyfish.framework.query.Query;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* 高层封装的模型操作
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface ReactiveEntityOperations {
|
||||
|
||||
/**
|
||||
* 查询,通过查询条件和类进行精确查询
|
||||
*
|
||||
* @param query 查询
|
||||
* @param entityInformation 实体信息
|
||||
* @param <T> 实体类的实例化结果
|
||||
* @param <E> 实体信息具体实现类
|
||||
* @return 查询结果
|
||||
*/
|
||||
<T extends Domain, E extends EntityInformation<T, String>> Mono<T> find(Query query, E entityInformation);
|
||||
|
||||
/**
|
||||
* 查询全部符合条件的实体
|
||||
*
|
||||
* @param query 查询
|
||||
* @param entityInformation 实体信息
|
||||
* @param <T> 实体类的实例化结果
|
||||
* @param <E> 实体信息具体实现类
|
||||
* @return 结果
|
||||
*/
|
||||
<T extends Domain, E extends EntityInformation<T, String>> Flux<T> findAll(Query query, E entityInformation);
|
||||
|
||||
/**
|
||||
* 保存实体
|
||||
*
|
||||
* @param bean 实体
|
||||
* @param <T> 实体类的实例化结果
|
||||
* @return 结果
|
||||
*/
|
||||
<T extends Domain> Mono<T> save(T bean);
|
||||
|
||||
/**
|
||||
* 转换为原生查询
|
||||
*
|
||||
* @param query 查询
|
||||
* @param entityInformation 实体信息
|
||||
* @param <R> 泛型,原生查询类型
|
||||
* @param <T> 泛型,实体类型
|
||||
* @return 结果
|
||||
*/
|
||||
default <R, T extends Domain> R toNative(Query query, EntityInformation<T, String> entityInformation) {
|
||||
return Queries.convert(query, entityInformation);
|
||||
}
|
||||
}
|
@ -31,8 +31,7 @@ public interface ReactiveQueryModelExecutor<T> {
|
||||
*
|
||||
* @param query must not be {@literal null}.
|
||||
* @return a single entity matching the given {@link Qo} or {@link Optional#empty()} if none was found.
|
||||
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if the Qo yields more than one
|
||||
* result.
|
||||
* result.
|
||||
*/
|
||||
Mono<T> findOne(Qo<T> query);
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.flyfish.framework.repository.base;
|
||||
|
||||
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
|
||||
/**
|
||||
* 标记实体的仓库
|
||||
@ -14,5 +14,5 @@ public interface DomainRepository<T> {
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
MongoEntityInformation<T, String> getEntityInformation();
|
||||
EntityInformation<T, String> getEntityInformation();
|
||||
}
|
46
flyfish-data/flyfish-data-domain/pom.xml
Normal file
46
flyfish-data/flyfish-data-domain/pom.xml
Normal file
@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.flyfish.framework</groupId>
|
||||
<artifactId>flyfish-data</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>flyfish-data-domain</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.flyfish.framework</groupId>
|
||||
<artifactId>flyfish-common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-relational</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-mongodb</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,100 @@
|
||||
package com.flyfish.framework.annotations.query;
|
||||
|
||||
import com.flyfish.framework.enums.NamedEnum;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 查询字段注解,声明映射,查询逻辑
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface QueryField {
|
||||
|
||||
/**
|
||||
* @return 绑定的持久层字段
|
||||
*/
|
||||
@AliasFor("name")
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* @return 绑定的持久层字段
|
||||
*/
|
||||
String name() default "";
|
||||
|
||||
/**
|
||||
* @return 表达式类型
|
||||
*/
|
||||
Type type() default Type.EQ;
|
||||
|
||||
/**
|
||||
* 拼接在某个字段前
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String before() default "";
|
||||
|
||||
/**
|
||||
* @return 和前面条件表达式的连接方式
|
||||
*/
|
||||
Logistic[] start() default Logistic.AND;
|
||||
|
||||
/**
|
||||
* @return 和后面条件表达式的连接方式
|
||||
*/
|
||||
Logistic[] end() default Logistic.NULL;
|
||||
|
||||
/**
|
||||
* @return 当前类内排序值
|
||||
*/
|
||||
int order() default 0;
|
||||
|
||||
/**
|
||||
* 查询类型
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
enum Type implements NamedEnum {
|
||||
|
||||
EQ("等于字段值"),
|
||||
NE("不等于字段值"),
|
||||
GT("大于字段值"),
|
||||
GTE("大于等于字段值"),
|
||||
LT("小于字段值"),
|
||||
LTE("小于等于字段值"),
|
||||
LIKE("模糊匹配字段值"),
|
||||
LIKE_LEFT("匹配左半部分字段值"),
|
||||
LIKE_RIGHT("匹配有半部分字段值"),
|
||||
IN("在字段值列表内"),
|
||||
NIN("不再字段值列表内"),
|
||||
NOT_NULL("不为空"),
|
||||
IS_NULL("为空"),
|
||||
BETWEEN("介于列表下标0和1的值之间"),
|
||||
DATE_GTE("日期大于字段值"),
|
||||
DATE_LTE("日期小于字段值");
|
||||
|
||||
private final String name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询逻辑
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
enum Logistic implements NamedEnum {
|
||||
|
||||
NULL("无"),
|
||||
AND("与"),
|
||||
OR("或"),
|
||||
BRACKET_START("左括号"),
|
||||
BRACKET_END("右括号");
|
||||
|
||||
private final String name;
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user