|
| 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 | +} |
0 commit comments