Skip to content

Commit bdb2e58

Browse files
committed
Add support for count statements
1 parent 055b3ed commit bdb2e58

File tree

1 file changed

+47
-39
lines changed

1 file changed

+47
-39
lines changed

src/main/java/org/mybatis/dynamic/sql/util/spring/SpringUtils.java

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,60 @@
3838
public class SpringUtils {
3939
private SpringUtils() {}
4040

41-
public static <T> List<T> selectList(Buildable<SelectModel> selectStatement, NamedParameterJdbcTemplate template,
41+
public static long count(NamedParameterJdbcTemplate template, Buildable<SelectModel> countStatement) {
42+
return count(template, countStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
43+
}
44+
45+
public static long count(NamedParameterJdbcTemplate template, SelectStatementProvider countStatement) {
46+
return template.queryForObject(countStatement.getSelectStatement(), countStatement.getParameters(), Long.class);
47+
}
48+
49+
public static int delete(NamedParameterJdbcTemplate template, Buildable<DeleteModel> deleteStatement) {
50+
return delete(template, deleteStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
51+
}
52+
53+
public static int delete(NamedParameterJdbcTemplate template, DeleteStatementProvider deleteStatement) {
54+
return template.update(deleteStatement.getDeleteStatement(), deleteStatement.getParameters());
55+
}
56+
57+
public static int generalInsert(NamedParameterJdbcTemplate template,
58+
Buildable<GeneralInsertModel> insertStatement) {
59+
return generalInsert(template, insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
60+
}
61+
62+
public static int generalInsert(NamedParameterJdbcTemplate template,
63+
GeneralInsertStatementProvider insertStatement) {
64+
return template.update(insertStatement.getInsertStatement(), insertStatement.getParameters());
65+
}
66+
67+
public static <T> int insert(NamedParameterJdbcTemplate template, Buildable<InsertModel<T>> insertStatement) {
68+
return insert(template, insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
69+
}
70+
71+
public static <T> int insert(NamedParameterJdbcTemplate template, InsertStatementProvider<T> insertStatement) {
72+
return template.update(insertStatement.getInsertStatement(),
73+
new BeanPropertySqlParameterSource(insertStatement.getRecord()));
74+
}
75+
76+
public static <T> List<T> selectList(NamedParameterJdbcTemplate template, Buildable<SelectModel> selectStatement,
4277
RowMapper<T> rowMapper) {
43-
return selectList(selectStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER), template,
78+
return selectList(template, selectStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER),
4479
rowMapper);
4580
}
4681

47-
public static <T> List<T> selectList(SelectStatementProvider selectStatement, NamedParameterJdbcTemplate template,
82+
public static <T> List<T> selectList(NamedParameterJdbcTemplate template, SelectStatementProvider selectStatement,
4883
RowMapper<T> rowMapper) {
4984
return template.query(selectStatement.getSelectStatement(), selectStatement.getParameters(), rowMapper);
5085
}
5186

52-
public static <T> Optional<T> selectOne(Buildable<SelectModel> selectStatement,
53-
NamedParameterJdbcTemplate template, RowMapper<T> rowMapper) {
54-
return selectOne(selectStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER),
55-
template, rowMapper);
87+
public static <T> Optional<T> selectOne(NamedParameterJdbcTemplate template, Buildable<SelectModel> selectStatement,
88+
RowMapper<T> rowMapper) {
89+
return selectOne(template, selectStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER),
90+
rowMapper);
5691
}
5792

58-
public static <T> Optional<T> selectOne(SelectStatementProvider selectStatement,
59-
NamedParameterJdbcTemplate template, RowMapper<T> rowMapper) {
93+
public static <T> Optional<T> selectOne(NamedParameterJdbcTemplate template, SelectStatementProvider selectStatement,
94+
RowMapper<T> rowMapper) {
6095
T result;
6196
try {
6297
result = template.queryForObject(selectStatement.getSelectStatement(), selectStatement.getParameters(),
@@ -68,38 +103,11 @@ public static <T> Optional<T> selectOne(SelectStatementProvider selectStatement,
68103
return Optional.ofNullable(result);
69104
}
70105

71-
public static int delete(Buildable<DeleteModel> deleteStatement, NamedParameterJdbcTemplate template) {
72-
return delete(deleteStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER), template);
73-
}
74-
75-
public static int delete(DeleteStatementProvider deleteStatement, NamedParameterJdbcTemplate template) {
76-
return template.update(deleteStatement.getDeleteStatement(), deleteStatement.getParameters());
77-
}
78-
79-
public static int generalInsert(Buildable<GeneralInsertModel> insertStatement,
80-
NamedParameterJdbcTemplate template) {
81-
return generalInsert(insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER), template);
82-
}
83-
84-
public static int generalInsert(GeneralInsertStatementProvider insertStatement,
85-
NamedParameterJdbcTemplate template) {
86-
return template.update(insertStatement.getInsertStatement(), insertStatement.getParameters());
87-
}
88-
89-
public static <T> int insert(Buildable<InsertModel<T>> insertStatement, NamedParameterJdbcTemplate template) {
90-
return insert(insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER), template);
91-
}
92-
93-
public static <T> int insert(InsertStatementProvider<T> insertStatement, NamedParameterJdbcTemplate template) {
94-
return template.update(insertStatement.getInsertStatement(),
95-
new BeanPropertySqlParameterSource(insertStatement.getRecord()));
96-
}
97-
98-
public static int update(Buildable<UpdateModel> updateStatement, NamedParameterJdbcTemplate template) {
99-
return update(updateStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER), template);
106+
public static int update(NamedParameterJdbcTemplate template, Buildable<UpdateModel> updateStatement) {
107+
return update(template, updateStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
100108
}
101109

102-
public static int update(UpdateStatementProvider updateStatement, NamedParameterJdbcTemplate template) {
110+
public static int update(NamedParameterJdbcTemplate template, UpdateStatementProvider updateStatement) {
103111
return template.update(updateStatement.getUpdateStatement(), updateStatement.getParameters());
104112
}
105113
}

0 commit comments

Comments
 (0)