View Javadoc
1   /*
2    *    Copyright 2016-2025 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    *       https://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.BatchInsertModel;
25  import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
26  import org.mybatis.dynamic.sql.insert.InsertModel;
27  import org.mybatis.dynamic.sql.insert.MultiRowInsertModel;
28  import org.mybatis.dynamic.sql.insert.render.BatchInsert;
29  import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
30  import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
31  import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
32  import org.mybatis.dynamic.sql.render.RenderingStrategies;
33  import org.mybatis.dynamic.sql.select.SelectModel;
34  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
35  import org.mybatis.dynamic.sql.update.UpdateModel;
36  import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
37  import org.mybatis.dynamic.sql.util.Buildable;
38  import org.mybatis.dynamic.sql.util.Utilities;
39  import org.springframework.dao.EmptyResultDataAccessException;
40  import org.springframework.jdbc.core.RowMapper;
41  import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
42  import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
43  import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
44  import org.springframework.jdbc.support.KeyHolder;
45  
46  public class NamedParameterJdbcTemplateExtensions {
47      private final NamedParameterJdbcTemplate template;
48  
49      public NamedParameterJdbcTemplateExtensions(NamedParameterJdbcTemplate template) {
50          this.template = Objects.requireNonNull(template);
51      }
52  
53      public long count(Buildable<SelectModel> countStatement) {
54          return count(countStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
55      }
56  
57      public long count(SelectStatementProvider countStatement) {
58          Long answer = template.queryForObject(countStatement.getSelectStatement(),
59                  countStatement.getParameters(), Long.class);
60  
61          return Utilities.safelyUnbox(answer);
62      }
63  
64      public int delete(Buildable<DeleteModel> deleteStatement) {
65          return delete(deleteStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
66      }
67  
68      public int delete(DeleteStatementProvider deleteStatement) {
69          return template.update(deleteStatement.getDeleteStatement(), deleteStatement.getParameters());
70      }
71  
72      public int generalInsert(Buildable<GeneralInsertModel> insertStatement) {
73          return generalInsert(insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
74      }
75  
76      public int generalInsert(GeneralInsertStatementProvider insertStatement) {
77          return template.update(insertStatement.getInsertStatement(), insertStatement.getParameters());
78      }
79  
80      public int generalInsert(Buildable<GeneralInsertModel> insertStatement, KeyHolder keyHolder) {
81          return generalInsert(insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER), keyHolder);
82      }
83  
84      public int generalInsert(GeneralInsertStatementProvider insertStatement, KeyHolder keyHolder) {
85          return template.update(insertStatement.getInsertStatement(),
86                  new MapSqlParameterSource(insertStatement.getParameters()), keyHolder);
87      }
88  
89      public <T> int insert(Buildable<InsertModel<T>> insertStatement) {
90          return insert(insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
91      }
92  
93      public <T> int insert(InsertStatementProvider<T> insertStatement) {
94          return template.update(insertStatement.getInsertStatement(),
95                  new BeanPropertySqlParameterSource(insertStatement));
96      }
97  
98      public <T> int insert(Buildable<InsertModel<T>> insertStatement, KeyHolder keyHolder) {
99          return insert(insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER), keyHolder);
100     }
101 
102     public <T> int insert(InsertStatementProvider<T> insertStatement, KeyHolder keyHolder) {
103         return template.update(insertStatement.getInsertStatement(),
104                 new BeanPropertySqlParameterSource(insertStatement), keyHolder);
105     }
106 
107     public <T> int[] insertBatch(Buildable<BatchInsertModel<T>> insertStatement) {
108         return insertBatch(insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
109     }
110 
111     public <T> int[] insertBatch(BatchInsert<T> insertStatement) {
112         return template.batchUpdate(insertStatement.getInsertStatementSQL(),
113                 BatchInsertUtility.createBatch(insertStatement.getRecords()));
114     }
115 
116     public <T> int insertMultiple(Buildable<MultiRowInsertModel<T>> insertStatement) {
117         return insertMultiple(insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
118     }
119 
120     public <T> int insertMultiple(MultiRowInsertStatementProvider<T> insertStatement) {
121         return template.update(insertStatement.getInsertStatement(),
122                 new BeanPropertySqlParameterSource(insertStatement));
123     }
124 
125     public <T> int insertMultiple(Buildable<MultiRowInsertModel<T>> insertStatement, KeyHolder keyHolder) {
126         return insertMultiple(insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER), keyHolder);
127     }
128 
129     public <T> int insertMultiple(MultiRowInsertStatementProvider<T> insertStatement, KeyHolder keyHolder) {
130         return template.update(insertStatement.getInsertStatement(),
131                 new BeanPropertySqlParameterSource(insertStatement), keyHolder);
132     }
133 
134     public <T> List<T> selectList(Buildable<SelectModel> selectStatement, RowMapper<T> rowMapper) {
135         return selectList(selectStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER), rowMapper);
136     }
137 
138     public <T> List<T> selectList(SelectStatementProvider selectStatement, RowMapper<T> rowMapper) {
139         return template.query(selectStatement.getSelectStatement(), selectStatement.getParameters(), rowMapper);
140     }
141 
142     public <T> Optional<T> selectOne(Buildable<SelectModel> selectStatement, RowMapper<T> rowMapper) {
143         return selectOne(selectStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER), rowMapper);
144     }
145 
146     public <T> Optional<T> selectOne(SelectStatementProvider selectStatement, RowMapper<T> rowMapper) {
147         T result;
148         try {
149             result = template.queryForObject(selectStatement.getSelectStatement(), selectStatement.getParameters(),
150                             rowMapper);
151         } catch (EmptyResultDataAccessException e) {
152             result = null;
153         }
154 
155         return Optional.ofNullable(result);
156     }
157 
158     public int update(Buildable<UpdateModel> updateStatement) {
159         return update(updateStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER));
160     }
161 
162     public int update(UpdateStatementProvider updateStatement) {
163         return template.update(updateStatement.getUpdateStatement(), updateStatement.getParameters());
164     }
165 }