com.querydsl.core.Tuple Java Examples

The following examples show how to use com.querydsl.core.Tuple. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseService.java    From ZTuoExchange_framework with MIT License 7 votes vote down vote up
/**
 * @param expressions        查询列表
 * @param entityPaths        查询表
 * @param predicates         条件
 * @param orderSpecifierList 排序
 * @param pageNo             页码
 * @param pageSize           页面大小
 */
@Transactional(readOnly = true)
public PageListMapResult queryDslForPageListResult(
        List<Expression> expressions,
        List<EntityPath> entityPaths,
        List<Predicate> predicates,
        List<OrderSpecifier> orderSpecifierList,
        Integer pageNo,
        Integer pageSize) {
    JPAQuery<Tuple> jpaQuery = queryFactory.select(expressions.toArray(new Expression[expressions.size()]))
            .from(entityPaths.toArray(new EntityPath[entityPaths.size()]))
            .where(predicates.toArray(new Predicate[predicates.size()]));
    List<Tuple> tuples = jpaQuery.orderBy(orderSpecifierList.toArray(new OrderSpecifier[orderSpecifierList.size()]))
            .offset((pageNo - 1) * pageSize).limit(pageSize)
            .fetch();
    List<Map<String, Object>> list = new LinkedList<>();//返回结果
    //封装结果
    for (int i = 0; i < tuples.size(); i++) {
        //遍历tuples
        Map<String, Object> map = new LinkedHashMap<>();//一条信息
        for (Expression expression : expressions) {
            map.put(expression.toString().split(" as ")[1],//别名作为Key
                    tuples.get(i).get(expression));//获取结果
        }
        list.add(map);
    }
    PageListMapResult pageListMapResult = new PageListMapResult(list, pageNo, pageSize, jpaQuery.fetchCount());//分页封装
    return pageListMapResult;
}
 
Example #2
Source File: BaseService.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
@Transactional(readOnly = true)
public PageListMapResult queryDslForPageListResult(QueryDslContext qdc, Integer pageNo, Integer pageSize) {
    JPAQuery<Tuple> jpaQuery = queryFactory.select(qdc.expressionToArray())
            .from(qdc.entityPathToArray())
            .where(qdc.predicatesToArray());
    List<Tuple> tuples = jpaQuery.orderBy(qdc.orderSpecifiersToArray())
            .offset((pageNo - 1) * pageSize).limit(pageSize)
            .fetch();
    List<Map<String, Object>> list = new LinkedList<>();//返回结果
    //封装结果
    for (int i = 0; i < tuples.size(); i++) {
        //遍历tuples
        Map<String, Object> map = new LinkedHashMap<>();//一条信息
        for (Expression expression : qdc.getExpressions()) {
            map.put(expression.toString().split(" as ")[1],//别名作为Key
                    tuples.get(i).get(expression));//获取结果
        }
        list.add(map);
    }
    PageListMapResult pageListMapResult = new PageListMapResult(list, pageNo, pageSize, jpaQuery.fetchCount());//分页封装
    return pageListMapResult;
}
 
Example #3
Source File: BaseService.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
@Transactional(readOnly = true)
public PageListMapResult queryDslForPageListResult(QueryDslContext qdc, Integer pageNo, Integer pageSize) {
    JPAQuery<Tuple> jpaQuery = queryFactory.select(qdc.expressionToArray())
            .from(qdc.entityPathToArray())
            .where(qdc.predicatesToArray());
    List<Tuple> tuples = jpaQuery.orderBy(qdc.orderSpecifiersToArray())
            .offset((pageNo - 1) * pageSize).limit(pageSize)
            .fetch();
    List<Map<String, Object>> list = new LinkedList<>();//返回结果
    //封装结果
    for (int i = 0; i < tuples.size(); i++) {
        //遍历tuples
        Map<String, Object> map = new LinkedHashMap<>();//一条信息
        for (Expression expression : qdc.getExpressions()) {
            map.put(expression.toString().split(" as ")[1],//别名作为Key
                    tuples.get(i).get(expression));//获取结果
        }
        list.add(map);
    }
    PageListMapResult pageListMapResult = new PageListMapResult(list, pageNo, pageSize, jpaQuery.fetchCount());//分页封装
    return pageListMapResult;
}
 
Example #4
Source File: QueryDSLIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenGroupingByTitle_thenReturnsTuples() {

    QBlogPost blogPost = QBlogPost.blogPost;

    NumberPath<Long> count = Expressions.numberPath(Long.class, "c");

    List<Tuple> userTitleCounts = queryFactory.select(blogPost.title, blogPost.id.count().as(count))
            .from(blogPost)
            .groupBy(blogPost.title)
            .orderBy(count.desc())
            .fetch();

    assertEquals("Hello World!", userTitleCounts.get(0).get(blogPost.title));
    assertEquals(new Long(2), userTitleCounts.get(0).get(count));

    assertEquals("My Second Post", userTitleCounts.get(1).get(blogPost.title));
    assertEquals(new Long(1), userTitleCounts.get(1).get(count));

}
 
Example #5
Source File: BaseService.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
/**
 * @param expressions        查询列表
 * @param entityPaths        查询表
 * @param predicates         条件
 * @param orderSpecifierList 排序
 * @param pageNo             页码
 * @param pageSize           页面大小
 */
@Transactional(readOnly = true)
public PageListMapResult queryDslForPageListResult(
        List<Expression> expressions,
        List<EntityPath> entityPaths,
        List<Predicate> predicates,
        List<OrderSpecifier> orderSpecifierList,
        Integer pageNo,
        Integer pageSize) {
    JPAQuery<Tuple> jpaQuery = queryFactory.select(expressions.toArray(new Expression[expressions.size()]))
            .from(entityPaths.toArray(new EntityPath[entityPaths.size()]))
            .where(predicates.toArray(new Predicate[predicates.size()]));
    List<Tuple> tuples = jpaQuery.orderBy(orderSpecifierList.toArray(new OrderSpecifier[orderSpecifierList.size()]))
            .offset((pageNo - 1) * pageSize).limit(pageSize)
            .fetch();
    List<Map<String, Object>> list = new LinkedList<>();//返回结果
    //封装结果
    for (int i = 0; i < tuples.size(); i++) {
        //遍历tuples
        Map<String, Object> map = new LinkedHashMap<>();//一条信息
        for (Expression expression : expressions) {
            map.put(expression.toString().split(" as ")[1],//别名作为Key
                    tuples.get(i).get(expression));//获取结果
        }
        list.add(map);
    }
    PageListMapResult pageListMapResult = new PageListMapResult(list, pageNo, pageSize, jpaQuery.fetchCount());//分页封装
    return pageListMapResult;
}
 
Example #6
Source File: TupleUtils.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
/**
 * @param tuples      查询结果
 * @param expressions 查询字段
 * @return
 */
public static List<Map<String, Object>> tupleToMap(List<Tuple> tuples, List<Expression> expressions) {
    List<Map<String, Object>> list = new LinkedList<>();//返回结果
    //封装结果
    for (int i = 0; i < tuples.size(); i++) {
        //遍历tuples
        Map<String, Object> map = new LinkedHashMap<>();//一条信息
        for (Expression expression : expressions) {
            map.put(expression.toString().split(" as ")[1],//别名作为Key
                    tuples.get(i).get(expression));//获取结果
        }
        list.add(map);
    }
    return list;
}
 
Example #7
Source File: TupleUtils.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
/**
 * @param tuples      查询结果
 * @param expressions 查询字段
 * @return
 */
public static List<Map<String, Object>> tupleToMap(List<Tuple> tuples, List<Expression> expressions) {
    List<Map<String, Object>> list = new LinkedList<>();//返回结果
    //封装结果
    for (int i = 0; i < tuples.size(); i++) {
        //遍历tuples
        Map<String, Object> map = new LinkedHashMap<>();//一条信息
        for (Expression expression : expressions) {
            map.put(expression.toString().split(" as ")[1],//别名作为Key
                    tuples.get(i).get(expression));//获取结果
        }
        list.add(map);
    }
    return list;
}
 
Example #8
Source File: QuerydslExecutorImpl.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Override
public List<QuerydslTuple> getResultTuples() {
	List<?> results = executeQuery();

	List<QuerydslTuple> tuples = new ArrayList<>();
	for (Object result : results) {
		if (result instanceof Tuple) {
			tuples.add(new QuerydslTupleImpl((Tuple) result, selectionBindings));
		} else {
			tuples.add(new QuerydslObjectArrayTupleImpl(result, selectionBindings));
		}
	}
	return tuples;
}
 
Example #9
Source File: QueryDslTupleImplTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {

	expression = Mockito.mock(Expression.class);
	Tuple tuple = Mockito.mock(Tuple.class);
	Mockito.when(tuple.size()).thenReturn(2);
	Mockito.when(tuple.get(expression)).thenReturn("test");
	Mockito.when(tuple.toArray()).thenReturn(new Object[]{"0", "1"});
	Mockito.when(tuple.get(0, String.class)).thenReturn("0");
	Mockito.when(tuple.get(1, String.class)).thenReturn("1");

	Mockito.when(tuple.size()).thenReturn(2);
	Map<String, Integer> selectionBindings = new HashMap<>();
	impl = new QuerydslTupleImpl(tuple, selectionBindings);
}
 
Example #10
Source File: QuerydslExecutorImpl.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Override
public List<QuerydslTuple> getResultTuples() {
	List<?> results = executeQuery();

	List<QuerydslTuple> tuples = new ArrayList<>();
	for (Object result : results) {
		if (result instanceof Tuple) {
			tuples.add(new QuerydslTupleImpl((Tuple) result, selectionBindings));
		}
		else {
			tuples.add(new ObjectArrayTupleImpl(result));
		}
	}
	return tuples;
}
 
Example #11
Source File: QueryDslTupleImplTest.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	Tuple tuple = Mockito.mock(Tuple.class);
	Mockito.when(tuple.size()).thenReturn(2);
	Mockito.when(tuple.toArray()).thenReturn(new Object[] { "0", "1" });
	Mockito.when(tuple.get(0, String.class)).thenReturn("0");
	Mockito.when(tuple.get(1, String.class)).thenReturn("1");
	Map<String, Integer> selectionBindings = new HashMap<>();
	impl = new QuerydslTupleImpl(tuple, selectionBindings);
}
 
Example #12
Source File: QuerydslTupleImpl.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
public QuerydslTupleImpl(Tuple tuple, Map<String, Integer> selectionBindings) {
	this.tuple = tuple;
	this.selectionBindings = selectionBindings;
}
 
Example #13
Source File: QuerydslTupleImpl.java    From katharsis-framework with Apache License 2.0 4 votes vote down vote up
public QuerydslTupleImpl(Tuple tuple, Map<String, Integer> selectionBindings) {
	this.tuple = tuple;
	this.selectionBindings = selectionBindings;
}
 
Example #14
Source File: FeedEntryDAO.java    From commafeed with Apache License 2.0 4 votes vote down vote up
public List<FeedCapacity> findFeedsExceedingCapacity(long maxCapacity, long max) {
	NumberExpression<Long> count = entry.id.count();
	List<Tuple> tuples = query().select(entry.feed.id, count).from(entry).groupBy(entry.feed).having(count.gt(maxCapacity)).limit(max)
			.fetch();
	return tuples.stream().map(t -> new FeedCapacity(t.get(entry.feed.id), t.get(count))).collect(Collectors.toList());
}