|
| 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 examples.generated.always.mybatis; |
| 17 | + |
| 18 | +import static examples.generated.always.mybatis.GeneratedAlwaysDynamicSqlSupport.*; |
| 19 | +import static org.mybatis.dynamic.sql.SqlBuilder.*; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import java.util.Optional; |
| 23 | + |
| 24 | +import org.apache.ibatis.annotations.Insert; |
| 25 | +import org.apache.ibatis.annotations.InsertProvider; |
| 26 | +import org.apache.ibatis.annotations.Options; |
| 27 | +import org.apache.ibatis.annotations.Param; |
| 28 | +import org.apache.ibatis.annotations.Result; |
| 29 | +import org.apache.ibatis.annotations.ResultMap; |
| 30 | +import org.apache.ibatis.annotations.Results; |
| 31 | +import org.apache.ibatis.annotations.SelectProvider; |
| 32 | +import org.mybatis.dynamic.sql.BasicColumn; |
| 33 | +import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider; |
| 34 | +import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider; |
| 35 | +import org.mybatis.dynamic.sql.select.SelectDSLCompleter; |
| 36 | +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; |
| 37 | +import org.mybatis.dynamic.sql.update.UpdateDSL; |
| 38 | +import org.mybatis.dynamic.sql.update.UpdateDSLCompleter; |
| 39 | +import org.mybatis.dynamic.sql.update.UpdateModel; |
| 40 | +import org.mybatis.dynamic.sql.util.SqlProviderAdapter; |
| 41 | + |
| 42 | +import examples.generated.always.GeneratedAlwaysRecord; |
| 43 | +import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper; |
| 44 | +import org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper; |
| 45 | +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; |
| 46 | + |
| 47 | +public interface GeneratedAlwaysMapper extends CommonInsertMapper<GeneratedAlwaysRecord>, CommonUpdateMapper { |
| 48 | + @SelectProvider(type=SqlProviderAdapter.class, method="select") |
| 49 | + @Results(id="gaResults", value={ |
| 50 | + @Result(property="id", column="id", id=true), |
| 51 | + @Result(property="firstName", column="first_name"), |
| 52 | + @Result(property="lastName", column="last_name"), |
| 53 | + @Result(property="fullName", column="full_name") |
| 54 | + }) |
| 55 | + List<GeneratedAlwaysRecord> selectMany(SelectStatementProvider selectStatement); |
| 56 | + |
| 57 | + @SelectProvider(type=SqlProviderAdapter.class, method="select") |
| 58 | + @ResultMap("gaResults") |
| 59 | + Optional<GeneratedAlwaysRecord> selectOne(SelectStatementProvider selectStatement); |
| 60 | + |
| 61 | + @Override |
| 62 | + @InsertProvider(type=SqlProviderAdapter.class, method="insert") |
| 63 | + @Options(useGeneratedKeys=true, keyProperty="record.fullName") |
| 64 | + int insert(InsertStatementProvider<GeneratedAlwaysRecord> insertStatement); |
| 65 | + |
| 66 | + // This is kludgy. Currently MyBatis does not support nested lists in parameter objects |
| 67 | + // when returning generated keys. |
| 68 | + // So we need to do this silliness and decompose the multi row insert into its component parts |
| 69 | + // for the actual MyBatis call |
| 70 | + @Insert("${insertStatement}") |
| 71 | + @Options(useGeneratedKeys=true, keyProperty="records.fullName") |
| 72 | + int insertMultipleWithGeneratedKeys(@Param("insertStatement") String statement, @Param("records") List<GeneratedAlwaysRecord> records); |
| 73 | + |
| 74 | + default int insertMultipleWithGeneratedKeys(MultiRowInsertStatementProvider<GeneratedAlwaysRecord> multiInsert) { |
| 75 | + return insertMultipleWithGeneratedKeys(multiInsert.getInsertStatement(), multiInsert.getRecords()); |
| 76 | + } |
| 77 | + |
| 78 | + BasicColumn[] selectList = |
| 79 | + BasicColumn.columnList(id.as("A_ID"), firstName, lastName, fullName); |
| 80 | + |
| 81 | + default Optional<GeneratedAlwaysRecord> selectOne(SelectDSLCompleter completer) { |
| 82 | + return MyBatis3Utils.selectOne(this::selectOne, selectList, generatedAlways, completer); |
| 83 | + } |
| 84 | + |
| 85 | + default List<GeneratedAlwaysRecord> select(SelectDSLCompleter completer) { |
| 86 | + return MyBatis3Utils.selectList(this::selectMany, selectList, generatedAlways, completer); |
| 87 | + } |
| 88 | + |
| 89 | + default Optional<GeneratedAlwaysRecord> selectByPrimaryKey(Integer _id) { |
| 90 | + return selectOne(c -> c.where(id, isEqualTo(_id))); |
| 91 | + } |
| 92 | + |
| 93 | + default int insert(GeneratedAlwaysRecord record) { |
| 94 | + return MyBatis3Utils.insert(this::insert, record, generatedAlways, c -> |
| 95 | + c.map(id).toProperty("id") |
| 96 | + .map(firstName).toProperty("firstName") |
| 97 | + .map(lastName).toProperty("lastName") |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + default int insertSelective(GeneratedAlwaysRecord record) { |
| 102 | + return MyBatis3Utils.insert(this::insert, record, generatedAlways, c -> |
| 103 | + c.map(id).toPropertyWhenPresent("id", record::getId) |
| 104 | + .map(firstName).toPropertyWhenPresent("firstName", record::getFirstName) |
| 105 | + .map(lastName).toPropertyWhenPresent("lastName", record::getLastName) |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + default int update(UpdateDSLCompleter completer) { |
| 110 | + return MyBatis3Utils.update(this::update, generatedAlways, completer); |
| 111 | + } |
| 112 | + |
| 113 | + default int updateByPrimaryKey(GeneratedAlwaysRecord record) { |
| 114 | + return update(c -> |
| 115 | + c.set(firstName).equalTo(record::getFirstName) |
| 116 | + .set(lastName).equalTo(record::getLastName) |
| 117 | + .where(id, isEqualTo(record::getId)) |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + default int updateByPrimaryKeySelective(GeneratedAlwaysRecord record) { |
| 122 | + return update(c -> |
| 123 | + c.set(firstName).equalToWhenPresent(record::getFirstName) |
| 124 | + .set(lastName).equalToWhenPresent(record::getLastName) |
| 125 | + .where(id, isEqualTo(record::getId)) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + static UpdateDSL<UpdateModel> updateSelectiveColumns(GeneratedAlwaysRecord record, UpdateDSL<UpdateModel> dsl) { |
| 130 | + return dsl.set(id).equalToWhenPresent(record::getId) |
| 131 | + .set(firstName).equalToWhenPresent(record::getFirstName) |
| 132 | + .set(lastName).equalToWhenPresent(record::getLastName); |
| 133 | + } |
| 134 | +} |
0 commit comments