0417最终版
This commit is contained in:
parent
d5c7ab0f53
commit
56251b7ca7
|
@ -15,7 +15,6 @@ public class JoinBuilder<T> {
|
||||||
private final Class<T> rootClass;
|
private final Class<T> rootClass;
|
||||||
private final Map<Class<?>, JoinInfo> joins = new HashMap<>();
|
private final Map<Class<?>, JoinInfo> joins = new HashMap<>();
|
||||||
private final Map<Class<?>, List<Condition>> conditions = new HashMap<>();
|
private final Map<Class<?>, List<Condition>> conditions = new HashMap<>();
|
||||||
private Map<Class<?>, String[]> selectedFields;
|
|
||||||
private Map<String, Object> namedSelections = new LinkedHashMap<>();
|
private Map<String, Object> namedSelections = new LinkedHashMap<>();
|
||||||
|
|
||||||
private JoinBuilder(Class<T> rootClass) {
|
private JoinBuilder(Class<T> rootClass) {
|
||||||
|
@ -28,51 +27,75 @@ public class JoinBuilder<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加连接表
|
* 添加连接表
|
||||||
* @param joinClass 要连接的表实体类
|
*
|
||||||
|
* @param joinClass 要连接的表实体类
|
||||||
* @param sourceField 主表的外键字段
|
* @param sourceField 主表的外键字段
|
||||||
* @param targetField 连接表的目标字段
|
* @param targetField 连接表的目标字段
|
||||||
*/
|
*/
|
||||||
public JoinBuilder<T> join(Class<?> joinClass, String sourceField, String targetField) {
|
public void join(Class<?> joinClass, String sourceField, String targetField) {
|
||||||
joins.put(joinClass, new JoinInfo(sourceField, targetField));
|
joins.put(joinClass, new JoinInfo(sourceField, targetField));
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加等值条件
|
* 添加等值条件
|
||||||
|
*
|
||||||
* @param entityClass 实体类
|
* @param entityClass 实体类
|
||||||
* @param fieldName 字段名
|
* @param fieldName 字段名
|
||||||
* @param value 字段值
|
* @param value 字段值
|
||||||
*/
|
*/
|
||||||
public <V> JoinBuilder<T> equal(Class<?> entityClass, String fieldName, V value) {
|
public <V> void equal(Class<?> entityClass, String fieldName, V value) {
|
||||||
if (!conditions.containsKey(entityClass)) {
|
if (!conditions.containsKey(entityClass)) {
|
||||||
conditions.put(entityClass, new ArrayList<>());
|
conditions.put(entityClass, new ArrayList<>());
|
||||||
}
|
}
|
||||||
conditions.get(entityClass).add(new Condition(fieldName, value, Operator.EQUAL));
|
conditions.get(entityClass).add(new Condition(fieldName, value, Operator.EQUAL));
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加一个具有别名的字段选择(字符串版本)
|
* 添加一个具有别名的字段选择(字符串版本)
|
||||||
* @param alias 字段别名
|
*
|
||||||
|
* @param alias 字段别名
|
||||||
* @param entityClass 实体类
|
* @param entityClass 实体类
|
||||||
* @param fieldName 字段名称
|
* @param fieldName 字段名称
|
||||||
*/
|
*/
|
||||||
public <E> JoinBuilder<T> selectAs(String alias, Class<E> entityClass, String fieldName) {
|
public <E> void selectAs(String alias, Class<E> entityClass, String fieldName) {
|
||||||
// 存储字段信息,稍后在configureSelections处理
|
|
||||||
if (selectedFields == null) {
|
|
||||||
selectedFields = new HashMap<>();
|
|
||||||
}
|
|
||||||
if (!selectedFields.containsKey(entityClass)) {
|
|
||||||
selectedFields.put(entityClass, new String[0]);
|
|
||||||
}
|
|
||||||
namedSelections.put(alias, new SelectionInfo(entityClass, fieldName));
|
namedSelections.put(alias, new SelectionInfo(entityClass, fieldName));
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行查询并返回结果
|
||||||
|
* @param entityManager EntityManager实例
|
||||||
|
* @return 查询结果列表
|
||||||
|
*/
|
||||||
|
public List<Object[]> execute(EntityManager entityManager) {
|
||||||
|
// 创建CriteriaBuilder
|
||||||
|
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||||
|
|
||||||
|
// 创建CriteriaQuery
|
||||||
|
CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
|
||||||
|
|
||||||
|
// 设置根实体
|
||||||
|
Root<T> root = criteriaQuery.from(rootClass);
|
||||||
|
|
||||||
|
// 创建连接关系
|
||||||
|
Map<Class<?>, From<?, ?>> fromMap = createJoins(root, criteriaQuery);
|
||||||
|
|
||||||
|
// 应用所有条件,包括表之间的连接条件和额外查询条件
|
||||||
|
List<Predicate> predicates = applyAllConditions(root, fromMap, criteriaBuilder);
|
||||||
|
|
||||||
|
// 添加条件到查询
|
||||||
|
if (!predicates.isEmpty()) {
|
||||||
|
criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[0])));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置查询结果投影
|
||||||
|
configureSelections(root, fromMap, criteriaQuery);
|
||||||
|
|
||||||
|
// 执行查询并返回结果
|
||||||
|
return entityManager.createQuery(criteriaQuery).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
public long count(EntityManager entityManager) {
|
public long count(EntityManager entityManager) {
|
||||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||||
|
@ -140,38 +163,7 @@ public class JoinBuilder<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行查询并返回结果
|
|
||||||
* @param entityManager EntityManager实例
|
|
||||||
* @return 查询结果列表
|
|
||||||
*/
|
|
||||||
public List<Object[]> execute(EntityManager entityManager) {
|
|
||||||
// 创建CriteriaBuilder
|
|
||||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
|
||||||
|
|
||||||
// 创建CriteriaQuery
|
|
||||||
CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
|
|
||||||
|
|
||||||
// 设置根实体
|
|
||||||
Root<T> root = criteriaQuery.from(rootClass);
|
|
||||||
|
|
||||||
// 创建连接关系
|
|
||||||
Map<Class<?>, From<?, ?>> fromMap = createJoins(root, criteriaQuery);
|
|
||||||
|
|
||||||
// 应用所有条件,包括表之间的连接条件和额外查询条件
|
|
||||||
List<Predicate> predicates = applyAllConditions(root, fromMap, criteriaBuilder);
|
|
||||||
|
|
||||||
// 添加条件到查询
|
|
||||||
if (!predicates.isEmpty()) {
|
|
||||||
criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[0])));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置查询结果投影
|
|
||||||
configureSelections(root, fromMap, criteriaQuery);
|
|
||||||
|
|
||||||
// 执行查询并返回结果
|
|
||||||
return entityManager.createQuery(criteriaQuery).getResultList();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建连接 - 修改为使用from而不是join
|
* 创建连接 - 修改为使用from而不是join
|
||||||
|
@ -286,31 +278,6 @@ public class JoinBuilder<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果没有命名选择或处理失败,回退到原来的处理方式
|
|
||||||
if (selectedFields != null && !selectedFields.isEmpty()) {
|
|
||||||
List<Selection<?>> selections = new ArrayList<>();
|
|
||||||
|
|
||||||
// 添加根实体的选择字段
|
|
||||||
if (selectedFields.containsKey(rootClass)) {
|
|
||||||
for (String field : selectedFields.get(rootClass)) {
|
|
||||||
selections.add(root.get(field));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加关联实体的选择字段
|
|
||||||
for (Class<?> entityClass : selectedFields.keySet()) {
|
|
||||||
if (entityClass != rootClass && fromMap.containsKey(entityClass)) {
|
|
||||||
From<?, ?> from = fromMap.get(entityClass);
|
|
||||||
for (String field : selectedFields.get(entityClass)) {
|
|
||||||
selections.add(from.get(field));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!selections.isEmpty()) {
|
|
||||||
query.multiselect(selections);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private <X> Predicate applyCondition(Condition condition, From<?, X> from, CriteriaBuilder criteriaBuilder) {
|
private <X> Predicate applyCondition(Condition condition, From<?, X> from, CriteriaBuilder criteriaBuilder) {
|
||||||
|
|
Loading…
Reference in New Issue