Skip to content

Commit 2891df2

Browse files
committed
1 parent 7359215 commit 2891df2

File tree

9 files changed

+86
-25
lines changed

9 files changed

+86
-25
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>com.codingapi.springboot</groupId>
1414
<artifactId>springboot-parent</artifactId>
15-
<version>3.2.3</version>
15+
<version>3.2.4</version>
1616

1717
<url>https://github.com/codingapi/springboot-framewrok</url>
1818
<name>springboot-parent</name>

springboot-starter-data-fast/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.2.3</version>
8+
<version>3.2.4</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/jdbc/JdbcQuery.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,22 @@ public <T> Page<T> queryForPage(String sql, String countSql, Class<T> clazz, Pag
5151
return new PageImpl<>(list, pageRequest, count);
5252
}
5353

54+
public <T> Page<T> queryForPage(String sql, Class<T> clazz, PageRequest pageRequest, Object... params) {
55+
String countSql = "select count(1) "+sql;
56+
return this.queryForPage(sql, countSql, clazz, pageRequest, params);
57+
}
58+
5459
public Page<Map<String, Object>> queryForPage(String sql, String countSql, PageRequest pageRequest, Object... params) {
5560
List<Map<String, Object>> list = jdbcTemplate.query(sql, params, new CamelCaseRowMapper());
56-
5761
long count = this.countQuery(countSql, params);
5862
return new PageImpl<>(list, pageRequest, count);
5963
}
6064

65+
public Page<Map<String, Object>> queryForPage(String sql, PageRequest pageRequest, Object... params) {
66+
String countSql = "select count(1) "+sql;
67+
return this.queryForPage(sql, countSql, pageRequest, params);
68+
}
69+
6170

6271
private long countQuery(String sql, Object... params) {
6372
int paramsLength = params.length;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codingapi.springboot.fast.jpa.repository;
2+
3+
import org.springframework.core.ResolvableType;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.repository.NoRepositoryBean;
6+
7+
@NoRepositoryBean
8+
public interface BaseRepository<T,ID> extends JpaRepository<T,ID> {
9+
10+
@SuppressWarnings("unchecked")
11+
default Class<T> getEntityClass() {
12+
ResolvableType resolvableType = ResolvableType.forClass(getClass()).as(BaseRepository.class);
13+
return (Class<T>) resolvableType.getGeneric(0).resolve();
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.codingapi.springboot.fast.jpa.repository;
2+
3+
import com.codingapi.springboot.fast.jdbc.JdbcQueryContext;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.PageRequest;
6+
import org.springframework.data.repository.NoRepositoryBean;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
@NoRepositoryBean
12+
public interface DynamicNativeRepository<T, ID> extends BaseRepository<T, ID> {
13+
14+
default List<Map<String, Object>> dynamicNativeListMapQuery(String sql, Object... params) {
15+
return JdbcQueryContext.getInstance().getJdbcQuery().queryForList(sql, params);
16+
}
17+
18+
default List<T> dynamicNativeListQuery(String sql, Object... params) {
19+
return dynamicNativeListQuery(getEntityClass(), sql, params);
20+
}
21+
22+
default <V> List<V> dynamicNativeListQuery(Class<V> clazz, String sql, Object... params) {
23+
return JdbcQueryContext.getInstance().getJdbcQuery().queryForList(sql, clazz, params);
24+
}
25+
26+
default Page<T> dynamicNativePageQuery(String sql, String countSql, PageRequest request, Object... params) {
27+
return dynamicNativePageQuery(getEntityClass(), sql, countSql, request, params);
28+
}
29+
30+
default Page<T> dynamicNativePageQuery(String sql, PageRequest request, Object... params) {
31+
return dynamicNativePageQuery(getEntityClass(), sql, request, params);
32+
}
33+
34+
default <V> Page<V> dynamicNativePageQuery(Class<V> clazz, String sql, String countSql, PageRequest request, Object... params) {
35+
return JdbcQueryContext.getInstance().getJdbcQuery().queryForPage(sql, countSql, clazz, request, params);
36+
}
37+
38+
default <V> Page<V> dynamicNativePageQuery(Class<V> clazz, String sql, PageRequest request, Object... params) {
39+
return JdbcQueryContext.getInstance().getJdbcQuery().queryForPage(sql, clazz, request, params);
40+
}
41+
42+
default Page<Map<String, Object>> dynamicNativePageMapQuery(String sql, String countSql, PageRequest request, Object... params) {
43+
return JdbcQueryContext.getInstance().getJdbcQuery().queryForPage(sql, countSql, request, params);
44+
}
45+
46+
default Page<Map<String, Object>> dynamicNativePageMapQuery(String sql, PageRequest request, Object... params) {
47+
return JdbcQueryContext.getInstance().getJdbcQuery().queryForPage(sql, request, params);
48+
}
49+
50+
}

springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/jpa/repository/DynamicRepository.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
package com.codingapi.springboot.fast.jpa.repository;
22

33
import com.codingapi.springboot.fast.jpa.JpaQueryContext;
4-
import org.springframework.core.ResolvableType;
54
import org.springframework.data.domain.Page;
65
import org.springframework.data.domain.PageRequest;
7-
import org.springframework.data.jpa.repository.JpaRepository;
86
import org.springframework.data.repository.NoRepositoryBean;
97

108
import java.util.List;
119

1210
@NoRepositoryBean
1311
@SuppressWarnings("unchecked")
14-
public interface DynamicRepository<T, ID> extends JpaRepository<T, ID> {
15-
16-
default Class<?> getEntityClass() {
17-
ResolvableType resolvableType = ResolvableType.forClass(this.getClass()).as(DynamicRepository.class);
18-
return resolvableType.getGeneric(new int[]{0}).resolve();
19-
}
12+
public interface DynamicRepository<T, ID> extends BaseRepository<T, ID> {
2013

2114
default List<T> dynamicListQuery(String sql, Object... params) {
2215
return (List<T>) JpaQueryContext.getInstance().getJPAQuery().listQuery(getEntityClass(), sql, params);

springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/jpa/repository/FastRepository.java

+5-12
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,33 @@
22

33
import com.codingapi.springboot.framework.dto.request.PageRequest;
44
import com.codingapi.springboot.framework.dto.request.SearchRequest;
5-
import org.springframework.core.ResolvableType;
65
import org.springframework.data.domain.Page;
76
import org.springframework.data.jpa.repository.JpaRepository;
87
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
98
import org.springframework.data.repository.NoRepositoryBean;
109

1110
/**
1211
* 更强大的Repository对象
12+
*
1313
* @param <T>
1414
* @param <ID>
1515
*/
1616
@NoRepositoryBean
17-
public interface FastRepository<T, ID> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T>, DynamicRepository<T, ID> {
17+
public interface FastRepository<T, ID> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T>, DynamicRepository<T, ID>, DynamicNativeRepository<T, ID> {
1818

1919
default Page<T> findAll(PageRequest request) {
2020
if (request.hasFilter()) {
21-
Class<T> clazz = getDomainClass();
21+
Class<T> clazz = getEntityClass();
2222
ExampleBuilder exampleBuilder = new ExampleBuilder(request, clazz);
2323
return findAll(exampleBuilder.getExample(), request);
2424
}
2525
return findAll((org.springframework.data.domain.PageRequest) request);
2626
}
2727

2828

29-
@SuppressWarnings("unchecked")
30-
default Class<T> getDomainClass() {
31-
ResolvableType resolvableType = ResolvableType.forClass(getClass()).as(FastRepository.class);
32-
return (Class<T>) resolvableType.getGeneric(0).resolve();
33-
}
34-
35-
3629
default Page<T> pageRequest(PageRequest request) {
3730
if (request.hasFilter()) {
38-
Class<T> clazz = getDomainClass();
31+
Class<T> clazz = getEntityClass();
3932
DynamicSQLBuilder dynamicSQLBuilder = new DynamicSQLBuilder(request, clazz);
4033
return dynamicPageQuery(dynamicSQLBuilder.getHQL(), request, dynamicSQLBuilder.getParams());
4134
}
@@ -44,7 +37,7 @@ default Page<T> pageRequest(PageRequest request) {
4437

4538

4639
default Page<T> searchRequest(SearchRequest request) {
47-
Class<T> clazz = getDomainClass();
40+
Class<T> clazz = getEntityClass();
4841
return pageRequest(request.toPageRequest(clazz));
4942
}
5043

springboot-starter-security/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>3.2.3</version>
9+
<version>3.2.4</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-security</artifactId>

springboot-starter/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.codingapi.springboot</groupId>
77
<artifactId>springboot-parent</artifactId>
8-
<version>3.2.3</version>
8+
<version>3.2.4</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

0 commit comments

Comments
 (0)