From f895e0625a47130c40bd59bf59c6bfd3573ddde0 Mon Sep 17 00:00:00 2001 From: Jeff Butler Date: Mon, 19 Aug 2019 07:57:40 -0400 Subject: [PATCH 1/2] Add utility method to build column lists --- .../java/org/mybatis/dynamic/sql/BasicColumn.java | 13 ++++++++++++- src/site/markdown/docs/mybatis3.md | 2 +- .../java/examples/animal/data/AnimalDataTest.java | 4 ++-- .../java/examples/schema_supplier/UserMapper.java | 2 +- src/test/java/examples/simple/PersonMapper.java | 2 +- .../examples/simple/PersonWithAddressMapper.java | 4 ++-- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/mybatis/dynamic/sql/BasicColumn.java b/src/main/java/org/mybatis/dynamic/sql/BasicColumn.java index 0ee4e838f..80b8e0d70 100644 --- a/src/main/java/org/mybatis/dynamic/sql/BasicColumn.java +++ b/src/main/java/org/mybatis/dynamic/sql/BasicColumn.java @@ -1,5 +1,5 @@ /** - * Copyright 2016-2017 the original author or authors. + * Copyright 2016-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,4 +66,15 @@ default String renderWithTableAndColumnAlias(TableAliasCalculator tableAliasCalc return alias().map(a -> nameAndTableAlias + " as " + a) //$NON-NLS-1$ .orElse(nameAndTableAlias); } + + /** + * Utility method to make it easier to build column lists for methods that require an + * array rather than the varargs method. + * + * @param columns list of BasicColumn + * @return an array of BasicColumn + */ + static BasicColumn[] columnList(BasicColumn...columns) { + return columns; + } } diff --git a/src/site/markdown/docs/mybatis3.md b/src/site/markdown/docs/mybatis3.md index b32705479..1309232c7 100644 --- a/src/site/markdown/docs/mybatis3.md +++ b/src/site/markdown/docs/mybatis3.md @@ -148,7 +148,7 @@ We also envision creating a static field for a reusable list of columns for a se ```java static BasicColumn[] selectList = - new BasicColumn[] {id.as("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId}; + BasicColumn.columnList(id.as("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId); ``` The `selectOne` method can be used to implement a generalized select one method: diff --git a/src/test/java/examples/animal/data/AnimalDataTest.java b/src/test/java/examples/animal/data/AnimalDataTest.java index 997d65620..4245de788 100644 --- a/src/test/java/examples/animal/data/AnimalDataTest.java +++ b/src/test/java/examples/animal/data/AnimalDataTest.java @@ -1786,7 +1786,7 @@ public void testUpdateWithAddAndSubtract() { assertThat(rows).isEqualTo(1); AnimalData record = MyBatis3Utils.selectOne(mapper::selectOne, - new BasicColumn[] {id, bodyWeight, brainWeight}, + BasicColumn.columnList(id, bodyWeight, brainWeight), animalData, c -> c.where(id, isEqualTo(1)) ); @@ -1819,7 +1819,7 @@ public void testUpdateWithMultiplyAndDivide() { assertThat(rows).isEqualTo(1); AnimalData record = MyBatis3Utils.selectOne(mapper::selectOne, - new BasicColumn[] {id, bodyWeight, brainWeight}, + BasicColumn.columnList(id, bodyWeight, brainWeight), animalData, c -> c.where(id, isEqualTo(1)) ); diff --git a/src/test/java/examples/schema_supplier/UserMapper.java b/src/test/java/examples/schema_supplier/UserMapper.java index a7c6d3764..9d0be4088 100644 --- a/src/test/java/examples/schema_supplier/UserMapper.java +++ b/src/test/java/examples/schema_supplier/UserMapper.java @@ -46,7 +46,7 @@ public interface UserMapper { List selectMany(SelectStatementProvider selectStatement); default List select(MyBatis3SelectCompleter completer) { - return MyBatis3Utils.selectList(this::selectMany, new BasicColumn[] {id, name}, user, completer); + return MyBatis3Utils.selectList(this::selectMany, BasicColumn.columnList(id, name), user, completer); } default int insert(User record) { diff --git a/src/test/java/examples/simple/PersonMapper.java b/src/test/java/examples/simple/PersonMapper.java index 81664fe5a..f094c918b 100644 --- a/src/test/java/examples/simple/PersonMapper.java +++ b/src/test/java/examples/simple/PersonMapper.java @@ -84,7 +84,7 @@ public interface PersonMapper { Optional selectOne(SelectStatementProvider selectStatement); static BasicColumn[] selectList = - new BasicColumn[] {id.as("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId}; + BasicColumn.columnList(id.as("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId); @UpdateProvider(type=SqlProviderAdapter.class, method="update") int update(UpdateStatementProvider updateStatement); diff --git a/src/test/java/examples/simple/PersonWithAddressMapper.java b/src/test/java/examples/simple/PersonWithAddressMapper.java index 4627e16f1..795698e54 100644 --- a/src/test/java/examples/simple/PersonWithAddressMapper.java +++ b/src/test/java/examples/simple/PersonWithAddressMapper.java @@ -71,8 +71,8 @@ public interface PersonWithAddressMapper { Optional selectOne(SelectStatementProvider selectStatement); static BasicColumn[] selectList = - new BasicColumn[] {id.as("A_ID"), firstName, lastName, birthDate, employed, occupation, address.id, - address.streetAddress, address.city, address.state}; + BasicColumn.columnList(id.as("A_ID"), firstName, lastName, birthDate, employed, occupation, address.id, + address.streetAddress, address.city, address.state); default Optional selectOne(MyBatis3SelectCompleter completer) { CompletableQuery start = SqlBuilder.select(selectList).from(person) From 9c418a9ab5daacecef84110baceaaf84b10961e8 Mon Sep 17 00:00:00 2001 From: Jeff Butler Date: Mon, 19 Aug 2019 08:21:16 -0400 Subject: [PATCH 2/2] Repackage and rename the completer functions --- .../mybatis/dynamic/sql/delete/DeleteDSL.java | 3 +- .../DeleteDSLCompleter.java} | 25 +++++++------- .../delete/MyBatis3DeleteModelAdapter.java | 3 +- .../dynamic/sql/select/CompletableQuery.java | 3 +- .../select/MyBatis3SelectModelAdapter.java | 3 +- .../SelectDSLCompleter.java} | 33 +++++++++++-------- .../update/MyBatis3UpdateModelAdapter.java | 3 +- .../mybatis/dynamic/sql/update/UpdateDSL.java | 3 +- .../UpdateDSLCompleter.java} | 13 ++++---- .../sql/util/mybatis3/MyBatis3Utils.java | 23 +++++++------ src/site/markdown/docs/mybatis3.md | 28 ++++++++-------- .../schema_supplier/SchemaSupplierTest.java | 8 ++--- .../examples/schema_supplier/UserMapper.java | 4 +-- .../java/examples/simple/PersonMapper.java | 18 +++++----- .../examples/simple/PersonMapperTest.java | 14 ++++---- .../simple/PersonWithAddressMapper.java | 6 ++-- 16 files changed, 95 insertions(+), 95 deletions(-) rename src/main/java/org/mybatis/dynamic/sql/{util/mybatis3/MyBatis3DeleteCompleter.java => delete/DeleteDSLCompleter.java} (71%) rename src/main/java/org/mybatis/dynamic/sql/{util/mybatis3/MyBatis3SelectCompleter.java => select/SelectDSLCompleter.java} (65%) rename src/main/java/org/mybatis/dynamic/sql/{util/mybatis3/MyBatis3UpdateCompleter.java => update/UpdateDSLCompleter.java} (87%) diff --git a/src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java b/src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java index f9e45e8f3..154395c9e 100644 --- a/src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java +++ b/src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java @@ -25,7 +25,6 @@ import org.mybatis.dynamic.sql.VisitableCondition; import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider; import org.mybatis.dynamic.sql.util.Buildable; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteCompleter; import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; import org.mybatis.dynamic.sql.where.AbstractWhereDSL; @@ -81,7 +80,7 @@ public static DeleteDSL deleteFrom(SqlTable table) { /** * Delete record(s) by executing a MyBatis3 mapper method. * - * @deprecated in favor of {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, MyBatis3DeleteCompleter)}. + * @deprecated in favor of {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, DeleteDSLCompleter)}. * This method will be removed without direct replacement in a future version * @param return value from a delete method - typically Integer * @param mapperMethod MyBatis3 mapper method that performs the delete diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteCompleter.java b/src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSLCompleter.java similarity index 71% rename from src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteCompleter.java rename to src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSLCompleter.java index 620352b2e..6725a7fa6 100644 --- a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteCompleter.java +++ b/src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSLCompleter.java @@ -13,23 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mybatis.dynamic.sql.util.mybatis3; +package org.mybatis.dynamic.sql.delete; import java.util.function.Function; import java.util.function.ToIntFunction; import org.mybatis.dynamic.sql.SqlTable; -import org.mybatis.dynamic.sql.delete.DeleteDSL; -import org.mybatis.dynamic.sql.delete.DeleteModel; import org.mybatis.dynamic.sql.util.Buildable; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; /** * Represents a function that can be used to create a general delete method in the style * of MyBatis Generator. When using this function, you can create a method that does not require a user to * call the build() and render() methods - making client code look a bit cleaner. * - *

This function is intended to be used in conjunction with the utility function - * {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, MyBatis3DeleteCompleter)} + *

This function is intended to be used in conjunction with a utility method like + * {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, DeleteDSLCompleter)} * *

For example, you can create mapper interface methods like this: * @@ -37,7 +36,7 @@ * @DeleteProvider(type=SqlProviderAdapter.class, method="delete") * int delete(DeleteStatementProvider deleteStatement); * - * default int delete(MyBatis3DeleteCompleter completer) { + * default int delete(DeleteDSLCompleter completer) { * return MyBatis3Utils.deleteFrom(this::delete, person, completer); * } * @@ -52,27 +51,27 @@ *

You can implement a "delete all" with the following code: * *

- * int rows = mapper.delete(q -> q);
+ * int rows = mapper.delete(c -> c);
  * 
* *

Or * *

- * long rows = mapper.delete(MyBatis3DeleteCompleter.allRows());
+ * long rows = mapper.delete(DeleteDSLCompleter.allRows());
  * 
* @author Jeff Butler */ @FunctionalInterface -public interface MyBatis3DeleteCompleter extends +public interface DeleteDSLCompleter extends Function, Buildable> { /** - * Returns a helper that can be used to delete every row in a table. + * Returns a completer that can be used to delete every row in a table. * - * @return the helper that will delete every row in a table + * @return the completer that will delete every row in a table */ - static MyBatis3DeleteCompleter allRows() { - return h -> h; + static DeleteDSLCompleter allRows() { + return c -> c; } } diff --git a/src/main/java/org/mybatis/dynamic/sql/delete/MyBatis3DeleteModelAdapter.java b/src/main/java/org/mybatis/dynamic/sql/delete/MyBatis3DeleteModelAdapter.java index 6353bb7af..86fc913b6 100644 --- a/src/main/java/org/mybatis/dynamic/sql/delete/MyBatis3DeleteModelAdapter.java +++ b/src/main/java/org/mybatis/dynamic/sql/delete/MyBatis3DeleteModelAdapter.java @@ -20,12 +20,11 @@ import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider; import org.mybatis.dynamic.sql.render.RenderingStrategy; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteCompleter; /** * This adapter will render the underlying delete model for MyBatis3, and then call a MyBatis mapper method. * - * @deprecated in favor of {@link MyBatis3DeleteCompleter}. This class will be removed without replacement in a + * @deprecated in favor of {@link DeleteDSLCompleter}. This class will be removed without replacement in a * future version * * @author Jeff Butler diff --git a/src/main/java/org/mybatis/dynamic/sql/select/CompletableQuery.java b/src/main/java/org/mybatis/dynamic/sql/select/CompletableQuery.java index e58efb5e5..c0c80eed9 100644 --- a/src/main/java/org/mybatis/dynamic/sql/select/CompletableQuery.java +++ b/src/main/java/org/mybatis/dynamic/sql/select/CompletableQuery.java @@ -21,11 +21,10 @@ import org.mybatis.dynamic.sql.SqlCriterion; import org.mybatis.dynamic.sql.VisitableCondition; import org.mybatis.dynamic.sql.util.Buildable; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectCompleter; /** * This interface describes operations allowed for a select statement after the from and join clauses. This is - * primarily to support {@link MyBatis3SelectCompleter}. + * primarily to support {@link SelectDSLCompleter}. * * @author Jeff Butler * diff --git a/src/main/java/org/mybatis/dynamic/sql/select/MyBatis3SelectModelAdapter.java b/src/main/java/org/mybatis/dynamic/sql/select/MyBatis3SelectModelAdapter.java index 3e7e5b65a..baf72eadb 100644 --- a/src/main/java/org/mybatis/dynamic/sql/select/MyBatis3SelectModelAdapter.java +++ b/src/main/java/org/mybatis/dynamic/sql/select/MyBatis3SelectModelAdapter.java @@ -20,12 +20,11 @@ import org.mybatis.dynamic.sql.render.RenderingStrategy; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectCompleter; /** * This adapter will render the underlying select model for MyBatis3, and then call a MyBatis mapper method. * - * @deprecated in favor is {@link MyBatis3SelectCompleter}. This class will be removed without direct replacement + * @deprecated in favor is {@link SelectDSLCompleter}. This class will be removed without direct replacement * in a future version * * @author Jeff Butler diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectCompleter.java b/src/main/java/org/mybatis/dynamic/sql/select/SelectDSLCompleter.java similarity index 65% rename from src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectCompleter.java rename to src/main/java/org/mybatis/dynamic/sql/select/SelectDSLCompleter.java index ca59b0f96..14c57efbe 100644 --- a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectCompleter.java +++ b/src/main/java/org/mybatis/dynamic/sql/select/SelectDSLCompleter.java @@ -13,22 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mybatis.dynamic.sql.util.mybatis3; +package org.mybatis.dynamic.sql.select; import java.util.function.Function; import org.mybatis.dynamic.sql.SortSpecification; -import org.mybatis.dynamic.sql.select.CompletableQuery; -import org.mybatis.dynamic.sql.select.SelectModel; import org.mybatis.dynamic.sql.util.Buildable; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; /** * Represents a function that can be used to create a general count method in the style * of MyBatis Generator. When using this function, you can create a method that does not require a user to * call the build() and render() methods - making client code look a bit cleaner. * - *

This function is intended to by used in conjunction with several utility methods in - * {@link MyBatis3Utils} + *

This function is intended to by used in conjunction with utility methods like the select and count + * methods in {@link MyBatis3Utils} * *

For example, you can create mapper interface methods like this: * @@ -36,7 +35,7 @@ * @SelectProvider(type=SqlProviderAdapter.class, method="select") * long count(SelectStatementProvider selectStatement); * - * default long count(MyBatis3SelectCompleter completer) { + * default long count(SelectDSLCompleter completer) { return MyBatis3Utils.count(this::count, person, completer); * } * @@ -57,25 +56,31 @@ *

Or * *

- * long rows = mapper.count(MyBatis3CountHelper.allRows());
+ * long rows = mapper.count(SelectDSLCompleter.allRows());
  * 
* * @author Jeff Butler */ @FunctionalInterface -public interface MyBatis3SelectCompleter extends +public interface SelectDSLCompleter extends Function, Buildable> { /** - * Returns a helper that can be used to count every row in a table. + * Returns a completer that can be used to select every row in a table. * - * @return the helper that will count every row in a table + * @return the completer that will select every row in a table */ - static MyBatis3SelectCompleter allRows() { - return h -> h; + static SelectDSLCompleter allRows() { + return c -> c; } - static MyBatis3SelectCompleter allRowsOrderedBy(SortSpecification...columns) { - return h -> h.orderBy(columns); + /** + * Returns a completer that can be used to select every row in a table with specified order. + * + * @param columns list of sort specifications for an order by clause + * @return the completer that will select every row in a table with specified order + */ + static SelectDSLCompleter allRowsOrderedBy(SortSpecification...columns) { + return c -> c.orderBy(columns); } } diff --git a/src/main/java/org/mybatis/dynamic/sql/update/MyBatis3UpdateModelAdapter.java b/src/main/java/org/mybatis/dynamic/sql/update/MyBatis3UpdateModelAdapter.java index 62c0068d3..e0b91b0ed 100644 --- a/src/main/java/org/mybatis/dynamic/sql/update/MyBatis3UpdateModelAdapter.java +++ b/src/main/java/org/mybatis/dynamic/sql/update/MyBatis3UpdateModelAdapter.java @@ -20,12 +20,11 @@ import org.mybatis.dynamic.sql.render.RenderingStrategy; import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateCompleter; /** * This adapter will render the underlying update model for MyBatis3, and then call a MyBatis mapper method. * - * @deprecated in favor of {@link MyBatis3UpdateCompleter}. This class will be removed without direct + * @deprecated in favor of {@link UpdateDSLCompleter}. This class will be removed without direct * replacement in a future version. * @author Jeff Butler * diff --git a/src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java b/src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java index 58016c09e..3746d143e 100644 --- a/src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java +++ b/src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java @@ -38,7 +38,6 @@ import org.mybatis.dynamic.sql.util.StringConstantMapping; import org.mybatis.dynamic.sql.util.UpdateMapping; import org.mybatis.dynamic.sql.util.ValueMapping; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateCompleter; import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; import org.mybatis.dynamic.sql.where.AbstractWhereDSL; @@ -100,7 +99,7 @@ public static UpdateDSL update(SqlTable table) { /** * Executes an update using a MyBatis3 mapper method. * - * @deprecated in favor of {@link MyBatis3Utils#update(ToIntFunction, SqlTable, MyBatis3UpdateCompleter)}. This + * @deprecated in favor of {@link MyBatis3Utils#update(ToIntFunction, SqlTable, UpdateDSLCompleter)}. This * method will be removed without direct replacement in a future version. * @param return value from an update method - typically Integer * @param mapperMethod MyBatis3 mapper method that performs the update diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateCompleter.java b/src/main/java/org/mybatis/dynamic/sql/update/UpdateDSLCompleter.java similarity index 87% rename from src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateCompleter.java rename to src/main/java/org/mybatis/dynamic/sql/update/UpdateDSLCompleter.java index 7d81c43bf..8c5996d0c 100644 --- a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateCompleter.java +++ b/src/main/java/org/mybatis/dynamic/sql/update/UpdateDSLCompleter.java @@ -13,23 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mybatis.dynamic.sql.util.mybatis3; +package org.mybatis.dynamic.sql.update; import java.util.function.Function; import java.util.function.ToIntFunction; import org.mybatis.dynamic.sql.SqlTable; -import org.mybatis.dynamic.sql.update.UpdateDSL; -import org.mybatis.dynamic.sql.update.UpdateModel; import org.mybatis.dynamic.sql.util.Buildable; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; /** * Represents a function that can be used to create a general update method in the style * of MyBatis Generator. When using this function, you can create a method that does not require a user to * call the build() and render() methods - making client code look a bit cleaner. * - *

This function is intended to be used in conjunction with the utility function - * {@link MyBatis3Utils#update(ToIntFunction, SqlTable, MyBatis3UpdateCompleter)} + *

This function is intended to be used in conjunction in the utility method like + * {@link MyBatis3Utils#update(ToIntFunction, SqlTable, UpdateDSLCompleter)} * *

For example, you can create mapper interface methods like this: * @@ -37,7 +36,7 @@ * @UpdateProvider(type=SqlProviderAdapter.class, method="update") * int update(UpdateStatementProvider updateStatement); * - * default int update(MyBatis3UpdateCompleter completer) { + * default int update(UpdateDSLCompleter completer) { return MyBatis3Utils.update(this::update, person, completer); * } * @@ -88,6 +87,6 @@ * @author Jeff Butler */ @FunctionalInterface -public interface MyBatis3UpdateCompleter extends +public interface UpdateDSLCompleter extends Function, Buildable> { } diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3Utils.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3Utils.java index 91af52226..f38140711 100644 --- a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3Utils.java +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3Utils.java @@ -26,6 +26,7 @@ import org.mybatis.dynamic.sql.SqlBuilder; import org.mybatis.dynamic.sql.SqlTable; import org.mybatis.dynamic.sql.delete.DeleteDSL; +import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter; import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider; import org.mybatis.dynamic.sql.insert.InsertDSL; import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL; @@ -35,9 +36,11 @@ import org.mybatis.dynamic.sql.select.CompletableQuery; import org.mybatis.dynamic.sql.select.QueryExpressionDSL; import org.mybatis.dynamic.sql.select.SelectDSL; +import org.mybatis.dynamic.sql.select.SelectDSLCompleter; import org.mybatis.dynamic.sql.select.SelectModel; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; import org.mybatis.dynamic.sql.update.UpdateDSL; +import org.mybatis.dynamic.sql.update.UpdateDSLCompleter; import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; /** @@ -50,17 +53,17 @@ public class MyBatis3Utils { private MyBatis3Utils() {} public static long count(ToLongFunction mapper, - SqlTable table, MyBatis3SelectCompleter completer) { + SqlTable table, SelectDSLCompleter completer) { return count(mapper, SelectDSL.select(SqlBuilder.count()).from(table), completer); } public static long count(ToLongFunction mapper, - QueryExpressionDSL start, MyBatis3SelectCompleter completer) { + QueryExpressionDSL start, SelectDSLCompleter completer) { return mapper.applyAsLong(completer.apply(start).build().render(RenderingStrategies.MYBATIS3)); } public static int deleteFrom(ToIntFunction mapper, - SqlTable table, MyBatis3DeleteCompleter completer) { + SqlTable table, DeleteDSLCompleter completer) { return mapper.applyAsInt( completer.apply(DeleteDSL.deleteFrom(table)) .build() @@ -80,38 +83,38 @@ public static int insertMultiple(ToIntFunction List selectDistinct(Function> mapper, - BasicColumn[] selectList, SqlTable table, MyBatis3SelectCompleter completer) { + BasicColumn[] selectList, SqlTable table, SelectDSLCompleter completer) { return selectDistinct(mapper, SelectDSL.selectDistinct(selectList).from(table), completer); } public static List selectDistinct(Function> mapper, - CompletableQuery start, MyBatis3SelectCompleter completer) { + CompletableQuery start, SelectDSLCompleter completer) { return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3)); } public static List selectList(Function> mapper, - BasicColumn[] selectList, SqlTable table, MyBatis3SelectCompleter completer) { + BasicColumn[] selectList, SqlTable table, SelectDSLCompleter completer) { return selectList(mapper, SelectDSL.select(selectList).from(table), completer); } public static List selectList(Function> mapper, - CompletableQuery start, MyBatis3SelectCompleter completer) { + CompletableQuery start, SelectDSLCompleter completer) { return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3)); } public static R selectOne(Function mapper, - BasicColumn[] selectList, SqlTable table, MyBatis3SelectCompleter completer) { + BasicColumn[] selectList, SqlTable table, SelectDSLCompleter completer) { return selectOne(mapper, SelectDSL.select(selectList).from(table), completer); } public static R selectOne(Function mapper, CompletableQuery start, - MyBatis3SelectCompleter completer) { + SelectDSLCompleter completer) { return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3)); } public static int update(ToIntFunction mapper, - SqlTable table, MyBatis3UpdateCompleter completer) { + SqlTable table, UpdateDSLCompleter completer) { return mapper.applyAsInt( completer.apply(UpdateDSL.update(table)) .build() diff --git a/src/site/markdown/docs/mybatis3.md b/src/site/markdown/docs/mybatis3.md index 1309232c7..2363ec41c 100644 --- a/src/site/markdown/docs/mybatis3.md +++ b/src/site/markdown/docs/mybatis3.md @@ -19,12 +19,12 @@ long count(SelectStatementProvider selectStatement); This is a standard method for MyBatis Dynamic SQL that executes a query and returns a `long`. The second method will reuse this method and supply everything needed to build the select statement except the where clause: ```java -default long count(MyBatis3SelectCompleter completer) { +default long count(SelectDSLCompleter completer) { return MyBatis3Utils.count(this::count, person, completer); } ``` -This method shows the use of `MyBatis3SelectCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. Clients can use the method as follows: +This method shows the use of `SelectDSLCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. Clients can use the method as follows: ```java long rows = mapper.count(c -> @@ -34,7 +34,7 @@ long rows = mapper.count(c -> There is a utility method that can be used to count all rows in a table: ```java -long rows = mapper.count(MyBatis3SelectCompleter.allRows()); +long rows = mapper.count(SelectDSLCompleter.allRows()); ``` ## Delete Method Support @@ -51,12 +51,12 @@ int delete(DeleteStatementProvider deleteStatement); This is a standard method for MyBatis Dynamic SQL that executes a delete and returns an `int` - the number of rows deleted. The second method will reuse this method and supply everything needed to build the delete statement except the where clause: ```java -default int delete(MyBatis3DeleteCompleter completer) { +default int delete(DeleteDSLCompleter completer) { return MyBatis3Utils.deleteFrom(this::delete, person, completer); } ``` -This method shows the use of `MyBatis3DeleteCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. Clients can use the method as follows: +This method shows the use of `DeleteDSLCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. Clients can use the method as follows: ```java int rows = mapper.delete(c -> @@ -66,7 +66,7 @@ int rows = mapper.delete(c -> There is a utility method that can be used to delete all rows in a table: ```java -int rows = mapper.delete(MyBatis3DeleteCompleter.allRows()); +int rows = mapper.delete(DeleteDSLCompleter.allRows()); ``` ## Insert Method Support @@ -154,12 +154,12 @@ static BasicColumn[] selectList = The `selectOne` method can be used to implement a generalized select one method: ```java -default Optional selectOne(MyBatis3SelectCompleter completer) { +default Optional selectOne(SelectDSLCompleter completer) { return MyBatis3Utils.selectOne(this::selectOne, selectList, person, completer); } ``` -This method shows the use of `MyBatis3SelectCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. +This method shows the use of `SelectDSLCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. The general `selectOne` method can be used to implement a `selectByPrimaryKey` method: @@ -174,11 +174,11 @@ default Optional selectByPrimaryKey(Integer id_) { The `selectMany` method can be used to implement generalized select methods where a user can specify a where clause and/or an order by clause. Typically we recommend two of these methods - for select, and select distinct: ```java -default List select(MyBatis3SelectCompleter completer) { +default List select(SelectDSLCompleter completer) { return MyBatis3Utils.selectList(this::selectMany, selectList, person, completer); } -default List selectDistinct(MyBatis3SelectCompleter completer) { +default List selectDistinct(SelectDSLCompleter completer) { return MyBatis3Utils.selectDistinct(this::selectMany, selectList, person, completer); } ``` @@ -197,14 +197,14 @@ There are utility methods that will select all rows in a table: ```java List rows = - mapper.selectByExample(MyBatis3SelectCompleter.allRows()); + mapper.selectByExample(SelectDSLCompleter.allRows()); ``` The following query will select all rows in a specified order: ```java List rows = - mapper.selectByExample(MyBatis3SelectCompleter.allRowsOrderedBy(lastName, firstName)); + mapper.selectByExample(SelectDSLCompleter.allRowsOrderedBy(lastName, firstName)); ``` ## Update Method Support @@ -221,12 +221,12 @@ int update(UpdateStatementProvider updateStatement); This is a standard method for MyBatis Dynamic SQL that executes a query and returns an `int` - the number of rows updated. The second method will reuse this method and supply everything needed to build the update statement except the values and the where clause: ```java -default int update(MyBatis3UpdateCompleter completer) { +default int update(UpdateDSLCompleter completer) { return MyBatis3Utils.update(this::update, person, completer); } ``` -This method shows the use of `MyBatis3UpdateCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply values and a where clause. Clients can use the method as follows: +This method shows the use of `UpdateDSLCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply values and a where clause. Clients can use the method as follows: ```java int rows = mapper.update(c -> diff --git a/src/test/java/examples/schema_supplier/SchemaSupplierTest.java b/src/test/java/examples/schema_supplier/SchemaSupplierTest.java index 882df4d7d..85e094726 100644 --- a/src/test/java/examples/schema_supplier/SchemaSupplierTest.java +++ b/src/test/java/examples/schema_supplier/SchemaSupplierTest.java @@ -35,7 +35,7 @@ import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectCompleter; +import org.mybatis.dynamic.sql.select.SelectDSLCompleter; public class SchemaSupplierTest { @@ -89,7 +89,7 @@ public void testSchemaProperty() { insertFlintstones(mapper); - List records = mapper.select(MyBatis3SelectCompleter.allRows()); + List records = mapper.select(SelectDSLCompleter.allRows()); assertThat(records.size()).isEqualTo(2); } } @@ -102,14 +102,14 @@ public void testSchemaSwitchingProperty() { System.setProperty(SchemaSupplier.schema_property, "schema1"); insertFlintstones(mapper); - List records = mapper.select(MyBatis3SelectCompleter.allRows()); + List records = mapper.select(SelectDSLCompleter.allRows()); assertThat(records.size()).isEqualTo(2); System.setProperty(SchemaSupplier.schema_property, "schema2"); insertRubbles(mapper); - records = mapper.select(MyBatis3SelectCompleter.allRows()); + records = mapper.select(SelectDSLCompleter.allRows()); assertThat(records.size()).isEqualTo(3); } } diff --git a/src/test/java/examples/schema_supplier/UserMapper.java b/src/test/java/examples/schema_supplier/UserMapper.java index 9d0be4088..46b43fb60 100644 --- a/src/test/java/examples/schema_supplier/UserMapper.java +++ b/src/test/java/examples/schema_supplier/UserMapper.java @@ -28,9 +28,9 @@ import org.mybatis.dynamic.sql.SqlBuilder; import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider; import org.mybatis.dynamic.sql.render.RenderingStrategies; +import org.mybatis.dynamic.sql.select.SelectDSLCompleter; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; import org.mybatis.dynamic.sql.util.SqlProviderAdapter; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectCompleter; import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; public interface UserMapper { @@ -45,7 +45,7 @@ public interface UserMapper { }) List selectMany(SelectStatementProvider selectStatement); - default List select(MyBatis3SelectCompleter completer) { + default List select(SelectDSLCompleter completer) { return MyBatis3Utils.selectList(this::selectMany, BasicColumn.columnList(id, name), user, completer); } diff --git a/src/test/java/examples/simple/PersonMapper.java b/src/test/java/examples/simple/PersonMapper.java index f094c918b..ad66c8081 100644 --- a/src/test/java/examples/simple/PersonMapper.java +++ b/src/test/java/examples/simple/PersonMapper.java @@ -33,17 +33,17 @@ import org.apache.ibatis.annotations.UpdateProvider; import org.apache.ibatis.type.JdbcType; import org.mybatis.dynamic.sql.BasicColumn; +import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter; import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider; import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider; import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider; +import org.mybatis.dynamic.sql.select.SelectDSLCompleter; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; +import org.mybatis.dynamic.sql.update.UpdateDSLCompleter; import org.mybatis.dynamic.sql.update.UpdateDSL; import org.mybatis.dynamic.sql.update.UpdateModel; import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; import org.mybatis.dynamic.sql.util.SqlProviderAdapter; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteCompleter; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectCompleter; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateCompleter; import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; /** @@ -89,11 +89,11 @@ public interface PersonMapper { @UpdateProvider(type=SqlProviderAdapter.class, method="update") int update(UpdateStatementProvider updateStatement); - default long count(MyBatis3SelectCompleter completer) { + default long count(SelectDSLCompleter completer) { return MyBatis3Utils.count(this::count, person, completer); } - default int delete(MyBatis3DeleteCompleter completer) { + default int delete(DeleteDSLCompleter completer) { return MyBatis3Utils.deleteFrom(this::delete, person, completer); } @@ -143,15 +143,15 @@ default int insertSelective(PersonRecord record) { ); } - default Optional selectOne(MyBatis3SelectCompleter completer) { + default Optional selectOne(SelectDSLCompleter completer) { return MyBatis3Utils.selectOne(this::selectOne, selectList, person, completer); } - default List select(MyBatis3SelectCompleter completer) { + default List select(SelectDSLCompleter completer) { return MyBatis3Utils.selectList(this::selectMany, selectList, person, completer); } - default List selectDistinct(MyBatis3SelectCompleter completer) { + default List selectDistinct(SelectDSLCompleter completer) { return MyBatis3Utils.selectDistinct(this::selectMany, selectList, person, completer); } @@ -161,7 +161,7 @@ default Optional selectByPrimaryKey(Integer id_) { ); } - default int update(MyBatis3UpdateCompleter completer) { + default int update(UpdateDSLCompleter completer) { return MyBatis3Utils.update(this::update, person, completer); } diff --git a/src/test/java/examples/simple/PersonMapperTest.java b/src/test/java/examples/simple/PersonMapperTest.java index 16603b45f..3ffecbd54 100644 --- a/src/test/java/examples/simple/PersonMapperTest.java +++ b/src/test/java/examples/simple/PersonMapperTest.java @@ -39,8 +39,8 @@ import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteCompleter; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectCompleter; +import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter; +import org.mybatis.dynamic.sql.select.SelectDSLCompleter; public class PersonMapperTest { @@ -85,7 +85,7 @@ public void testSelectAll() { try (SqlSession session = sqlSessionFactory.openSession()) { PersonMapper mapper = session.getMapper(PersonMapper.class); - List rows = mapper.select(MyBatis3SelectCompleter.allRows()); + List rows = mapper.select(SelectDSLCompleter.allRows()); assertThat(rows.size()).isEqualTo(6); assertThat(rows.get(0).getId()).isEqualTo(1); @@ -99,7 +99,7 @@ public void testSelectAllOrdered() { PersonMapper mapper = session.getMapper(PersonMapper.class); List rows = mapper - .select(MyBatis3SelectCompleter.allRowsOrderedBy(lastName.descending(), firstName.descending())); + .select(SelectDSLCompleter.allRowsOrderedBy(lastName.descending(), firstName.descending())); assertThat(rows.size()).isEqualTo(6); assertThat(rows.get(0).getId()).isEqualTo(5); @@ -177,7 +177,7 @@ public void testDelete() { public void testDeleteAll() { try (SqlSession session = sqlSessionFactory.openSession()) { PersonMapper mapper = session.getMapper(PersonMapper.class); - int rows = mapper.delete(MyBatis3DeleteCompleter.allRows()); + int rows = mapper.delete(DeleteDSLCompleter.allRows()); assertThat(rows).isEqualTo(6); } @@ -448,7 +448,7 @@ public void testCount() { public void testCountAll() { try (SqlSession session = sqlSessionFactory.openSession()) { PersonMapper mapper = session.getMapper(PersonMapper.class); - long rows = mapper.count(MyBatis3SelectCompleter.allRows()); + long rows = mapper.count(SelectDSLCompleter.allRows()); assertThat(rows).isEqualTo(6L); } @@ -487,7 +487,7 @@ public void testJoinAllRows() { try (SqlSession session = sqlSessionFactory.openSession()) { PersonWithAddressMapper mapper = session.getMapper(PersonWithAddressMapper.class); List records = mapper.select( - MyBatis3SelectCompleter.allRowsOrderedBy(id) + SelectDSLCompleter.allRowsOrderedBy(id) ); assertThat(records.size()).isEqualTo(6L); diff --git a/src/test/java/examples/simple/PersonWithAddressMapper.java b/src/test/java/examples/simple/PersonWithAddressMapper.java index 795698e54..7c9b21444 100644 --- a/src/test/java/examples/simple/PersonWithAddressMapper.java +++ b/src/test/java/examples/simple/PersonWithAddressMapper.java @@ -37,10 +37,10 @@ import org.mybatis.dynamic.sql.BasicColumn; import org.mybatis.dynamic.sql.SqlBuilder; import org.mybatis.dynamic.sql.select.CompletableQuery; +import org.mybatis.dynamic.sql.select.SelectDSLCompleter; import org.mybatis.dynamic.sql.select.SelectModel; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; import org.mybatis.dynamic.sql.util.SqlProviderAdapter; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectCompleter; import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; /** @@ -74,13 +74,13 @@ public interface PersonWithAddressMapper { BasicColumn.columnList(id.as("A_ID"), firstName, lastName, birthDate, employed, occupation, address.id, address.streetAddress, address.city, address.state); - default Optional selectOne(MyBatis3SelectCompleter completer) { + default Optional selectOne(SelectDSLCompleter completer) { CompletableQuery start = SqlBuilder.select(selectList).from(person) .fullJoin(address).on(person.addressId, equalTo(address.id)); return MyBatis3Utils.selectOne(this::selectOne, start, completer); } - default List select(MyBatis3SelectCompleter completer) { + default List select(SelectDSLCompleter completer) { CompletableQuery start = SqlBuilder.select(selectList).from(person) .fullJoin(address).on(person.addressId, equalTo(address.id)); return MyBatis3Utils.selectList(this::selectMany, start, completer);