Skip to content

Commit 4c14a38

Browse files
committed
新增count 求集合数
1 parent 333695c commit 4c14a38

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

jpa-lambda-core/src/main/java/com/github/xuejike/query/jpa/lambda/JpaQuery.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ public JpaLambdaQuery<T> lambda(){
3131
JpaLambdaQuery<T> query = new JpaLambdaQuery<T>(this);
3232
return query;
3333
}
34+
@Override
3435
public JpaQuery<T> orderAsc(String ...fields){
3536
for (String field : fields) {
3637
orderList.add(Order.asc(field));
3738
}
3839
return this;
3940
}
41+
@Override
4042
public JpaQuery<T> orderDesc(String ...fields){
4143
for (String field : fields) {
4244
orderList.add(Order.desc(field));
@@ -60,17 +62,10 @@ public JpaQuery<T> distinct(String field, String alias) {
6062
return this;
6163
}
6264

63-
@Override
64-
public JpaQuery<T> count(String field, String alias) {
65-
Projection property = Projections.count(field);
66-
if (alias != null){
67-
property = ((CountProjection) property).as(alias);
68-
}
69-
selectProjectionList.add(property);
70-
return this;
71-
}
7265

73-
public JpaQuery<T> select(String field,String alias){
66+
67+
@Override
68+
public JpaQuery<T> select(String field, String alias){
7469
Projection property = Projections.property(field);
7570
if (alias != null){
7671
property = ((PropertyProjection) property).as(alias);
@@ -342,7 +337,8 @@ public JpaQuery<T> allEq(Map<String,Object> eqMap){
342337
whereCriterionList.add(Restrictions.allEq(eqMap));
343338
return this;
344339
}
345-
public JpaQuery<T> example(T obj,MatchMode likeModel,String ... excludeProperties){
340+
@Override
341+
public JpaQuery<T> example(T obj, MatchMode likeModel, String ... excludeProperties){
346342
Example example = Example.create(obj);
347343
Optional.ofNullable(likeModel).ifPresent(example::enableLike);
348344
Optional.ofNullable(excludeProperties)

jpa-lambda-core/src/main/java/com/github/xuejike/query/jpa/lambda/core/AbstractJpaQuery.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ public Page<T> pageList(IPage iPage){
119119

120120
return page;
121121
}
122+
public Long count(){
123+
Long count = (Long) createCriteria(true)
124+
.setProjection(Projections.rowCount())
125+
.uniqueResult();
126+
return count;
127+
}
128+
public Long count(String field){
129+
Projection property = Projections.count(field);
130+
131+
Long count = (Long) createCriteria(true).setProjection(property).uniqueResult();
132+
return count;
133+
}
122134
public <E>List<E> getObjList(Class<E> dto){
123135
return createCriteria().setResultTransformer(Transformers.aliasToBean(dto)).list();
124136
}

jpa-lambda-core/src/main/java/com/github/xuejike/query/jpa/lambda/core/SelectCriteria.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ default R select(F field){
99
default R distinct(F field){
1010
return distinct(field,null);
1111
}
12-
R count(F field,F alias);
13-
default R count(F field){
14-
return count(field,null);
15-
}
12+
1613
R max(F field,F alias);
1714
default R max(F field){return max(field,null);}
1815

0 commit comments

Comments
 (0)