diff --git a/README.md b/README.md index 5dcad943a..597d224fe 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,7 @@ One capability is that very expressive dynamic queries can be generated. Here's ```java @Test public void testComplexCondition() { - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { + try(SqlSession sqlSession = sqlSessionFactory.openSession()) { AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight) @@ -72,8 +71,6 @@ One capability is that very expressive dynamic queries can be generated. Here's List animals = mapper.selectMany(selectStatement); assertThat(animals.size()).isEqualTo(4); - } finally { - sqlSession.close(); } } ``` diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3CountByExampleHelper.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3CountHelper.java similarity index 82% rename from src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3CountByExampleHelper.java rename to src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3CountHelper.java index bd96f142c..afee157c4 100644 --- a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3CountByExampleHelper.java +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3CountHelper.java @@ -22,7 +22,7 @@ import org.mybatis.dynamic.sql.util.Buildable; /** - * Represents a function that can be used to create a "CountByExample" method in the style + * 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().execute() methods - making client code look a bit cleaner. * @@ -32,7 +32,7 @@ * @SelectProvider(type=SqlProviderAdapter.class, method="select") * long count(SelectStatementProvider selectStatement); * - * default long countByExample(MyBatis3CountByExampleHelper helper) { + * default long count(MyBatis3CountHelper helper) { * return helper.apply(SelectDSL.selectWithMapper(this::count, SqlBuilder.count()) * .from(simpleTable)) * .build() @@ -43,26 +43,26 @@ *

And then call the simplified default method like this: * *

- * long rows = mapper.countByExample(q ->
+ * long rows = mapper.count(q ->
  *         q.where(occupation, isNull()));
  * 
* *

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

- * long rows = mapper.countByExample(q -> q);
+ * long rows = mapper.count(q -> q);
  * 
* *

Or * *

- * long rows = mapper.countByExample(MyBatis3CountByExampleHelper.allRows());
+ * long rows = mapper.count(MyBatis3CountHelper.allRows());
  * 
* * @author Jeff Butler */ @FunctionalInterface -public interface MyBatis3CountByExampleHelper extends +public interface MyBatis3CountHelper extends Function>, Buildable>> { /** @@ -70,7 +70,7 @@ public interface MyBatis3CountByExampleHelper extends * * @return the helper that will count every row in a table */ - static MyBatis3CountByExampleHelper allRows() { + static MyBatis3CountHelper allRows() { return h -> h; } } diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteByExampleHelper.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteHelper.java similarity index 75% rename from src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteByExampleHelper.java rename to src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteHelper.java index 9f58a2887..ee4fa21f6 100644 --- a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteByExampleHelper.java +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteHelper.java @@ -18,11 +18,10 @@ import java.util.function.Function; import org.mybatis.dynamic.sql.delete.DeleteDSL; -import org.mybatis.dynamic.sql.delete.MyBatis3DeleteModelAdapter; import org.mybatis.dynamic.sql.util.Buildable; /** - * Represents a function that can be used to create a "DeleteByExample" method in the style + * 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().execute() methods - making client code look a bit cleaner. * @@ -32,7 +31,7 @@ * @DeleteProvider(type=SqlProviderAdapter.class, method="delete") * int delete(DeleteStatementProvider deleteStatement); * - * default int deleteByExample(MyBatis3DeleteByExampleHelper helper) { + * default int delete(MyBatis3DeleteHelper helper) { * return helper.apply(DeleteDSL.deleteFromWithMapper(this::delete, simpleTable)) * .build() * .execute(); @@ -42,34 +41,34 @@ *

And then call the simplified default method like this: * *

- * int rows = mapper.deleteByExample(q ->
+ * int rows = mapper.delete(q ->
  *           q.where(occupation, isNull()));
  * 
* *

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

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

Or * *

- * long rows = mapper.deleteByExample(MyBatis3DeleteByExampleHelper.allRows());
+ * long rows = mapper.delete(MyBatis3DeleteHelper.allRows());
  * 
* @author Jeff Butler */ @FunctionalInterface -public interface MyBatis3DeleteByExampleHelper extends - Function>, Buildable>> { +public interface MyBatis3DeleteHelper extends + Function, Buildable> { /** * Returns a helper that can be used to delete every row in a table. * * @return the helper that will delete every row in a table */ - static MyBatis3DeleteByExampleHelper allRows() { + static MyBatis3DeleteHelper allRows() { return h -> h; } } diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteModelToIntAdapter.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteModelToIntAdapter.java new file mode 100644 index 000000000..c20690d92 --- /dev/null +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3DeleteModelToIntAdapter.java @@ -0,0 +1,59 @@ +/** + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.dynamic.sql.util.mybatis3; + +import java.util.Objects; +import java.util.function.ToIntFunction; + +import org.mybatis.dynamic.sql.delete.DeleteModel; +import org.mybatis.dynamic.sql.delete.MyBatis3DeleteModelAdapter; +import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider; +import org.mybatis.dynamic.sql.render.RenderingStrategy; + +/** + * This adapter will render the underlying delete model for MyBatis3, and then call a MyBatis mapper method. + * This version of the adapter is preferred over {@link MyBatis3DeleteModelAdapter} because it is not generic + * and does not force a box/unbox for mappers that always return a primitive int. + * + * @see MyBatis3Utils + * + * @author Jeff Butler + * + */ +public class MyBatis3DeleteModelToIntAdapter { + + private DeleteModel deleteModel; + private ToIntFunction mapperMethod; + + private MyBatis3DeleteModelToIntAdapter(DeleteModel deleteModel, + ToIntFunction mapperMethod) { + this.deleteModel = Objects.requireNonNull(deleteModel); + this.mapperMethod = Objects.requireNonNull(mapperMethod); + } + + public int execute() { + return mapperMethod.applyAsInt(deleteStatement()); + } + + private DeleteStatementProvider deleteStatement() { + return deleteModel.render(RenderingStrategy.MYBATIS3); + } + + public static MyBatis3DeleteModelToIntAdapter of(DeleteModel deleteModel, + ToIntFunction mapperMethod) { + return new MyBatis3DeleteModelToIntAdapter(deleteModel, mapperMethod); + } +} diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectByExampleHelper.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectListHelper.java similarity index 83% rename from src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectByExampleHelper.java rename to src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectListHelper.java index 1e99abe04..adc02ea7e 100644 --- a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectByExampleHelper.java +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectListHelper.java @@ -24,7 +24,7 @@ import org.mybatis.dynamic.sql.util.Buildable; /** - * Represents a function that can be used to create a "SelectByExample" method in the style + * Represents a function that can be used to create a general select 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().execute() methods - making client code look a bit cleaner. * @@ -42,7 +42,7 @@ * }) * List<SimpleRecord> selectMany(SelectStatementProvider selectStatement); * - * default List<SimpleRecord> selectByExample(MyBatis3SelectByExampleHelper<SimpleRecord> helper) { + * default List<SimpleRecord> select(MyBatis3SelectHelper<SimpleRecord> helper) { * return helper.apply(SelectDSL.selectWithMapper(this::selectMany, simpleTable.allColumns()) * .from(simpleTable)) * .build() @@ -53,7 +53,7 @@ *

And then call the simplified default method like this: * *

- * List<SimpleRecord> rows = mapper.selectByExample(q ->
+ * List<SimpleRecord> rows = mapper.select(q ->
  *     q.where(id, isEqualTo(1))
  *     .or(occupation, isNull()));
  * 
@@ -61,19 +61,19 @@ *

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

- * List<SimpleRecord> rows = mapper.selectByExample(q -> q);
+ * List<SimpleRecord> rows = mapper.select(q -> q);
  * 
* *

Or * *

- * List<SimpleRecord> rows = mapper.selectByExample(MyBatis3SelectByExampleHelper.allRows());
+ * List<SimpleRecord> rows = mapper.select(MyBatis3SelectHelper.allRows());
  * 
* * @author Jeff Butler */ @FunctionalInterface -public interface MyBatis3SelectByExampleHelper extends +public interface MyBatis3SelectListHelper extends Function>>, Buildable>>> { @@ -84,7 +84,7 @@ public interface MyBatis3SelectByExampleHelper extends * * @return the helper that will select every row in a table */ - static MyBatis3SelectByExampleHelper allRows() { + static MyBatis3SelectListHelper allRows() { return h -> h; } @@ -96,7 +96,7 @@ static MyBatis3SelectByExampleHelper allRows() { * * @return the helper that will select every row in a table in the specified order */ - static MyBatis3SelectByExampleHelper allRowsOrderdBy(SortSpecification...columns) { + static MyBatis3SelectListHelper allRowsOrderdBy(SortSpecification...columns) { return h -> h.orderBy(columns); } } diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectOneHelper.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectOneHelper.java new file mode 100644 index 000000000..17e57bf84 --- /dev/null +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3SelectOneHelper.java @@ -0,0 +1,58 @@ +/** + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.dynamic.sql.util.mybatis3; + +import java.util.Optional; +import java.util.function.Function; + +import org.mybatis.dynamic.sql.select.MyBatis3SelectModelAdapter; +import org.mybatis.dynamic.sql.select.QueryExpressionDSL; +import org.mybatis.dynamic.sql.util.Buildable; + +/** + * Represents a function that can be used to create a general select one 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().execute() methods - making client code look a bit cleaner. + * + *

For example, you can create mapper interface methods like this: + * + *

+ * @SelectProvider(type=SqlProviderAdapter.class, method="select")
+ * @ResultMap("SimpleTableResult")
+ * Optional<SimpleTableRecord> selectOne(SelectStatementProvider selectStatement);
+ *
+ * default Optional<SimpleTableRecord> selectOne(MyBatis3SelectOneHelper<SimpleTableRecord> helper) {
+ *     return helper.apply(SelectDSL.selectWithMapper(this::selectOne, simpleTable.allColumns())
+ *             .from(simpleTable))
+ *             .build()
+ *             .execute();
+ * }
+ * 
+ * + *

And then call the simplified default method like this: + * + *

+ * Optional<SimpleRecord> record = mapper.selectOne(q ->
+ *     q.where(id, isEqualTo(1)));
+ * 
+ * + * @author Jeff Butler + */ +@FunctionalInterface +public interface MyBatis3SelectOneHelper extends + Function>>, + Buildable>>> { +} diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateByExampleCompleter.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateByExampleCompleter.java deleted file mode 100644 index 21e75a5be..000000000 --- a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateByExampleCompleter.java +++ /dev/null @@ -1,86 +0,0 @@ -/** - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.mybatis.dynamic.sql.util.mybatis3; - -import java.util.Objects; -import java.util.function.ToIntFunction; - -import org.mybatis.dynamic.sql.SqlTable; -import org.mybatis.dynamic.sql.update.UpdateDSL; -import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; - -/** - * This class is used to complete an update by example method in the style of MyBatis generator. - * - * @author Jeff Butler - * - * @see MyBatis3UpdateByExampleHelper - * - * @param the type of record that will be updated - */ -public class MyBatis3UpdateByExampleCompleter { - private SqlTable table; - private MyBatis3UpdateByExampleHelper helper; - private ToIntFunction mapper; - private MyBatis3UpdateByExampleValueSetter valueSetter; - - private MyBatis3UpdateByExampleCompleter(MyBatis3UpdateByExampleCompleter.Builder builder) { - helper = Objects.requireNonNull(builder.helper); - mapper = Objects.requireNonNull(builder.mapper); - valueSetter = Objects.requireNonNull(builder.valueSetter); - table = Objects.requireNonNull(builder.table); - } - - public int usingRecord(T record) { - return valueSetter.andThen(helper) - .apply(record, UpdateDSL.updateWithMapper(p -> mapper.applyAsInt(p), table)) - .build() - .execute(); - } - - public static class Builder { - private SqlTable table; - private MyBatis3UpdateByExampleHelper helper; - private ToIntFunction mapper; - private MyBatis3UpdateByExampleValueSetter valueSetter; - - public MyBatis3UpdateByExampleCompleter.Builder withTable(SqlTable table) { - this.table = table; - return this; - } - - public MyBatis3UpdateByExampleCompleter.Builder withHelper(MyBatis3UpdateByExampleHelper helper) { - this.helper = helper; - return this; - } - - public MyBatis3UpdateByExampleCompleter.Builder withMapper( - ToIntFunction mapper) { - this.mapper = mapper; - return this; - } - - public MyBatis3UpdateByExampleCompleter.Builder withValueSetter( - MyBatis3UpdateByExampleValueSetter valueSetter) { - this.valueSetter = valueSetter; - return this; - } - - public MyBatis3UpdateByExampleCompleter build() { - return new MyBatis3UpdateByExampleCompleter<>(this); - } - } -} diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateByExampleHelper.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateByExampleHelper.java deleted file mode 100644 index 47432c621..000000000 --- a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateByExampleHelper.java +++ /dev/null @@ -1,91 +0,0 @@ -/** - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.mybatis.dynamic.sql.util.mybatis3; - -import java.util.function.Function; - -import org.mybatis.dynamic.sql.update.MyBatis3UpdateModelAdapter; -import org.mybatis.dynamic.sql.update.UpdateDSL; -import org.mybatis.dynamic.sql.util.Buildable; - -/** - * Represents a function that can be used to create an "UpdateByExample" 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().execute() methods - making client code look a bit cleaner. - * - *

For example, you can create mapper interface methods like this: - * - *

- * @UpdateProvider(type=SqlProviderAdapter.class, method="update")
- * int update(UpdateStatementProvider updateStatement);
- *   
- * default MyBatis3UpdateByExampleCompleter updateByExampleSelective(MyBatis3UpdateByExampleHelper helper) {
- *     return new MyBatis3UpdateByExampleCompleter.Builder<SimpleTableRecord>()
- *             .withHelper(helper)
- *             .withMapper(this::update)
- *             .withTable(simpleTable)
- *             .withValueSetter((record, dsl) ->
- *                 dsl.set(id).equalToWhenPresent(record::getId)
- *                 .set(firstName).equalToWhenPresent(record::getFirstName)
- *                 .set(lastName).equalToWhenPresent(record::getLastName)
- *                 .set(birthDate).equalToWhenPresent(record::getBirthDate)
- *                 .set(employed).equalToWhenPresent(record::getEmployed)
- *                 .set(occupation).equalToWhenPresent(record::getOccupation))
- *             .build();
- * }
- * 
- * - *

And then call the simplified default method like this: - * - *

- * int rows = mapper.updateByExampleSelective(q ->
- *                q.where(id, isEqualTo(100))
- *                .and(firstName, isEqualTo("Joe")))
- *                .usingRecord(record);
- * 
- * - *

You can implement an "update all" with the following code: - * - *

- * int rows = mapper.updateByExampleSelective(q -> q)
- *                .usingRecord(record);
- * 
- * - *

Or - * - *

- * int rows = mapper.updateByExampleSelective(MyBatis3UpdateByExampleHelper.allRows())
- *                .usingRecord(record);
- * 
- * - * @see MyBatis3UpdateByExampleCompleter - * @see MyBatis3UpdateByExampleValueSetter - * - * @author Jeff Butler - */ -@FunctionalInterface -public interface MyBatis3UpdateByExampleHelper extends - Function>, Buildable>> { - - /** - * Returns a helper that can be used to update every row in a table. - * - * @return the helper that will update every row in a table - */ - static MyBatis3UpdateByExampleHelper allRows() { - return h -> h; - } -} diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateByExampleValueSetter.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateByExampleValueSetter.java deleted file mode 100644 index 8d2240564..000000000 --- a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateByExampleValueSetter.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.mybatis.dynamic.sql.util.mybatis3; - -import java.util.function.BiFunction; - -import org.mybatis.dynamic.sql.update.MyBatis3UpdateModelAdapter; -import org.mybatis.dynamic.sql.update.UpdateDSL; - -/** - * Represents a function that can be used to set values in an "UpdateByExample" method in the style - * of MyBatis Generator. When using this function, you can create a method that will map record fields to - * tables columns to be updated in a common mapper, and then allow a user to set a where clause as needed. - * - * @author Jeff Butler - * - * @see MyBatis3UpdateByExampleHelper - * - * @param the type of record that will be updated - */ -@FunctionalInterface -public interface MyBatis3UpdateByExampleValueSetter extends - BiFunction>, UpdateDSL>> { -} diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateHelper.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateHelper.java new file mode 100644 index 000000000..bac535cb3 --- /dev/null +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateHelper.java @@ -0,0 +1,89 @@ +/** + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.dynamic.sql.util.mybatis3; + +import java.util.function.Function; + +import org.mybatis.dynamic.sql.update.UpdateDSL; +import org.mybatis.dynamic.sql.util.Buildable; + +/** + * 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().execute() methods - making client code look a bit cleaner. + * + *

For example, you can create mapper interface methods like this: + * + *

+ * @UpdateProvider(type=SqlProviderAdapter.class, method="update")
+ * int update(UpdateStatementProvider updateStatement);
+ *   
+ * default int update(MyBatis3UpdateHelper helper) {
+ *     return helper.apply(UpdateDSL.updateWithMapper(this::update, simpleTable))
+ *             .build()
+ *             .execute();
+ * }
+ * 
+ * + *

And then call the simplified default method like this: + * + *

+ * int rows = mapper.update(q ->
+ *                q.set(firstName).equalTo("Fred")
+ *                .where(id, isEqualTo(100))
+ *            );
+ * 
+ * + *

You can implement an "update all" simply by omitting a where clause: + * + *

+ * int rows = mapper.update(q ->
+ *                q.set(firstName).equalTo("Fred")
+ *            );
+ * 
+ * + *

You could also implement a helper method that would set fields based on values of a record. For example, + * the following method would set all fields of a record based on whether or not the values are null: + * + *

+ * static UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> setSelective(SimpleTableRecord record,
+ *         UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> dsl) {
+ *     return dsl.set(id).equalToWhenPresent(record::getId)
+ *             .set(firstName).equalToWhenPresent(record::getFirstName)
+ *             .set(lastName).equalToWhenPresent(record::getLastName)
+ *             .set(birthDate).equalToWhenPresent(record::getBirthDate)
+ *             .set(employed).equalToWhenPresent(record::getEmployed)
+ *             .set(occupation).equalToWhenPresent(record::getOccupation);
+ * }
+ * 
+ * + *

The helper method could be used like this: + * + *

+ * rows = mapper.update(dsl ->
+ *        SimpleTableAnnotatedMapperNewStyle.setSelective(record, dsl)
+ *        .where(id, isLessThan(100)));
+ * 
+ * + *

In this way, you could mimic the function of the old style "updateByExampleSelective" methods from + * MyBatis Generator. + * + * @author Jeff Butler + */ +@FunctionalInterface +public interface MyBatis3UpdateHelper extends + Function, Buildable> { +} diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateModelToIntAdapter.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateModelToIntAdapter.java new file mode 100644 index 000000000..f280b34c5 --- /dev/null +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3UpdateModelToIntAdapter.java @@ -0,0 +1,59 @@ +/** + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.dynamic.sql.util.mybatis3; + +import java.util.Objects; +import java.util.function.ToIntFunction; + +import org.mybatis.dynamic.sql.render.RenderingStrategy; +import org.mybatis.dynamic.sql.update.MyBatis3UpdateModelAdapter; +import org.mybatis.dynamic.sql.update.UpdateModel; +import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; + +/** + * This adapter will render the underlying update model for MyBatis3, and then call a MyBatis mapper method. + * This version of the adapter is preferred over {@link MyBatis3UpdateModelAdapter} because it is not generic + * and does not force a box/unbox for mappers that always return a primitive int. + * + * @see MyBatis3Utils + * + * @author Jeff Butler + * + */ +public class MyBatis3UpdateModelToIntAdapter { + + private UpdateModel updateModel; + private ToIntFunction mapperMethod; + + private MyBatis3UpdateModelToIntAdapter(UpdateModel updateModel, + ToIntFunction mapperMethod) { + this.updateModel = Objects.requireNonNull(updateModel); + this.mapperMethod = Objects.requireNonNull(mapperMethod); + } + + public int execute() { + return mapperMethod.applyAsInt(updateStatement()); + } + + private UpdateStatementProvider updateStatement() { + return updateModel.render(RenderingStrategy.MYBATIS3); + } + + public static MyBatis3UpdateModelToIntAdapter of(UpdateModel updateModel, + ToIntFunction mapperMethod) { + return new MyBatis3UpdateModelToIntAdapter(updateModel, mapperMethod); + } +} 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 new file mode 100644 index 000000000..199827e86 --- /dev/null +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3Utils.java @@ -0,0 +1,58 @@ +/** + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.dynamic.sql.util.mybatis3; + +import java.util.function.ToIntFunction; + +import org.mybatis.dynamic.sql.SqlTable; +import org.mybatis.dynamic.sql.delete.DeleteDSL; +import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider; +import org.mybatis.dynamic.sql.update.UpdateDSL; +import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; + +/** + * Utility functions for building MyBatis3 mappers. + * + * @author Jeff Butler + * + */ +public class MyBatis3Utils { + private MyBatis3Utils() {} + + /** + * Initiates a delete statement using the non-boxing adapter. + * + * @param mapper a MyBatis3 mapper delete method + * @param table the table to delete from + * @return a partially completed DeleteDSL + */ + public static DeleteDSL deleteFrom(ToIntFunction mapper, + SqlTable table) { + return DeleteDSL.deleteFrom(dm -> MyBatis3DeleteModelToIntAdapter.of(dm, mapper), table); + } + + /** + * Initiates an update statement using the non-boxing adapter. + * + * @param mapper a MyBatis3 mapper update method + * @param table the table to update + * @return a partially completed UpdateDSL + */ + public static UpdateDSL update(ToIntFunction mapper, + SqlTable table) { + return UpdateDSL.update(um -> MyBatis3UpdateModelToIntAdapter.of(um, mapper), table); + } +} diff --git a/src/site/markdown/docs/howItWorks.md b/src/site/markdown/docs/howItWorks.md index 385364663..218475db6 100644 --- a/src/site/markdown/docs/howItWorks.md +++ b/src/site/markdown/docs/howItWorks.md @@ -61,14 +61,11 @@ public interface Mapper { Using this mapper with MyBatis looks like this: ```java - SqlSession sqlSession = sqlSessionFactory.openSession(); - try { + try(SqlSession sqlSession = sqlSessionFactory.openSession()) { Mapper mapper = sqlSession.getMapper(Mapper.class); Parameter parameter = new Parameter(2); TableCode tableCode = mapper.getTableCode(parameter); assertThat(tableCode.getId()).isEqualTo(2); - } finally { - sqlSession.close(); } ``` diff --git a/src/site/markdown/docs/insert.md b/src/site/markdown/docs/insert.md index ece85cd44..ca9f5fbba 100644 --- a/src/site/markdown/docs/insert.md +++ b/src/site/markdown/docs/insert.md @@ -187,8 +187,7 @@ A batch insert is a collection of statements that can be used to execute a JDBC ```java ... - SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH); - try { + try(SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) { SimpleTableMapper mapper = session.getMapper(SimpleTableMapper.class); List records = getRecordsToInsert(); // not shown @@ -206,8 +205,6 @@ A batch insert is a collection of statements that can be used to execute a JDBC batchInsert.insertStatements().stream().forEach(mapper::insert); session.commit(); - } finally { - session.close(); } ... ``` diff --git a/src/site/markdown/docs/mybatis3.md b/src/site/markdown/docs/mybatis3.md index 63a63559a..565792422 100644 --- a/src/site/markdown/docs/mybatis3.md +++ b/src/site/markdown/docs/mybatis3.md @@ -1,65 +1,231 @@ # Specialized Support for MyBatis3 -Most of the examples shown on this site are for usage with MyBatis3 - even though the library does support other SQL runtimes like Spring JDBC templates. But the library does have some additional specialized support for MyBatis3 beyond what is shown in the other examples. +Most of the examples shown on this site are for usage with MyBatis3 - even though the library does support other SQL runtimes like Spring JDBC templates. In addition to the examples shown elsewhere, the library has additional specialized support for MyBatis3 beyond what is shown in the other examples. This support mainly exists to support MyBatis Generator and the code generated by that tool. Even without MyBatis Generator, some of the techniques shown on this page may prove useful. -This support is added to the DELETE, SELECT, and UPDATE statement generators and enables the creating of reusable "by example" methods as delivered in MyBatis Generator. These methods can provide some boilerplate code for the setup of the statement (a column list and table name for example), and allow the user to specify a where clause. +This support is added to the DELETE, SELECT, and UPDATE statement generators and enables the creating of reusable methods as delivered in MyBatis Generator. These methods can provide some boilerplate code for the setup of the statement (a column list and table name for example), and allow the user to specify a where clause. -With version 1.1.3, specialized interfaces were added that can further simplify client code. +With version 1.1.3, specialized interfaces were added that can further simplify client code. This support enables the creation of methods that have similar functionality to some of the methods generated in previous versions of MyBatis generator like countByExample, deleteByExample, and selectByExample. We no longer use the "by example" terms for these methods as this library has eliminated the Example class that was generated by prior versions of MyBatis generator. -For example, it is possible to write a mapper interface like this: +## Count Method Support + +The goal of count method support is to enable the creation of methods that execute a count query allowing a user to specify a where clause at runtime, but abstracting away all other details. + +To use this support, we envision creating two methods on a MyBatis mapper interface. The first method is the standard MyBatis Dynamic SQL method that will execute a select: ```java -import static examples.simple.SimpleTableDynamicSqlSupport.*; +@SelectProvider(type=SqlProviderAdapter.class, method="select") +long count(SelectStatementProvider selectStatement); +``` -import java.util.List; +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: -import org.apache.ibatis.annotations.Mapper; -import org.apache.ibatis.annotations.Result; -import org.apache.ibatis.annotations.Results; -import org.apache.ibatis.annotations.SelectProvider; -import org.apache.ibatis.type.JdbcType; -import org.mybatis.dynamic.sql.select.SelectDSL; -import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; -import org.mybatis.dynamic.sql.util.SqlProviderAdapter; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectByExampleHelper; +```java +default long count(MyBatis3CountHelper helper) { + return helper.apply(SelectDSL.selectWithMapper(this::count, SqlBuilder.count()) + .from(simpleTable)) + .build() + .execute(); +} +``` -@Mapper -public interface SimpleTableMapper { +This method shows the use of `MyBatis3CountHelper` 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(h -> + h.where(occupation, isNull())); +``` + +There is a utility method that can be used to count all rows in a table: + +```java +long rows = mapper.count(MyBatis3CountHelper.allRows()); +``` + +## Delete Method Support + +The goal of delete method support is to enable the creation of methods that execute a delete statement allowing a user to specify a where clause at runtime, but abstracting away all other details. + +To use this support, we envision creating two methods on a MyBatis mapper interface. The first method is the standard MyBatis Dynamic SQL method that will execute a delete: + +```java +@DeleteProvider(type=SqlProviderAdapter.class, method="delete") +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(MyBatis3DeleteHelper helper) { + return helper.apply(MyBatis3Utils.deleteFrom(this::delete, simpleTable)) + .build() + .execute(); +} +``` + +This method shows the use of `MyBatis3DeleteHelper` 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(h -> + h.where(occupation, isNull())); +``` + +There is a utility method that can be used to delete all rows in a table: + +```java +int rows = mapper.delete(MyBatis3DeleteHelper.allRows()); +``` + +## Select Method Support + +The goal of select method support is to enable the creation of methods that execute a select statement allowing a user to specify a where clause and/or order by clause at runtime, but abstracting away all other details. + +To use this support, we envision creating several methods on a MyBatis mapper interface. The first two methods are the standard MyBatis Dynamic SQL method that will execute a select: + +```java +@SelectProvider(type=SqlProviderAdapter.class, method="select") +@Results(id="SimpleTableResult", value= { + @Result(column="A_ID", property="id", jdbcType=JdbcType.INTEGER, id=true), + @Result(column="first_name", property="firstName", jdbcType=JdbcType.VARCHAR), + @Result(column="last_name", property="lastName", jdbcType=JdbcType.VARCHAR), + @Result(column="birth_date", property="birthDate", jdbcType=JdbcType.DATE), + @Result(column="employed", property="employed", jdbcType=JdbcType.VARCHAR), + @Result(column="occupation", property="occupation", jdbcType=JdbcType.VARCHAR) +}) +List selectMany(SelectStatementProvider selectStatement); - @SelectProvider(type=SqlProviderAdapter.class, method="select") - @Results(id="SimpleTableResult", value= { - @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true), - @Result(column="first_name", property="firstName", jdbcType=JdbcType.VARCHAR), - @Result(column="last_name", property="lastName", jdbcType=JdbcType.VARCHAR), - @Result(column="birth_date", property="birthDate", jdbcType=JdbcType.DATE), - @Result(column="employed", property="employed", jdbcType=JdbcType.VARCHAR, typeHandler=YesNoTypeHandler.class), - @Result(column="occupation", property="occupation", jdbcType=JdbcType.VARCHAR) - }) - List selectMany(SelectStatementProvider selectStatement); +@SelectProvider(type=SqlProviderAdapter.class, method="select") +@ResultMap("SimpleTableResult") +Optional selectOne(SelectStatementProvider selectStatement); +``` + +These two methods are standard methods for MyBatis Dynamic SQL. They execute a select and return either a list of records, or a single record. + +The `selectOne` method can be used to implement a generalized select one method: + +```java +default Optional selectOne(MyBatis3SelectOneHelper helper) { + return helper.apply(SelectDSL.selectWithMapper(this::selectOne, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation) + .from(simpleTable)) + .build() + .execute(); +} +``` + +This method shows the use of `MyBatis3SelectOneHelper` 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: + +```java +default Optional selectByPrimaryKey(Integer id_) { + return selectOne(h -> + h.where(id, isEqualTo(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(MyBatis3SelectListHelper helper) { + return helper.apply(SelectDSL.selectWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, + employed, occupation) + .from(simpleTable)) + .build() + .execute(); +} - default List selectByExample(MyBatis3SelectByExampleHelper helper) { - return helper.apply(SelectDSL.selectWithMapper(this::selectMany, id, firstName, lastName, birthDate, employed, occupation) - .from(simpleTable)) - .build() - .execute(); - } +default List selectDistinct(MyBatis3SelectListHelper helper) { + return helper.apply(SelectDSL.selectDistinctWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, + birthDate, employed, occupation) + .from(simpleTable)) + .build() + .execute(); } ``` -Notice the `selectByExample` method - it specifies the column list and table name and accepts a lambda expression that can be used to build the WHERE clause. It also reuses the `selectMany` mapper method. -The code is used like this: +These methods show the use of `MyBatis3SelectListHelper` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause and/or an order by clause. + +Clients can use the methods as follows: + +```java +List rows = mapper.select(h -> + h.where(id, isEqualTo(1)) + .or(occupation, isNull())); +``` + +There are utility methods that will select all rows in a table: ```java - List rows = mapper.selectByExample(q -> - q.where(id, isEqualTo(1)) - .or(occupation, isNull())); +List rows = + mapper.selectByExample(MyBatis3SelectListHelper.allRows()); ``` +The following query will select all rows in a specified order: -It is expected that MyBatis Generator will generate code that looks like this. +```java +List rows = + mapper.selectByExample(MyBatis3SelectListHelper.allRowsOrderedBy(lastName, firstName)); +``` + +## Update Method Support + +The goal of update method support is to enable the creation of methods that execute an update statement allowing a user to specify values to set and a where clause at runtime, but abstracting away all other details. + +To use this support, we envision creating several methods on a MyBatis mapper interface. The first method is a standard MyBatis Dynamic SQL method that will execute a update: + +```java +@UpdateProvider(type=SqlProviderAdapter.class, method="update") +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(MyBatis3UpdateHelper helper) { + return helper.apply(MyBatis3Utils.update(this::update, simpleTable)) + .build() + .execute(); +} +``` + +This method shows the use of `MyBatis3UpdateHelper` 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(h -> + h.set(occupation).equalTo("Programmer") + .where(id, isEqualTo(100))); +``` + +All rows in a table can be updated by simply omitting the where clause: + +```java +int rows = mapper.update(h -> + h.set(occupation).equalTo("Programmer")); +``` + +It is also possible to write a utility method that will set values. For example: + +```java +static UpdateDSL> setSelective(SimpleTableRecord record, + UpdateDSL> dsl) { + return dsl.set(id).equalToWhenPresent(record::getId) + .set(firstName).equalToWhenPresent(record::getFirstName) + .set(lastName).equalToWhenPresent(record::getLastName) + .set(birthDate).equalToWhenPresent(record::getBirthDate) + .set(employed).equalToWhenPresent(record::getEmployed) + .set(occupation).equalToWhenPresent(record::getOccupation); +} +``` + +This method will selectively set values if corresponding fields in a record are non null. This method can be used as follows: + +```java +rows = mapper.update(h -> + setSelective(updateRecord, h) + .where(id, isEqualTo(100))); +``` -## Prior Support +# Prior Support Prior to version 1.1.3, it was also possible to write reusable methods, but they were a bit inconsistent with other helper methods. For example, it is possible to write a mapper interface like this: diff --git a/src/test/java/examples/simple/SimpleTableAnnotatedMapperNewStyle.java b/src/test/java/examples/simple/newstyle/SimpleTableMapperNewStyle.java similarity index 65% rename from src/test/java/examples/simple/SimpleTableAnnotatedMapperNewStyle.java rename to src/test/java/examples/simple/newstyle/SimpleTableMapperNewStyle.java index 880c8c28e..ea676b9f8 100644 --- a/src/test/java/examples/simple/SimpleTableAnnotatedMapperNewStyle.java +++ b/src/test/java/examples/simple/newstyle/SimpleTableMapperNewStyle.java @@ -13,12 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package examples.simple; +package examples.simple.newstyle; import static examples.simple.SimpleTableDynamicSqlSupport.*; import static org.mybatis.dynamic.sql.SqlBuilder.*; import java.util.List; +import java.util.Optional; import org.apache.ibatis.annotations.DeleteProvider; import org.apache.ibatis.annotations.InsertProvider; @@ -28,10 +29,8 @@ import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.SelectProvider; import org.apache.ibatis.annotations.UpdateProvider; -import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.type.JdbcType; import org.mybatis.dynamic.sql.SqlBuilder; -import org.mybatis.dynamic.sql.delete.DeleteDSL; import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider; import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider; import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider; @@ -41,20 +40,26 @@ import org.mybatis.dynamic.sql.update.UpdateDSL; import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; import org.mybatis.dynamic.sql.util.SqlProviderAdapter; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountByExampleHelper; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteByExampleHelper; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectByExampleHelper; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleCompleter; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleHelper; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountHelper; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteHelper; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectListHelper; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectOneHelper; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateHelper; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateModelToIntAdapter; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; + +import examples.simple.LastNameTypeHandler; +import examples.simple.SimpleTableRecord; +import examples.simple.YesNoTypeHandler; /** * - * Note: this is the canonical mapper with the new style ByExample methods + * Note: this is the canonical mapper with the new style methods * and represents the desired output for MyBatis Generator * */ @Mapper -public interface SimpleTableAnnotatedMapperNewStyle { +public interface SimpleTableMapperNewStyle { @InsertProvider(type=SqlProviderAdapter.class, method="insert") int insert(InsertStatementProvider insertStatement); @@ -78,11 +83,7 @@ public interface SimpleTableAnnotatedMapperNewStyle { @SelectProvider(type=SqlProviderAdapter.class, method="select") @ResultMap("SimpleTableResult") - List selectManyWithRowbounds(SelectStatementProvider selectStatement, RowBounds rowBounds); - - @SelectProvider(type=SqlProviderAdapter.class, method="select") - @ResultMap("SimpleTableResult") - SimpleTableRecord selectOne(SelectStatementProvider selectStatement); + Optional selectOne(SelectStatementProvider selectStatement); @DeleteProvider(type=SqlProviderAdapter.class, method="delete") int delete(DeleteStatementProvider deleteStatement); @@ -90,24 +91,23 @@ public interface SimpleTableAnnotatedMapperNewStyle { @SelectProvider(type=SqlProviderAdapter.class, method="select") long count(SelectStatementProvider selectStatement); - default long countByExample(MyBatis3CountByExampleHelper helper) { + default long count(MyBatis3CountHelper helper) { return helper.apply(SelectDSL.selectWithMapper(this::count, SqlBuilder.count()) .from(simpleTable)) .build() .execute(); } - default int deleteByExample(MyBatis3DeleteByExampleHelper helper) { - return helper.apply(DeleteDSL.deleteFromWithMapper(this::delete, simpleTable)) + default int delete(MyBatis3DeleteHelper helper) { + return helper.apply(MyBatis3Utils.deleteFrom(this::delete, simpleTable)) .build() .execute(); } default int deleteByPrimaryKey(Integer id_) { - return DeleteDSL.deleteFromWithMapper(this::delete, simpleTable) - .where(id, isEqualTo(id_)) - .build() - .execute(); + return delete(h -> + h.where(id, isEqualTo(id_)) + ); } default int insert(SimpleTableRecord record) { @@ -149,79 +149,78 @@ default int insertSelective(SimpleTableRecord record) { .render(RenderingStrategy.MYBATIS3)); } - default List selectByExample(MyBatis3SelectByExampleHelper helper) { + default Optional selectOne(MyBatis3SelectOneHelper helper) { + return helper.apply(SelectDSL.selectWithMapper(this::selectOne, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation) + .from(simpleTable)) + .build() + .execute(); + } + + default List select(MyBatis3SelectListHelper helper) { return helper.apply(SelectDSL.selectWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation) .from(simpleTable)) .build() .execute(); } - default List selectDistinctByExample(MyBatis3SelectByExampleHelper helper) { + default List selectDistinct(MyBatis3SelectListHelper helper) { return helper.apply(SelectDSL.selectDistinctWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation) .from(simpleTable)) .build() .execute(); } - default SimpleTableRecord selectByPrimaryKey(Integer id_) { - return SelectDSL.selectWithMapper(this::selectOne, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation) - .from(simpleTable) - .where(id, isEqualTo(id_)) - .build() - .execute(); + default Optional selectByPrimaryKey(Integer id_) { + return selectOne(h -> + h.where(id, isEqualTo(id_)) + ); } - default MyBatis3UpdateByExampleCompleter updateByExample(MyBatis3UpdateByExampleHelper helper) { - return new MyBatis3UpdateByExampleCompleter.Builder() - .withHelper(helper) - .withMapper(this::update) - .withTable(simpleTable) - .withValueSetter((record, dsl) -> - dsl.set(id).equalTo(record::getId) - .set(firstName).equalTo(record::getFirstName) - .set(lastName).equalTo(record::getLastName) - .set(birthDate).equalTo(record::getBirthDate) - .set(employed).equalTo(record::getEmployed) - .set(occupation).equalTo(record::getOccupation)) - .build(); - } - - default MyBatis3UpdateByExampleCompleter updateByExampleSelective(MyBatis3UpdateByExampleHelper helper) { - return new MyBatis3UpdateByExampleCompleter.Builder() - .withHelper(helper) - .withMapper(this::update) - .withTable(simpleTable) - .withValueSetter((record, dsl) -> - dsl.set(id).equalToWhenPresent(record::getId) - .set(firstName).equalToWhenPresent(record::getFirstName) - .set(lastName).equalToWhenPresent(record::getLastName) - .set(birthDate).equalToWhenPresent(record::getBirthDate) - .set(employed).equalToWhenPresent(record::getEmployed) - .set(occupation).equalToWhenPresent(record::getOccupation)) - .build(); + default int update(MyBatis3UpdateHelper helper) { + return helper.apply(MyBatis3Utils.update(this::update, simpleTable)) + .build() + .execute(); } - default int updateByPrimaryKey(SimpleTableRecord record) { - return UpdateDSL.updateWithMapper(this::update, simpleTable) + static UpdateDSL setAll(SimpleTableRecord record, + UpdateDSL dsl) { + return dsl.set(id).equalTo(record::getId) .set(firstName).equalTo(record::getFirstName) .set(lastName).equalTo(record::getLastName) .set(birthDate).equalTo(record::getBirthDate) .set(employed).equalTo(record::getEmployed) - .set(occupation).equalTo(record::getOccupation) - .where(id, isEqualTo(record::getId)) - .build() - .execute(); + .set(occupation).equalTo(record::getOccupation); } - - default int updateByPrimaryKeySelective(SimpleTableRecord record) { - return UpdateDSL.updateWithMapper(this::update, simpleTable) + + static UpdateDSL setSelective(SimpleTableRecord record, + UpdateDSL dsl) { + return dsl.set(id).equalToWhenPresent(record::getId) .set(firstName).equalToWhenPresent(record::getFirstName) .set(lastName).equalToWhenPresent(record::getLastName) .set(birthDate).equalToWhenPresent(record::getBirthDate) .set(employed).equalToWhenPresent(record::getEmployed) - .set(occupation).equalToWhenPresent(record::getOccupation) - .where(id, isEqualTo(record::getId)) - .build() - .execute(); + .set(occupation).equalToWhenPresent(record::getOccupation); + } + + default int updateByPrimaryKey(SimpleTableRecord record) { + return update(h -> + h.set(firstName).equalTo(record::getFirstName) + .set(lastName).equalTo(record::getLastName) + .set(birthDate).equalTo(record::getBirthDate) + .set(employed).equalTo(record::getEmployed) + .set(occupation).equalTo(record::getOccupation) + .where(id, isEqualTo(record::getId)) + ); + } + + default int updateByPrimaryKeySelective(SimpleTableRecord record) { + return update(h -> + h.set(firstName).equalToWhenPresent(record::getFirstName) + .set(lastName).equalToWhenPresent(record::getLastName) + .set(birthDate).equalToWhenPresent(record::getBirthDate) + .set(employed).equalToWhenPresent(record::getEmployed) + .set(occupation).equalToWhenPresent(record::getOccupation) + .where(id, isEqualTo(record::getId)) + ); } } diff --git a/src/test/java/examples/simple/SimpleTableAnnotatedNewStyleMapperTest.java b/src/test/java/examples/simple/newstyle/SimpleTableMapperNewStyleTest.java similarity index 58% rename from src/test/java/examples/simple/SimpleTableAnnotatedNewStyleMapperTest.java rename to src/test/java/examples/simple/newstyle/SimpleTableMapperNewStyleTest.java index 203ea859e..eaddf0958 100644 --- a/src/test/java/examples/simple/SimpleTableAnnotatedNewStyleMapperTest.java +++ b/src/test/java/examples/simple/newstyle/SimpleTableMapperNewStyleTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package examples.simple; +package examples.simple.newstyle; import static examples.simple.SimpleTableDynamicSqlSupport.*; import static org.assertj.core.api.Assertions.assertThat; @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Optional; import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; import org.apache.ibatis.jdbc.ScriptRunner; @@ -38,12 +39,14 @@ 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.MyBatis3CountByExampleHelper; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteByExampleHelper; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectByExampleHelper; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateByExampleHelper; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3CountHelper; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteHelper; +import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectListHelper; -public class SimpleTableAnnotatedNewStyleMapperTest { +import examples.simple.LastName; +import examples.simple.SimpleTableRecord; + +public class SimpleTableMapperNewStyleTest { private static final String JDBC_URL = "jdbc:hsqldb:mem:aname"; private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver"; @@ -63,17 +66,17 @@ public void setup() throws Exception { UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", ""); Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); Configuration config = new Configuration(environment); - config.addMapper(SimpleTableAnnotatedMapperNewStyle.class); + config.addMapper(SimpleTableMapperNewStyle.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); } @Test - public void testSelectByExample() { + public void testSelect() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); - List rows = mapper.selectByExample(q -> - q.where(id, isEqualTo(1)) + List rows = mapper.select(h -> + h.where(id, isEqualTo(1)) .or(occupation, isNull())); assertThat(rows.size()).isEqualTo(3); @@ -83,9 +86,9 @@ public void testSelectByExample() { @Test public void testSelectAll() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); - List rows = mapper.selectByExample(MyBatis3SelectByExampleHelper.allRows()); + List rows = mapper.select(MyBatis3SelectListHelper.allRows()); assertThat(rows.size()).isEqualTo(6); assertThat(rows.get(0).getId()).isEqualTo(1); @@ -96,10 +99,10 @@ public void testSelectAll() { @Test public void testSelectAllOrdered() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); List rows = mapper - .selectByExample(MyBatis3SelectByExampleHelper.allRowsOrderdBy(lastName.descending(), firstName.descending())); + .select(MyBatis3SelectListHelper.allRowsOrderdBy(lastName.descending(), firstName.descending())); assertThat(rows.size()).isEqualTo(6); assertThat(rows.get(0).getId()).isEqualTo(5); @@ -108,12 +111,12 @@ public void testSelectAllOrdered() { } @Test - public void testSelectDistinctByExample() { + public void testSelectDistinct() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); - List rows = mapper.selectDistinctByExample(q -> - q.where(id, isGreaterThan(1)) + List rows = mapper.selectDistinct(h -> + h.where(id, isGreaterThan(1)) .or(occupation, isNull())); assertThat(rows.size()).isEqualTo(5); @@ -121,12 +124,12 @@ public void testSelectDistinctByExample() { } @Test - public void testSelectByExampleWithTypeHandler() { + public void testSelectWithTypeHandler() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); - List rows = mapper.selectByExample(q -> - q.where(employed, isEqualTo(false)) + List rows = mapper.select(h -> + h.where(employed, isEqualTo(false)) .orderBy(id)); assertAll( @@ -137,13 +140,23 @@ public void testSelectByExampleWithTypeHandler() { } } + @Test + public void testSelectByPrimaryKeyWithMissingRecord() { + try (SqlSession session = sqlSessionFactory.openSession()) { + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); + + Optional record = mapper.selectByPrimaryKey(300); + assertThat(record.isPresent()).isFalse(); + } + } + @Test public void testFirstNameIn() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); - List rows = mapper.selectByExample(q -> - q.where(firstName, isIn("Fred", "Barney"))); + List rows = mapper.select(h -> + h.where(firstName, isIn("Fred", "Barney"))); assertAll( () -> assertThat(rows.size()).isEqualTo(2), @@ -154,11 +167,11 @@ public void testFirstNameIn() { } @Test - public void testDeleteByExample() { + public void testDelete() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); - int rows = mapper.deleteByExample(q -> - q.where(occupation, isNull())); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); + int rows = mapper.delete(h -> + h.where(occupation, isNull())); assertThat(rows).isEqualTo(2); } } @@ -166,8 +179,8 @@ public void testDeleteByExample() { @Test public void testDeleteAll() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); - int rows = mapper.deleteByExample(MyBatis3DeleteByExampleHelper.allRows()); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); + int rows = mapper.delete(MyBatis3DeleteHelper.allRows()); assertThat(rows).isEqualTo(6); } @@ -176,7 +189,7 @@ public void testDeleteAll() { @Test public void testDeleteByPrimaryKey() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); int rows = mapper.deleteByPrimaryKey(2); assertThat(rows).isEqualTo(1); @@ -186,7 +199,7 @@ public void testDeleteByPrimaryKey() { @Test public void testInsert() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); SimpleTableRecord record = new SimpleTableRecord(); record.setId(100); record.setFirstName("Joe"); @@ -203,7 +216,7 @@ public void testInsert() { @Test public void testInsertMultiple() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); List records = new ArrayList<>(); @@ -233,7 +246,7 @@ record = new SimpleTableRecord(); @Test public void testInsertSelective() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); SimpleTableRecord record = new SimpleTableRecord(); record.setId(100); record.setFirstName("Joe"); @@ -249,7 +262,7 @@ public void testInsertSelective() { @Test public void testUpdateByPrimaryKey() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); SimpleTableRecord record = new SimpleTableRecord(); record.setId(100); record.setFirstName("Joe"); @@ -265,15 +278,16 @@ public void testUpdateByPrimaryKey() { rows = mapper.updateByPrimaryKey(record); assertThat(rows).isEqualTo(1); - SimpleTableRecord newRecord = mapper.selectByPrimaryKey(100); - assertThat(newRecord.getOccupation()).isEqualTo("Programmer"); + Optional newRecord = mapper.selectByPrimaryKey(100); + assertThat(newRecord.isPresent()).isTrue(); + assertThat(newRecord.get().getOccupation()).isEqualTo("Programmer"); } } @Test public void testUpdateByPrimaryKeySelective() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); SimpleTableRecord record = new SimpleTableRecord(); record.setId(100); record.setFirstName("Joe"); @@ -291,16 +305,17 @@ public void testUpdateByPrimaryKeySelective() { rows = mapper.updateByPrimaryKeySelective(updateRecord); assertThat(rows).isEqualTo(1); - SimpleTableRecord newRecord = mapper.selectByPrimaryKey(100); - assertThat(newRecord.getOccupation()).isEqualTo("Programmer"); - assertThat(newRecord.getFirstName()).isEqualTo("Joe"); + Optional newRecord = mapper.selectByPrimaryKey(100); + assertThat(newRecord.isPresent()).isTrue(); + assertThat(newRecord.get().getOccupation()).isEqualTo("Programmer"); + assertThat(newRecord.get().getFirstName()).isEqualTo("Joe"); } } @Test - public void testUpdateByExample() { + public void testUpdate() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); SimpleTableRecord record = new SimpleTableRecord(); record.setId(100); record.setFirstName("Joe"); @@ -314,22 +329,50 @@ public void testUpdateByExample() { record.setOccupation("Programmer"); - rows = mapper.updateByExample(q -> - q.where(id, isEqualTo(100)) - .and(firstName, isEqualTo("Joe"))) - .usingRecord(record); + rows = mapper.update(h -> + SimpleTableMapperNewStyle.setAll(record, h) + .where(id, isEqualTo(100)) + .and(firstName, isEqualTo("Joe"))); + + assertThat(rows).isEqualTo(1); + + Optional newRecord = mapper.selectByPrimaryKey(100); + assertThat(newRecord.isPresent()).isTrue(); + assertThat(newRecord.get().getOccupation()).isEqualTo("Programmer"); + } + } + + @Test + public void testUpdateOneField() { + try (SqlSession session = sqlSessionFactory.openSession()) { + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); + SimpleTableRecord record = new SimpleTableRecord(); + record.setId(100); + record.setFirstName("Joe"); + record.setLastName(LastName.of("Jones")); + record.setBirthDate(new Date()); + record.setEmployed(true); + record.setOccupation("Developer"); + + int rows = mapper.insert(record); + assertThat(rows).isEqualTo(1); + + rows = mapper.update(h -> + h.set(occupation).equalTo("Programmer") + .where(id, isEqualTo(100))); assertThat(rows).isEqualTo(1); - SimpleTableRecord newRecord = mapper.selectByPrimaryKey(100); - assertThat(newRecord.getOccupation()).isEqualTo("Programmer"); + Optional newRecord = mapper.selectByPrimaryKey(100); + assertThat(newRecord.isPresent()).isTrue(); + assertThat(newRecord.get().getOccupation()).isEqualTo("Programmer"); } } @Test public void testUpdateAll() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); SimpleTableRecord record = new SimpleTableRecord(); record.setId(100); record.setFirstName("Joe"); @@ -341,23 +384,54 @@ public void testUpdateAll() { int rows = mapper.insert(record); assertThat(rows).isEqualTo(1); - record = new SimpleTableRecord(); - record.setOccupation("Programmer"); - rows = mapper.updateByExampleSelective(MyBatis3UpdateByExampleHelper.allRows()).usingRecord(record); + SimpleTableRecord updateRecord = new SimpleTableRecord(); + updateRecord.setOccupation("Programmer"); + rows = mapper.update(h -> + SimpleTableMapperNewStyle.setSelective(updateRecord, h)); assertThat(rows).isEqualTo(7); - SimpleTableRecord newRecord = mapper.selectByPrimaryKey(100); - assertThat(newRecord.getOccupation()).isEqualTo("Programmer"); + Optional newRecord = mapper.selectByPrimaryKey(100); + assertThat(newRecord.isPresent()).isTrue(); + assertThat(newRecord.get().getOccupation()).isEqualTo("Programmer"); + } + } + + @Test + public void testUpdateSelective() { + try (SqlSession session = sqlSessionFactory.openSession()) { + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); + SimpleTableRecord record = new SimpleTableRecord(); + record.setId(100); + record.setFirstName("Joe"); + record.setLastName(LastName.of("Jones")); + record.setBirthDate(new Date()); + record.setEmployed(true); + record.setOccupation("Developer"); + + int rows = mapper.insert(record); + assertThat(rows).isEqualTo(1); + + SimpleTableRecord updateRecord = new SimpleTableRecord(); + updateRecord.setOccupation("Programmer"); + rows = mapper.update(h -> + SimpleTableMapperNewStyle.setSelective(updateRecord, h) + .where(id, isEqualTo(100))); + + assertThat(rows).isEqualTo(1); + + Optional newRecord = mapper.selectByPrimaryKey(100); + assertThat(newRecord.isPresent()).isTrue(); + assertThat(newRecord.get().getOccupation()).isEqualTo("Programmer"); } } @Test - public void testCountByExample() { + public void testCount() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); - long rows = mapper.countByExample(q -> - q.where(occupation, isNull())); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); + long rows = mapper.count(h -> + h.where(occupation, isNull())); assertThat(rows).isEqualTo(2L); } @@ -366,8 +440,8 @@ public void testCountByExample() { @Test public void testCountAll() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); - long rows = mapper.countByExample(MyBatis3CountByExampleHelper.allRows()); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); + long rows = mapper.count(MyBatis3CountHelper.allRows()); assertThat(rows).isEqualTo(6L); } @@ -376,10 +450,10 @@ public void testCountAll() { @Test public void testTypeHandledLike() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); - List rows = mapper.selectByExample(q -> - q.where(lastName, isLike(LastName.of("Fl%"))) + List rows = mapper.select(h -> + h.where(lastName, isLike(LastName.of("Fl%"))) .orderBy(id)); assertThat(rows.size()).isEqualTo(3); @@ -390,10 +464,10 @@ public void testTypeHandledLike() { @Test public void testTypeHandledNotLike() { try (SqlSession session = sqlSessionFactory.openSession()) { - SimpleTableAnnotatedMapperNewStyle mapper = session.getMapper(SimpleTableAnnotatedMapperNewStyle.class); + SimpleTableMapperNewStyle mapper = session.getMapper(SimpleTableMapperNewStyle.class); - List rows = mapper.selectByExample(q -> - q.where(lastName, isNotLike(LastName.of("Fl%"))) + List rows = mapper.select(h -> + h.where(lastName, isNotLike(LastName.of("Fl%"))) .orderBy(id)); assertThat(rows.size()).isEqualTo(3);