Skip to content

Commit bf0c57a

Browse files
committed
Add an extension like class for Spring support
1 parent ee35e82 commit bf0c57a

File tree

5 files changed

+144
-74
lines changed

5 files changed

+144
-74
lines changed

src/main/java/org/mybatis/dynamic/sql/ParameterTypeConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*
2424
* <p>A parameter type converter is associated with a SqlColumn.
2525
*
26-
* <p>A parameter type converter is compatible with Spring's general Converter interface so existing converters can be reused
27-
* here if they are marked with this additional interface.
26+
* <p>A parameter type converter is compatible with Spring's general Converter interface so existing converters
27+
* can be reused here if they are marked with this additional interface.
2828
*
2929
* <p>The converter is only used for parameters - it is not used for result set processing. The converter will be
3030
* called in the following circumstances:

src/main/java/org/mybatis/dynamic/sql/insert/render/GeneralInsertValuePhraseVisitor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public <T> Optional<FieldAndValueAndParameters> visit(ValueWhenPresentMapping<T>
6969
return mapping.value().flatMap(v -> buildValueFragment(mapping, v));
7070
}
7171

72-
private <T> Optional<FieldAndValueAndParameters> buildValueFragment(AbstractColumnMapping<T> mapping, Object value) {
72+
private <T> Optional<FieldAndValueAndParameters> buildValueFragment(AbstractColumnMapping<T> mapping,
73+
Object value) {
7374
return buildFragment(mapping, value);
7475
}
7576

src/main/java/org/mybatis/dynamic/sql/update/render/UpdateRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private Optional<WhereClauseProvider> renderWhereClause(WhereModel whereModel) {
111111
.render();
112112
}
113113

114-
private <T> Function<AbstractColumnMapping<?>, Optional<FragmentAndParameters>> toFragmentAndParameters(
114+
private Function<AbstractColumnMapping<?>, Optional<FragmentAndParameters>> toFragmentAndParameters(
115115
SetPhraseVisitor visitor) {
116116
return updateMapping -> toFragmentAndParameters(visitor, updateMapping);
117117
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* Copyright 2016-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util.spring;
17+
18+
import java.util.List;
19+
import java.util.Objects;
20+
import java.util.Optional;
21+
22+
import org.mybatis.dynamic.sql.delete.DeleteModel;
23+
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
24+
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
25+
import org.mybatis.dynamic.sql.insert.InsertModel;
26+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
27+
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
28+
import org.mybatis.dynamic.sql.select.SelectModel;
29+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
30+
import org.mybatis.dynamic.sql.update.UpdateModel;
31+
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
32+
import org.mybatis.dynamic.sql.util.Buildable;
33+
import org.springframework.dao.EmptyResultDataAccessException;
34+
import org.springframework.jdbc.core.RowMapper;
35+
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
36+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
37+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
38+
import org.springframework.jdbc.support.KeyHolder;
39+
40+
public class NamedParameterJdbcTemplateExtensions {
41+
private NamedParameterJdbcTemplate template;
42+
43+
public NamedParameterJdbcTemplateExtensions(NamedParameterJdbcTemplate template) {
44+
this.template = Objects.requireNonNull(template);
45+
}
46+
47+
public long count(Buildable<SelectModel> countStatement) {
48+
return count(SpringUtils.buildSelect(countStatement));
49+
}
50+
51+
public long count(SelectStatementProvider countStatement) {
52+
return template.queryForObject(countStatement.getSelectStatement(), countStatement.getParameters(), Long.class);
53+
}
54+
55+
public int delete(Buildable<DeleteModel> deleteStatement) {
56+
return delete(SpringUtils.buildDelete(deleteStatement));
57+
}
58+
59+
public int delete(DeleteStatementProvider deleteStatement) {
60+
return template.update(deleteStatement.getDeleteStatement(), deleteStatement.getParameters());
61+
}
62+
63+
public int generalInsert(Buildable<GeneralInsertModel> insertStatement) {
64+
return generalInsert(SpringUtils.buildGeneralInsert(insertStatement));
65+
}
66+
67+
public int generalInsert(GeneralInsertStatementProvider insertStatement) {
68+
return template.update(insertStatement.getInsertStatement(), insertStatement.getParameters());
69+
}
70+
71+
public int generalInsert(Buildable<GeneralInsertModel> insertStatement, KeyHolder keyHolder) {
72+
return generalInsert(SpringUtils.buildGeneralInsert(insertStatement), keyHolder);
73+
}
74+
75+
public int generalInsert(GeneralInsertStatementProvider insertStatement, KeyHolder keyHolder) {
76+
return template.update(insertStatement.getInsertStatement(),
77+
new MapSqlParameterSource(insertStatement.getParameters()), keyHolder);
78+
}
79+
80+
public <T> int insert(Buildable<InsertModel<T>> insertStatement) {
81+
return insert(SpringUtils.buildInsert(insertStatement));
82+
}
83+
84+
public <T> int insert(InsertStatementProvider<T> insertStatement) {
85+
return template.update(insertStatement.getInsertStatement(),
86+
new BeanPropertySqlParameterSource(insertStatement.getRecord()));
87+
}
88+
89+
public <T> int insert(Buildable<InsertModel<T>> insertStatement, KeyHolder keyHolder) {
90+
return insert(SpringUtils.buildInsert(insertStatement), keyHolder);
91+
}
92+
93+
public <T> int insert(InsertStatementProvider<T> insertStatement, KeyHolder keyHolder) {
94+
return template.update(insertStatement.getInsertStatement(),
95+
new BeanPropertySqlParameterSource(insertStatement.getRecord()), keyHolder);
96+
}
97+
98+
public <T> List<T> selectList(Buildable<SelectModel> selectStatement, RowMapper<T> rowMapper) {
99+
return selectList(SpringUtils.buildSelect(selectStatement), rowMapper);
100+
}
101+
102+
public <T> List<T> selectList(SelectStatementProvider selectStatement, RowMapper<T> rowMapper) {
103+
return template.query(selectStatement.getSelectStatement(), selectStatement.getParameters(), rowMapper);
104+
}
105+
106+
public <T> Optional<T> selectOne(Buildable<SelectModel> selectStatement, RowMapper<T> rowMapper) {
107+
return selectOne(SpringUtils.buildSelect(selectStatement), rowMapper);
108+
}
109+
110+
public <T> Optional<T> selectOne(SelectStatementProvider selectStatement, RowMapper<T> rowMapper) {
111+
T result;
112+
try {
113+
result = template.queryForObject(selectStatement.getSelectStatement(), selectStatement.getParameters(),
114+
rowMapper);
115+
} catch (EmptyResultDataAccessException e) {
116+
result = null;
117+
}
118+
119+
return Optional.ofNullable(result);
120+
}
121+
122+
public int update(Buildable<UpdateModel> updateStatement) {
123+
return update(SpringUtils.buildUpdate(updateStatement));
124+
}
125+
126+
public int update(UpdateStatementProvider updateStatement) {
127+
return template.update(updateStatement.getUpdateStatement(), updateStatement.getParameters());
128+
}
129+
}

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

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616
package org.mybatis.dynamic.sql.util.spring;
1717

18-
import java.util.List;
19-
import java.util.Optional;
20-
2118
import org.mybatis.dynamic.sql.delete.DeleteModel;
2219
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
2320
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
@@ -30,84 +27,27 @@
3027
import org.mybatis.dynamic.sql.update.UpdateModel;
3128
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
3229
import org.mybatis.dynamic.sql.util.Buildable;
33-
import org.springframework.dao.EmptyResultDataAccessException;
34-
import org.springframework.jdbc.core.RowMapper;
35-
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
36-
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
3730

3831
public class SpringUtils {
3932
private SpringUtils() {}
4033

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));
34+
public static DeleteStatementProvider buildDelete(Buildable<DeleteModel> deleteStatement) {
35+
return deleteStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER);
5136
}
5237

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,
77-
RowMapper<T> rowMapper) {
78-
return selectList(template, selectStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER),
79-
rowMapper);
80-
}
81-
82-
public static <T> List<T> selectList(NamedParameterJdbcTemplate template, SelectStatementProvider selectStatement,
83-
RowMapper<T> rowMapper) {
84-
return template.query(selectStatement.getSelectStatement(), selectStatement.getParameters(), rowMapper);
85-
}
86-
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);
38+
public static GeneralInsertStatementProvider buildGeneralInsert(Buildable<GeneralInsertModel> insertStatement) {
39+
return insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER);
9140
}
9241

93-
public static <T> Optional<T> selectOne(NamedParameterJdbcTemplate template, SelectStatementProvider selectStatement,
94-
RowMapper<T> rowMapper) {
95-
T result;
96-
try {
97-
result = template.queryForObject(selectStatement.getSelectStatement(), selectStatement.getParameters(),
98-
rowMapper);
99-
} catch (EmptyResultDataAccessException e) {
100-
result = null;
101-
}
102-
103-
return Optional.ofNullable(result);
42+
public static <T> InsertStatementProvider<T> buildInsert(Buildable<InsertModel<T>> insertStatement) {
43+
return insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER);
10444
}
10545

106-
public static int update(NamedParameterJdbcTemplate template, Buildable<UpdateModel> updateStatement) {
107-
return update(template, updateStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
46+
public static SelectStatementProvider buildSelect(Buildable<SelectModel> selectStatement) {
47+
return selectStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER);
10848
}
10949

110-
public static int update(NamedParameterJdbcTemplate template, UpdateStatementProvider updateStatement) {
111-
return template.update(updateStatement.getUpdateStatement(), updateStatement.getParameters());
50+
public static UpdateStatementProvider buildUpdate(Buildable<UpdateModel> updateStatement) {
51+
return updateStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER);
11252
}
11353
}

0 commit comments

Comments
 (0)