Skip to content

Add Utility Classes and Pattern for Improved General Mappers #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -72,8 +71,6 @@ One capability is that very expressive dynamic queries can be generated. Here's

List<AnimalData> animals = mapper.selectMany(selectStatement);
assertThat(animals.size()).isEqualTo(4);
} finally {
sqlSession.close();
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -32,7 +32,7 @@
* &#64;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()
Expand All @@ -43,34 +43,34 @@
* <p>And then call the simplified default method like this:
*
* <pre>
* long rows = mapper.countByExample(q -&gt;
* long rows = mapper.count(q -&gt;
* q.where(occupation, isNull()));
* </pre>
*
* <p>You can implement a "count all" with the following code:
*
* <pre>
* long rows = mapper.countByExample(q -&gt; q);
* long rows = mapper.count(q -&gt; q);
* </pre>
*
* <p>Or
*
* <pre>
* long rows = mapper.countByExample(MyBatis3CountByExampleHelper.allRows());
* long rows = mapper.count(MyBatis3CountHelper.allRows());
* </pre>
*
* @author Jeff Butler
*/
@FunctionalInterface
public interface MyBatis3CountByExampleHelper extends
public interface MyBatis3CountHelper extends
Function<QueryExpressionDSL<MyBatis3SelectModelAdapter<Long>>, Buildable<MyBatis3SelectModelAdapter<Long>>> {

/**
* Returns a helper that can be used to count every row in a table.
*
* @return the helper that will count every row in a table
*/
static MyBatis3CountByExampleHelper allRows() {
static MyBatis3CountHelper allRows() {
return h -> h;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -32,7 +31,7 @@
* &#64;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();
Expand All @@ -42,34 +41,34 @@
* <p>And then call the simplified default method like this:
*
* <pre>
* int rows = mapper.deleteByExample(q -&gt;
* int rows = mapper.delete(q -&gt;
* q.where(occupation, isNull()));
* </pre>
*
* <p>You can implement a "delete all" with the following code:
*
* <pre>
* int rows = mapper.deleteByExample(q -&gt; q);
* int rows = mapper.delete(q -&gt; q);
* </pre>
*
* <p>Or
*
* <pre>
* long rows = mapper.deleteByExample(MyBatis3DeleteByExampleHelper.allRows());
* long rows = mapper.delete(MyBatis3DeleteHelper.allRows());
* </pre>

* @author Jeff Butler
*/
@FunctionalInterface
public interface MyBatis3DeleteByExampleHelper extends
Function<DeleteDSL<MyBatis3DeleteModelAdapter<Integer>>, Buildable<MyBatis3DeleteModelAdapter<Integer>>> {
public interface MyBatis3DeleteHelper extends
Function<DeleteDSL<MyBatis3DeleteModelToIntAdapter>, Buildable<MyBatis3DeleteModelToIntAdapter>> {

/**
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<DeleteStatementProvider> mapperMethod;

private MyBatis3DeleteModelToIntAdapter(DeleteModel deleteModel,
ToIntFunction<DeleteStatementProvider> 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<DeleteStatementProvider> mapperMethod) {
return new MyBatis3DeleteModelToIntAdapter(deleteModel, mapperMethod);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -42,7 +42,7 @@
* })
* List&lt;SimpleRecord&gt; selectMany(SelectStatementProvider selectStatement);
*
* default List&lt;SimpleRecord&gt; selectByExample(MyBatis3SelectByExampleHelper&lt;SimpleRecord&gt; helper) {
* default List&lt;SimpleRecord&gt; select(MyBatis3SelectHelper&lt;SimpleRecord&gt; helper) {
* return helper.apply(SelectDSL.selectWithMapper(this::selectMany, simpleTable.allColumns())
* .from(simpleTable))
* .build()
Expand All @@ -53,27 +53,27 @@
* <p>And then call the simplified default method like this:
*
* <pre>
* List&lt;SimpleRecord&gt; rows = mapper.selectByExample(q -&gt;
* List&lt;SimpleRecord&gt; rows = mapper.select(q -&gt;
* q.where(id, isEqualTo(1))
* .or(occupation, isNull()));
* </pre>
*
* <p>You can implement a "select all" with the following code:
*
* <pre>
* List&lt;SimpleRecord&gt; rows = mapper.selectByExample(q -&gt; q);
* List&lt;SimpleRecord&gt; rows = mapper.select(q -&gt; q);
* </pre>
*
* <p>Or
*
* <pre>
* List&lt;SimpleRecord&gt; rows = mapper.selectByExample(MyBatis3SelectByExampleHelper.allRows());
* List&lt;SimpleRecord&gt; rows = mapper.select(MyBatis3SelectHelper.allRows());
* </pre>
*
* @author Jeff Butler
*/
@FunctionalInterface
public interface MyBatis3SelectByExampleHelper<T> extends
public interface MyBatis3SelectListHelper<T> extends
Function<QueryExpressionDSL<MyBatis3SelectModelAdapter<List<T>>>,
Buildable<MyBatis3SelectModelAdapter<List<T>>>> {

Expand All @@ -84,7 +84,7 @@ public interface MyBatis3SelectByExampleHelper<T> extends
*
* @return the helper that will select every row in a table
*/
static <T> MyBatis3SelectByExampleHelper<T> allRows() {
static <T> MyBatis3SelectListHelper<T> allRows() {
return h -> h;
}

Expand All @@ -96,7 +96,7 @@ static <T> MyBatis3SelectByExampleHelper<T> allRows() {
*
* @return the helper that will select every row in a table in the specified order
*/
static <T> MyBatis3SelectByExampleHelper<T> allRowsOrderdBy(SortSpecification...columns) {
static <T> MyBatis3SelectListHelper<T> allRowsOrderdBy(SortSpecification...columns) {
return h -> h.orderBy(columns);
}
}
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>For example, you can create mapper interface methods like this:
*
* <pre>
* &#64;SelectProvider(type=SqlProviderAdapter.class, method="select")
* &#64;ResultMap("SimpleTableResult")
* Optional&lt;SimpleTableRecord&gt; selectOne(SelectStatementProvider selectStatement);
*
* default Optional&lt;SimpleTableRecord&gt; selectOne(MyBatis3SelectOneHelper&lt;SimpleTableRecord&gt; helper) {
* return helper.apply(SelectDSL.selectWithMapper(this::selectOne, simpleTable.allColumns())
* .from(simpleTable))
* .build()
* .execute();
* }
* </pre>
*
* <p>And then call the simplified default method like this:
*
* <pre>
* Optional&lt;SimpleRecord&gt; record = mapper.selectOne(q -&gt;
* q.where(id, isEqualTo(1)));
* </pre>
*
* @author Jeff Butler
*/
@FunctionalInterface
public interface MyBatis3SelectOneHelper<T> extends
Function<QueryExpressionDSL<MyBatis3SelectModelAdapter<Optional<T>>>,
Buildable<MyBatis3SelectModelAdapter<Optional<T>>>> {
}
Loading