diff --git a/CHANGELOG.md b/CHANGELOG.md index 2199233e8..459559026 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles ### Added - Added a callback capability to the "In" conditions that will be called before rendering when the conditions are empty. Also, removed the option that forced the library to render invalid SQL in that case. ([#241](https://github.com/mybatis/mybatis-dynamic-sql/pull/241)) +- Added a utility mapper for MyBatis that allows you to run any select query without having to predefine a result mapping. ([#255](https://github.com/mybatis/mybatis-dynamic-sql/pull/255)) ## Release 1.2.0 - August 19, 2020 diff --git a/pom.xml b/pom.xml index 0fe66e42d..1caf322b0 100644 --- a/pom.xml +++ b/pom.xml @@ -208,6 +208,12 @@ 5.2.8.RELEASE provided + + org.mybatis + mybatis + 3.5.5 + provided + org.junit.jupiter @@ -233,12 +239,6 @@ 3.17.2 test - - org.mybatis - mybatis - 3.5.5 - test - org.mybatis mybatis-spring diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/GeneralMapper.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/GeneralMapper.java new file mode 100644 index 000000000..c5f9626ae --- /dev/null +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/GeneralMapper.java @@ -0,0 +1,290 @@ +/* + * Copyright 2016-2020 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.math.BigDecimal; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; + +import org.apache.ibatis.annotations.SelectProvider; +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; +import org.mybatis.dynamic.sql.util.SqlProviderAdapter; + +/** + * This is a general purpose MyBatis mapper. It allows you to execute select statements without having to + * write a custom {@link org.apache.ibatis.annotations.ResultMap} for each statement. + * + *

This mapper contains three types of methods: + *

+ * + *

This mapper can be injected as-is into a MyBatis configuration, or it can be extended with existing mappers. + * + * @author Jeff Butler + */ +public interface GeneralMapper { + /** + * Select a single row as a Map of values. The row may have any number of columns. + * The Map key will be the column name as returned from the + * database (may be aliased if an alias is specified in the select statement). Map entries will be + * of data types determined by the JDBC driver. MyBatis will call ResultSet.getObject() to retrieve + * values from the ResultSet. Reference your JDBC driver documentation to learn about type mappings + * for your specific database. + * + * @param selectStatement the select statement + * @return A Map containing the row values. + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + Map selectOneMappedRow(SelectStatementProvider selectStatement); + + /** + * Select a single row of values and then convert the values to a custom type. This is similar + * to the Spring JDBC template method of processing result sets. In this case, MyBatis will first extract + * the row values into a Map, and then a row mapper can retrieve values from the Map and use them + * to construct a custom object. + * + *

See {@link GeneralMapper#selectOneMappedRow(SelectStatementProvider)} for details about + * how MyBatis will construct the Map of values. + * + * @param selectStatement the select statement + * @param rowMapper a function that will convert a Map of row values to the desired data type + * @param the datatype of the converted object + * @return the converted object + */ + default R selectOne(SelectStatementProvider selectStatement, + Function, R> rowMapper) { + return rowMapper.apply(selectOneMappedRow(selectStatement)); + } + + /** + * Select any number of rows and return a List of Maps containing row values (one Map for each row returned). + * The rows may have any number of columns. + * The Map key will be the column name as returned from the + * database (may be aliased if an alias is specified in the select statement). Map entries will be + * of data types determined by the JDBC driver. MyBatis will call ResultSet.getObject() to retrieve + * values from the ResultSet. Reference your JDBC driver documentation to learn about type mappings + * for your specific database. + * + * @param selectStatement the select statement + * @return A List of Maps containing the row values. + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + List> selectManyMappedRows(SelectStatementProvider selectStatement); + + /** + * Select any number of rows and then convert the values to a custom type. This is similar to the + * Spring JDBC template method of processing result sets. In this case, MyBatis will first extract the + * row values into a List of Map, and them a row mapper can retrieve values from the Map and use them + * to construct a custom object for each row. + * + * @param selectStatement the select statement + * @param rowMapper a function that will convert a Map of row values to the desired data type + * @param the datatype of the converted object + * @return the List of converted objects + */ + default List selectMany(SelectStatementProvider selectStatement, + Function, R> rowMapper) { + return selectManyMappedRows(selectStatement).stream() + .map(rowMapper) + .collect(Collectors.toList()); + } + + /** + * Retrieve a single {@link java.math.BigDecimal} from a result set. The result set must have + * only one column and one or zero rows. The column must be retrievable from the result set + * via the ResultSet.getBigDecimal() method. + * + * @param selectStatement the select statement + * @return the extracted value. May be null if zero rows are returned, or if the returned + * column is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + BigDecimal selectOneBigDecimal(SelectStatementProvider selectStatement); + + /** + * Retrieve a single {@link java.math.BigDecimal} from a result set. The result set must have + * only one column and one or zero rows. The column must be retrievable from the result set + * via the ResultSet.getBigDecimal() method. + * + * @param selectStatement the select statement + * @return the extracted value. The Optional will be empty if zero rows are returned, or if the returned + * column is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + Optional selectOptionalBigDecimal(SelectStatementProvider selectStatement); + + /** + * Retrieve a List of {@link java.math.BigDecimal} from a result set. The result set must have + * only one column, but can have any number of rows. The column must be retrievable from the result set + * via the ResultSet.getBigDecimal() method. + * + * @param selectStatement the select statement + * @return the list of extracted values. Any value may be null if a column in the result set is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + List selectManyBigDecimals(SelectStatementProvider selectStatement); + + /** + * Retrieve a single {@link java.lang.Double} from a result set. The result set must have + * only one column and one or zero rows. The column must be retrievable from the result set + * via the ResultSet.getDouble() method. + * + * @param selectStatement the select statement + * @return the extracted value. May be null if zero rows are returned, or if the returned + * column is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + Double selectOneDouble(SelectStatementProvider selectStatement); + + /** + * Retrieve a single {@link java.lang.Double} from a result set. The result set must have + * only one column and one or zero rows. The column must be retrievable from the result set + * via the ResultSet.getDouble() method. + * + * @param selectStatement the select statement + * @return the extracted value. The Optional will be empty if zero rows are returned, or if the returned + * column is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + Optional selectOptionalDouble(SelectStatementProvider selectStatement); + + /** + * Retrieve a List of {@link java.lang.Double} from a result set. The result set must have + * only one column, but can have any number of rows. The column must be retrievable from the result set + * via the ResultSet.getDouble() method. + * + * @param selectStatement the select statement + * @return the list of extracted values. Any value may be null if a column in the result set is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + List selectManyDoubles(SelectStatementProvider selectStatement); + + /** + * Retrieve a single {@link java.lang.Integer} from a result set. The result set must have + * only one column and one or zero rows. The column must be retrievable from the result set + * via the ResultSet.getInt() method. + * + * @param selectStatement the select statement + * @return the extracted value. May be null if zero rows are returned, or if the returned + * column is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + Integer selectOneInteger(SelectStatementProvider selectStatement); + + /** + * Retrieve a single {@link java.lang.Integer} from a result set. The result set must have + * only one column and one or zero rows. The column must be retrievable from the result set + * via the ResultSet.getInt() method. + * + * @param selectStatement the select statement + * @return the extracted value. The Optional will be empty if zero rows are returned, or if the returned + * column is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + Optional selectOptionalInteger(SelectStatementProvider selectStatement); + + /** + * Retrieve a List of {@link java.lang.Integer} from a result set. The result set must have + * only one column, but can have any number of rows. The column must be retrievable from the result set + * via the ResultSet.getInt() method. + * + * @param selectStatement the select statement + * @return the list of extracted values. Any value may be null if a column in the result set is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + List selectManyIntegers(SelectStatementProvider selectStatement); + + /** + * Retrieve a single {@link java.lang.Long} from a result set. The result set must have + * only one column and one or zero rows. The column must be retrievable from the result set + * via the ResultSet.getLong() method. + * + * @param selectStatement the select statement + * @return the extracted value. May be null if zero rows are returned, or if the returned + * column is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + Long selectOneLong(SelectStatementProvider selectStatement); + + /** + * Retrieve a single {@link java.lang.Long} from a result set. The result set must have + * only one column and one or zero rows. The column must be retrievable from the result set + * via the ResultSet.getLong() method. + * + * @param selectStatement the select statement + * @return the extracted value. The Optional will be empty if zero rows are returned, or if the returned + * column is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + Optional selectOptionalLong(SelectStatementProvider selectStatement); + + /** + * Retrieve a List of {@link java.lang.Long} from a result set. The result set must have + * only one column, but can have any number of rows. The column must be retrievable from the result set + * via the ResultSet.getLong() method. + * + * @param selectStatement the select statement + * @return the list of extracted values. Any value may be null if a column in the result set is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + List selectManyLongs(SelectStatementProvider selectStatement); + + /** + * Retrieve a single {@link java.lang.String} from a result set. The result set must have + * only one column and one or zero rows. The column must be retrievable from the result set + * via the ResultSet.getString() method. + * + * @param selectStatement the select statement + * @return the extracted value. May be null if zero rows are returned, or if the returned + * column is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + String selectOneString(SelectStatementProvider selectStatement); + + /** + * Retrieve a single {@link java.lang.String} from a result set. The result set must have + * only one column and one or zero rows. The column must be retrievable from the result set + * via the ResultSet.getString() method. + * + * @param selectStatement the select statement + * @return the extracted value. The Optional will be empty if zero rows are returned, or if the returned + * column is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + Optional selectOptionalString(SelectStatementProvider selectStatement); + + /** + * Retrieve a List of {@link java.lang.String} from a result set. The result set must have + * only one column, but can have any number of rows. The column must be retrievable from the result set + * via the ResultSet.getString() method. + * + * @param selectStatement the select statement + * @return the list of extracted values. Any value may be null if a column in the result set is null + */ + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + List selectManyStrings(SelectStatementProvider selectStatement); +} diff --git a/src/site/markdown/docs/mybatis3.md b/src/site/markdown/docs/mybatis3.md index bb87fd1ee..cf8758d67 100644 --- a/src/site/markdown/docs/mybatis3.md +++ b/src/site/markdown/docs/mybatis3.md @@ -5,6 +5,124 @@ The goal of this support is to reduce the amount of boilerplate code needed for With version 1.1.3, specialized interfaces and utilities 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. +## Dynamic Result Mapping Support +MyBatis is very good at mapping result sets to objects - this is one of its primary differentiators. MyBatis also requires +that you predefine the mappings for every possibility. This presents a challenge if you want very dynamic column lists +in a query. This library provides a generalized MyBatis mapper that can assist with that problem. + +The general mapper is `org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper`. This mapper can be injected into a MyBatis configuration +as is, or it can be extended by an existing mapper. If you are using MyBatis Spring support and auto scanning for mappers, +you can create an extension in your application's mapper package as follows: + +```java +package foo.mapper; + +import org.apache.ibatis.annotations.Mapper; +import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper; + +@Mapper +public interface MyGeneralMapper extends GeneralMapper { +} +``` + +The mapper contains three types of methods: + +1. The `selectOneMappedRow` and `selectManyMappedRows` methods allow you to use select statements with +any number of columns. MyBatis will process the rows and return a Map of values, or a List of Maps. +1. The `selectOne` and `selectMany` methods also allow you to use select statements with any number of columns. These methods +also allow you to specify a function that will transform a Map of row values into a specific object. +1. The other methods are for result sets with a single column. There are functions for many +data types (Integer, Long, String, etc.) There are also functions that return a single value, and Optional value, +or a List of values. + +An example of using the mapped row methods follows: + +```java +package foo.service; +import static org.mybatis.dynamic.sql.SqlBuilder.*; + +import java.util.List; +import java.util.Map; +import org.mybatis.dynamic.sql.render.RenderingStrategies; +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; +import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper; + +public class MyService { + public List> generalSearch() { + GeneralMapper mapper = getGeneralMapper(); // not shown + + SelectStatementProvider selectStatement = select(id, description) + .from(foo) + .where(description. isLike("%bar%")) + .build() + .render(RenderingStrategies.MYBATIS3); + return mapper.selectManyMappedRows(selectStatement); + } +} +``` + +As you can see, the method returns a List of Maps containing the row values. The Map key will be the column name as +returned from the database (typically in upper case), and the column value as returned from the `ResultSet.getObject()`. +See your JDBC driver's documentation for details about how SQL types are mapped to Java types to determine the data type +for your specific database. + +This method works well, but usually it is better to marshal the result set into actual objects. This can be accomplished +as follows: + +```java +package foo.service; +import static org.mybatis.dynamic.sql.SqlBuilder.*; + +import java.util.List; +import org.mybatis.dynamic.sql.render.RenderingStrategies; +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; +import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper; + +public class MyService { + public List generalSearch() { + GeneralMapper mapper = getGeneralMapper(); // not shown + + SelectStatementProvider selectStatement = select(id, description) + .from(foo) + .where(description. isLike("%bar%")) + .build() + .render(RenderingStrategies.MYBATIS3); + return mapper.selectMany(selectStatement, m -> { + TableCode tc = new TableCode(); + tc.setId((Integer) m.get("ID")); + tc.setDescription((String) m.get("DESCRIPTION")); + return tc; + }); + } +} +``` + +With this method you can centralize all the database specific operations in a single method. + +If you only have a single column in the result set, the general mapper provides methods to retrieve the value directly. +For example: + +```java +package foo.service; +import static org.mybatis.dynamic.sql.SqlBuilder.*; + +import org.mybatis.dynamic.sql.render.RenderingStrategies; +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; +import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper; + +public class MyService { + public Long getCount() { + GeneralMapper mapper = getGeneralMapper(); // not shown + + SelectStatementProvider selectStatement = countFrom(foo) + .where(description. isLike("%bar%")) + .build() + .render(RenderingStrategies.MYBATIS3); + return mapper.selectOneLong(selectStatement); + } +} +``` + ## 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. diff --git a/src/test/java/examples/animal/data/AnimalDataMapper.java b/src/test/java/examples/animal/data/AnimalDataMapper.java index 190d73b58..705f03ca6 100644 --- a/src/test/java/examples/animal/data/AnimalDataMapper.java +++ b/src/test/java/examples/animal/data/AnimalDataMapper.java @@ -16,7 +16,6 @@ package examples.animal.data; import java.util.List; -import java.util.Map; import org.apache.ibatis.annotations.DeleteProvider; import org.apache.ibatis.annotations.InsertProvider; @@ -51,15 +50,6 @@ public interface AnimalDataMapper { @ResultMap("AnimalDataResult") AnimalData selectOne(SelectStatementProvider selectStatement); - @SelectProvider(type=SqlProviderAdapter.class, method="select") - List> generalSelect(SelectStatementProvider selectStatement); - - @SelectProvider(type=SqlProviderAdapter.class, method="select") - Long selectALong(SelectStatementProvider selectStatement); - - @SelectProvider(type=SqlProviderAdapter.class, method="select") - Double selectADouble(SelectStatementProvider selectStatement); - @DeleteProvider(type=SqlProviderAdapter.class, method="delete") int delete(DeleteStatementProvider deleteStatement); diff --git a/src/test/java/examples/animal/data/AnimalDataTest.java b/src/test/java/examples/animal/data/AnimalDataTest.java index 222825bd3..bbcd74e4a 100644 --- a/src/test/java/examples/animal/data/AnimalDataTest.java +++ b/src/test/java/examples/animal/data/AnimalDataTest.java @@ -58,6 +58,7 @@ import org.mybatis.dynamic.sql.select.SelectModel; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; +import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper; import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; import org.mybatis.dynamic.sql.where.condition.IsIn; import org.mybatis.dynamic.sql.where.condition.IsNotIn; @@ -84,6 +85,7 @@ void setup() throws Exception { Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); Configuration config = new Configuration(environment); config.addMapper(AnimalDataMapper.class); + config.addMapper(GeneralMapper.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); } @@ -123,13 +125,13 @@ void testSelectAllRowsWithOrder() { @Test void testSelectAllRowsAllColumns() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(animalData.allColumns()) .from(animalData) .orderBy(id.descending()) .build() .render(RenderingStrategies.MYBATIS3); - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select * from AnimalData order by id DESC"), () -> assertThat(animals).hasSize(65), @@ -764,7 +766,7 @@ void testLikeCaseInsensitive() { @Test void testLikeLowerCase() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, lower(animalName).as("AnimalName"), bodyWeight, brainWeight) .from(animalData) @@ -772,7 +774,7 @@ void testLikeLowerCase() { .build() .render(RenderingStrategies.MYBATIS3); - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(animals).hasSize(2), @@ -785,7 +787,7 @@ void testLikeLowerCase() { @Test void testLikeUpperCase() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, upper(animalName).as("animalname"), bodyWeight, brainWeight) .from(animalData) @@ -793,7 +795,7 @@ void testLikeUpperCase() { .build() .render(RenderingStrategies.MYBATIS3); - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(animals).hasSize(2), @@ -806,7 +808,7 @@ void testLikeUpperCase() { @Test void testNumericConstant() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, constant("3").as("some_number")) .from(animalData, "a") @@ -818,7 +820,7 @@ void testNumericConstant() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -836,7 +838,7 @@ void testNumericConstant() { @Test void testStringConstant() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, stringConstant("fred").as("some_string")) .from(animalData, "a") @@ -848,7 +850,7 @@ void testStringConstant() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -866,7 +868,7 @@ void testStringConstant() { @Test void testDeprecatedAdd() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, DeprecatedAdd.of(bodyWeight, brainWeight).as("calculated_weight")) .from(animalData, "a") @@ -878,7 +880,7 @@ void testDeprecatedAdd() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -896,7 +898,7 @@ void testDeprecatedAdd() { @Test void testAdd() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, add(bodyWeight, brainWeight).as("calculated_weight")) .from(animalData, "a") @@ -908,7 +910,7 @@ void testAdd() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -926,7 +928,7 @@ void testAdd() { @Test void testAddConstant() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, add(bodyWeight, constant("22"), constant("33")).as("calculated_weight")) .from(animalData, "a") @@ -938,7 +940,7 @@ void testAddConstant() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -956,7 +958,7 @@ void testAddConstant() { @Test void testAddConstantWithConstantFirst() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, add(constant("22"), bodyWeight, constant("33")).as("calculated_weight")) .from(animalData, "a") @@ -968,7 +970,7 @@ void testAddConstantWithConstantFirst() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -986,7 +988,7 @@ void testAddConstantWithConstantFirst() { @Test void testConcatenate() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, concatenate(animalName, stringConstant(" - The Legend")).as("display_name")) .from(animalData, "a") @@ -998,7 +1000,7 @@ void testConcatenate() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -1013,7 +1015,7 @@ void testConcatenate() { @Test void testConcatenateConstantFirst() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, concatenate(stringConstant("Name: "), animalName).as("display_name")) .from(animalData, "a") @@ -1025,7 +1027,7 @@ void testConcatenateConstantFirst() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -1040,7 +1042,7 @@ void testConcatenateConstantFirst() { @Test void testDivide() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, divide(bodyWeight, brainWeight).as("calculated_weight")) .from(animalData, "a") @@ -1052,7 +1054,7 @@ void testDivide() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -1070,7 +1072,7 @@ void testDivide() { @Test void testDivideConstant() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, divide(bodyWeight, constant("10.0")).as("calculated_weight")) .from(animalData, "a") @@ -1082,7 +1084,7 @@ void testDivideConstant() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -1100,7 +1102,7 @@ void testDivideConstant() { @Test void testMultiply() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, multiply(bodyWeight, brainWeight).as("calculated_weight")) .from(animalData, "a") @@ -1112,7 +1114,7 @@ void testMultiply() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -1130,7 +1132,7 @@ void testMultiply() { @Test void testMultiplyConstant() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, multiply(bodyWeight, constant("2.0")).as("calculated_weight")) .from(animalData, "a") @@ -1142,7 +1144,7 @@ void testMultiplyConstant() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -1160,7 +1162,7 @@ void testMultiplyConstant() { @Test void testSubtract() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, subtract(bodyWeight, brainWeight).as("calculated_weight")) .from(animalData, "a") @@ -1172,7 +1174,7 @@ void testSubtract() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -1190,7 +1192,7 @@ void testSubtract() { @Test void testSubtractConstant() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, subtract(bodyWeight, constant("5.5")).as("calculated_weight")) .from(animalData, "a") @@ -1202,7 +1204,7 @@ void testSubtractConstant() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -1220,7 +1222,7 @@ void testSubtractConstant() { @Test void testGeneralOperator() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, applyOperator("-", bodyWeight, brainWeight).as("calculated_weight")) .from(animalData, "a") @@ -1232,7 +1234,7 @@ void testGeneralOperator() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -1250,7 +1252,7 @@ void testGeneralOperator() { @Test void testComplexExpression() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(id, animalName, add(multiply(bodyWeight, constant("5.5")), subtract(brainWeight, constant("2"))).as("calculated_weight")) .from(animalData, "a") @@ -1262,7 +1264,7 @@ void testComplexExpression() { + "from AnimalData a " + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}"; - List> animals = mapper.generalSelect(selectStatement); + List> animals = mapper.selectManyMappedRows(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected), @@ -1609,14 +1611,14 @@ void testOrderByWithFullClause() { @Test void testCount() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(count().as("total")) .from(animalData, "a") .build() .render(RenderingStrategies.MYBATIS3); - Long count = mapper.selectALong(selectStatement); + Long count = mapper.selectOneLong(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select count(*) as total from AnimalData a"), @@ -1628,14 +1630,14 @@ void testCount() { @Test void testCountField() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(count(brainWeight).as("total")) .from(animalData, "a") .build() .render(RenderingStrategies.MYBATIS3); - Long count = mapper.selectALong(selectStatement); + Long count = mapper.selectOneLong(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select count(a.brain_weight) as total from AnimalData a"), @@ -1647,14 +1649,14 @@ void testCountField() { @Test void testCountNoAlias() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(count()) .from(animalData) .build() .render(RenderingStrategies.MYBATIS3); - Long count = mapper.selectALong(selectStatement); + Long count = mapper.selectOneLong(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select count(*) from AnimalData"), @@ -1666,14 +1668,14 @@ void testCountNoAlias() { @Test void testMax() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(max(brainWeight).as("total")) .from(animalData, "a") .build() .render(RenderingStrategies.MYBATIS3); - Double max = mapper.selectADouble(selectStatement); + Double max = mapper.selectOneDouble(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select max(a.brain_weight) as total from AnimalData a"), @@ -1685,14 +1687,14 @@ void testMax() { @Test void testMaxNoAlias() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(max(brainWeight)) .from(animalData) .build() .render(RenderingStrategies.MYBATIS3); - Double max = mapper.selectADouble(selectStatement); + Double max = mapper.selectOneDouble(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select max(brain_weight) from AnimalData"), @@ -1725,14 +1727,14 @@ void testMaxSubselect() { @Test void testMin() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(min(brainWeight).as("total")) .from(animalData, "a") .build() .render(RenderingStrategies.MYBATIS3); - Double min = mapper.selectADouble(selectStatement); + Double min = mapper.selectOneDouble(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select min(a.brain_weight) as total from AnimalData a"), @@ -1744,14 +1746,14 @@ void testMin() { @Test void testMinNoAlias() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(min(brainWeight)) .from(animalData) .build() .render(RenderingStrategies.MYBATIS3); - Double min = mapper.selectADouble(selectStatement); + Double min = mapper.selectOneDouble(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select min(brain_weight) from AnimalData"), @@ -1807,14 +1809,14 @@ void testMinSubselectNoAlias() { @Test void testAvg() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(avg(brainWeight).as("average")) .from(animalData, "a") .build() .render(RenderingStrategies.MYBATIS3); - Double average = mapper.selectADouble(selectStatement); + Double average = mapper.selectOneDouble(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select avg(a.brain_weight) as average from AnimalData a"), @@ -1826,14 +1828,14 @@ void testAvg() { @Test void testSum() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { - AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(sum(brainWeight).as("total")) .from(animalData) .build() .render(RenderingStrategies.MYBATIS3); - Double total = mapper.selectADouble(selectStatement); + Double total = mapper.selectOneDouble(selectStatement); assertAll( () -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select sum(brain_weight) as total from AnimalData"), diff --git a/src/test/java/examples/animal/data/GeneralMapperTest.java b/src/test/java/examples/animal/data/GeneralMapperTest.java new file mode 100644 index 000000000..d691ddb33 --- /dev/null +++ b/src/test/java/examples/animal/data/GeneralMapperTest.java @@ -0,0 +1,495 @@ +/* + * Copyright 2016-2020 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 examples.animal.data; + +import static examples.animal.data.AnimalDataDynamicSqlSupport.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mybatis.dynamic.sql.SqlBuilder.*; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.math.BigDecimal; +import java.sql.Connection; +import java.sql.DriverManager; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; + +import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; +import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.mapping.Environment; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; +import org.assertj.core.data.Percentage; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mybatis.dynamic.sql.render.RenderingStrategies; +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; +import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper; + +class GeneralMapperTest { + + private static final String JDBC_URL = "jdbc:hsqldb:mem:aname"; + private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver"; + + private SqlSessionFactory sqlSessionFactory; + + @BeforeEach + void setup() throws Exception { + Class.forName(JDBC_DRIVER); + InputStream is = getClass().getResourceAsStream("/examples/animal/data/CreateAnimalData.sql"); + try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) { + ScriptRunner sr = new ScriptRunner(connection); + sr.setLogWriter(null); + sr.runScript(new InputStreamReader(is)); + } + + UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", ""); + Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); + Configuration config = new Configuration(environment); + config.addMapper(GeneralMapper.class); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); + } + + private final Function, AnimalData> rowMapper = map -> { + AnimalData ad = new AnimalData(); + ad.setId((Integer) map.get("ID")); + ad.setAnimalName((String) map.get("ANIMAL_NAME")); + ad.setBodyWeight((Double) map.get("BODY_WEIGHT")); + ad.setBrainWeight((Double) map.get("BRAIN_WEIGHT")); + return ad; + }; + + @Test + void testGeneralSelectOne() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id, animalName) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + Map row = mapper.selectOneMappedRow(selectStatement); + + assertThat(row).containsEntry("ID", 1); + assertThat(row).containsEntry("ANIMAL_NAME", "Lesser short-tailed shrew"); + } + } + + @Test + void testGeneralSelectOneWithRowMapper() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + + AnimalData animal = mapper.selectOne(selectStatement, rowMapper); + + assertThat(animal.getId()).isEqualTo(1); + assertThat(animal.getAnimalName()).isEqualTo("Lesser short-tailed shrew"); + assertThat(animal.getBodyWeight()).isEqualTo(0.14); + assertThat(animal.getBrainWeight()).isEqualTo(0.005); + } + } + + @Test + void testGeneralSelectMany() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id, animalName) + .from(animalData) + .where(id, isIn(1, 2)) + .orderBy(id) + .build() + .render(RenderingStrategies.MYBATIS3); + List> rows = mapper.selectManyMappedRows(selectStatement); + + assertThat(rows).hasSize(2); + + assertThat(rows.get(0)).containsEntry("ID", 1); + assertThat(rows.get(0)).containsEntry("ANIMAL_NAME", "Lesser short-tailed shrew"); + assertThat(rows.get(1)).containsEntry("ID", 2); + assertThat(rows.get(1)).containsEntry("ANIMAL_NAME", "Little brown bat"); + } + } + + @Test + void testGeneralSelectManyWithRowMapper() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight) + .from(animalData) + .where(id, isIn(1, 2)) + .orderBy(id) + .build() + .render(RenderingStrategies.MYBATIS3); + List rows = mapper.selectMany(selectStatement, rowMapper); + + assertThat(rows).hasSize(2); + + assertThat(rows.get(0).getId()).isEqualTo(1); + assertThat(rows.get(0).getAnimalName()).isEqualTo("Lesser short-tailed shrew"); + assertThat(rows.get(0).getBodyWeight()).isEqualTo(0.14); + assertThat(rows.get(0).getBrainWeight()).isEqualTo(0.005); + assertThat(rows.get(1).getId()).isEqualTo(2); + assertThat(rows.get(1).getAnimalName()).isEqualTo("Little brown bat"); + assertThat(rows.get(1).getBodyWeight()).isEqualTo(0.25); + assertThat(rows.get(1).getBrainWeight()).isEqualTo(0.01); + } + } + + @Test + void testSelectOneBigDecimal() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(bodyWeight) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + + BigDecimal bodyWeight = mapper.selectOneBigDecimal(selectStatement); + + assertThat(bodyWeight).isCloseTo(new BigDecimal("0.14"), Percentage.withPercentage(0.01)); + } + } + + @Test + void testSelectOptionalBigDecimalPresent() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(bodyWeight) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Optional bodyWeight = mapper.selectOptionalBigDecimal(selectStatement); + + assertThat(bodyWeight).hasValueSatisfying(bw -> assertThat(bw).isCloseTo(new BigDecimal("0.14"), Percentage.withPercentage(0.01))); + } + } + + @Test + void testSelectOptionalBigDecimalMissing() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(bodyWeight) + .from(animalData) + .where(id, isEqualTo(1000)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Optional bodyWeight = mapper.selectOptionalBigDecimal(selectStatement); + + assertThat(bodyWeight).isEmpty(); + } + } + + @Test + void testSelectManyBigDecimals() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(bodyWeight) + .from(animalData) + .where(id, isIn(1, 2)) + .orderBy(id) + .build() + .render(RenderingStrategies.MYBATIS3); + + List bodyWeights = mapper.selectManyBigDecimals(selectStatement); + + assertThat(bodyWeights).hasSize(2); + assertThat(bodyWeights.get(0)).isCloseTo(new BigDecimal("0.14"), Percentage.withPercentage(0.01)); + assertThat(bodyWeights.get(1)).isCloseTo(new BigDecimal("0.25"), Percentage.withPercentage(0.01)); + } + } + + @Test + void testSelectOneDouble() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(bodyWeight) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Double bodyWeight = mapper.selectOneDouble(selectStatement); + + assertThat(bodyWeight).isEqualTo(0.14); + } + } + + @Test + void testSelectOptionalDoublePresent() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(bodyWeight) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Optional bodyWeight = mapper.selectOptionalDouble(selectStatement); + + assertThat(bodyWeight).hasValueSatisfying(bw -> assertThat(bw).isEqualTo(0.14)); + } + } + + @Test + void testSelectOptionalDoubleMissing() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(bodyWeight) + .from(animalData) + .where(id, isEqualTo(1000)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Optional bodyWeight = mapper.selectOptionalDouble(selectStatement); + + assertThat(bodyWeight).isEmpty(); + } + } + + @Test + void testSelectManyDoubles() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(bodyWeight) + .from(animalData) + .where(id, isIn(1, 2)) + .orderBy(id) + .build() + .render(RenderingStrategies.MYBATIS3); + + List bodyWeights = mapper.selectManyDoubles(selectStatement); + + assertThat(bodyWeights).hasSize(2); + assertThat(bodyWeights.get(0)).isEqualTo(0.14); + assertThat(bodyWeights.get(1)).isEqualTo(0.25); + } + } + + @Test + void testSelectOneInteger() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Integer id = mapper.selectOneInteger(selectStatement); + + assertThat(id).isEqualTo(1); + } + } + + @Test + void testSelectOptionalIntegerPresent() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Optional id = mapper.selectOptionalInteger(selectStatement); + + assertThat(id).hasValueSatisfying(i -> assertThat(i).isEqualTo(1)); + } + } + + @Test + void testSelectOptionalIntegerMissing() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id) + .from(animalData) + .where(id, isEqualTo(1000)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Optional id = mapper.selectOptionalInteger(selectStatement); + + assertThat(id).isEmpty(); + } + } + + @Test + void testSelectManyIntegers() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id) + .from(animalData) + .where(id, isIn(1, 2)) + .orderBy(id) + .build() + .render(RenderingStrategies.MYBATIS3); + + List ids = mapper.selectManyIntegers(selectStatement); + + assertThat(ids).hasSize(2); + assertThat(ids.get(0)).isEqualTo(1); + assertThat(ids.get(1)).isEqualTo(2); + } + } + + @Test + void testSelectOneLong() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Long id = mapper.selectOneLong(selectStatement); + + assertThat(id).isEqualTo(1); + } + } + + @Test + void testSelectOptionalLongPresent() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Optional id = mapper.selectOptionalLong(selectStatement); + + assertThat(id).hasValueSatisfying(i -> assertThat(i).isEqualTo(1)); + } + } + + @Test + void testSelectOptionalLongMissing() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id) + .from(animalData) + .where(id, isEqualTo(1000)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Optional id = mapper.selectOptionalLong(selectStatement); + + assertThat(id).isEmpty(); + } + } + + @Test + void testSelectManyLongs() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(id) + .from(animalData) + .where(id, isIn(1, 2)) + .orderBy(id) + .build() + .render(RenderingStrategies.MYBATIS3); + + List ids = mapper.selectManyLongs(selectStatement); + + assertThat(ids).hasSize(2); + assertThat(ids.get(0)).isEqualTo(1); + assertThat(ids.get(1)).isEqualTo(2); + } + } + + @Test + void testSelectOneString() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(animalName) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + + String name = mapper.selectOneString(selectStatement); + + assertThat(name).isEqualTo("Lesser short-tailed shrew"); + } + } + + @Test + void testSelectOptionalStringPresent() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(animalName) + .from(animalData) + .where(id, isEqualTo(1)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Optional name = mapper.selectOptionalString(selectStatement); + + assertThat(name).hasValueSatisfying(n -> assertThat(n).isEqualTo("Lesser short-tailed shrew")); + } + } + + @Test + void testSelectOptionalStringMissing() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(animalName) + .from(animalData) + .where(id, isEqualTo(1000)) + .build() + .render(RenderingStrategies.MYBATIS3); + + Optional name = mapper.selectOptionalString(selectStatement); + + assertThat(name).isEmpty(); + } + } + + @Test + void testSelectManyStrings() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class); + SelectStatementProvider selectStatement = select(animalName) + .from(animalData) + .where(id, isIn(1, 2)) + .orderBy(id) + .build() + .render(RenderingStrategies.MYBATIS3); + + List names = mapper.selectManyStrings(selectStatement); + + assertThat(names).hasSize(2); + assertThat(names.get(0)).isEqualTo("Lesser short-tailed shrew"); + assertThat(names.get(1)).isEqualTo("Little brown bat"); + } + } +} diff --git a/src/test/java/examples/custom_render/CustomRenderingTest.java b/src/test/java/examples/custom_render/CustomRenderingTest.java index 1e6575bc1..1c57e4b92 100644 --- a/src/test/java/examples/custom_render/CustomRenderingTest.java +++ b/src/test/java/examples/custom_render/CustomRenderingTest.java @@ -50,6 +50,7 @@ import org.mybatis.dynamic.sql.render.RenderingStrategies; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; +import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; @@ -57,7 +58,7 @@ class CustomRenderingTest { @Container - private static PgContainer postgres = new PgContainer("examples/custom_render/dbInit.sql"); + private static final PgContainer postgres = new PgContainer("examples/custom_render/dbInit.sql"); private static SqlSessionFactory sqlSessionFactory; @@ -68,6 +69,7 @@ static void setUp() { postgres.getUnpooledDataSource()); configuration.setEnvironment(environment); configuration.addMapper(JsonTestMapper.class); + configuration.addMapper(GeneralMapper.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); } @@ -321,8 +323,10 @@ void testDefererence() { Optional record = mapper.selectOne(selectStatement); - assertThat(record).hasValueSatisfying( c -> - c.getInfo().equals("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}")); + assertThat(record).hasValueSatisfying( r -> + assertThat(r.getInfo()) + .isEqualTo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}") + ); } } @@ -364,7 +368,7 @@ void testDefererence2() { assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> records = mapper.generalSelect(selectStatement); + List> records = mapper.selectManyMappedRows(selectStatement); assertThat(records).hasSize(1); assertThat(records.get(0)).containsEntry("firstname", "Wilma"); } diff --git a/src/test/java/examples/custom_render/JsonTestMapper.java b/src/test/java/examples/custom_render/JsonTestMapper.java index bc6417d16..bc5f782bc 100644 --- a/src/test/java/examples/custom_render/JsonTestMapper.java +++ b/src/test/java/examples/custom_render/JsonTestMapper.java @@ -16,7 +16,6 @@ package examples.custom_render; import java.util.List; -import java.util.Map; import java.util.Optional; import org.apache.ibatis.annotations.DeleteProvider; @@ -33,11 +32,9 @@ import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; import org.mybatis.dynamic.sql.util.SqlProviderAdapter; +import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper; -public interface JsonTestMapper { - @SelectProvider(type = SqlProviderAdapter.class, method = "select") - List> generalSelect(SelectStatementProvider selectStatement); - +public interface JsonTestMapper extends GeneralMapper { @SelectProvider(type = SqlProviderAdapter.class, method = "select") @Results(id = "JsonTestResult", value = { @Result(column = "id", property = "id", id = true), diff --git a/src/test/java/examples/groupby/GroupByMapper.java b/src/test/java/examples/groupby/GroupByMapper.java deleted file mode 100644 index a25bfedf3..000000000 --- a/src/test/java/examples/groupby/GroupByMapper.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2016-2020 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 examples.groupby; - -import java.util.List; -import java.util.Map; - -import org.apache.ibatis.annotations.SelectProvider; -import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; -import org.mybatis.dynamic.sql.util.SqlProviderAdapter; - -public interface GroupByMapper { - - @SelectProvider(type=SqlProviderAdapter.class, method="select") - List> generalSelect(SelectStatementProvider selectStatement); -} diff --git a/src/test/java/examples/groupby/GroupByTest.java b/src/test/java/examples/groupby/GroupByTest.java index d61f9ab4e..06d131b94 100644 --- a/src/test/java/examples/groupby/GroupByTest.java +++ b/src/test/java/examples/groupby/GroupByTest.java @@ -40,6 +40,7 @@ import org.junit.jupiter.api.Test; import org.mybatis.dynamic.sql.render.RenderingStrategies; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; +import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper; class GroupByTest { @@ -61,14 +62,14 @@ 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(GroupByMapper.class); + config.addMapper(GeneralMapper.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); } @Test void testBasicGroupBy() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(gender, count()) .from(person) @@ -79,7 +80,7 @@ void testBasicGroupBy() { String expected = "select gender, count(*) from Person group by gender"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(2); Map row = rows.get(0); assertThat(row).containsEntry("GENDER", "Male"); @@ -94,7 +95,7 @@ void testBasicGroupBy() { @Test void testBasicGroupByWithAggregateAlias() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(gender, count().as("count")) .from(person) @@ -105,7 +106,7 @@ void testBasicGroupByWithAggregateAlias() { String expected = "select gender, count(*) as count from Person group by gender"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(2); Map row = rows.get(0); assertThat(row).containsEntry("GENDER", "Male"); @@ -120,7 +121,7 @@ void testBasicGroupByWithAggregateAlias() { @Test void testGroupByAfterJoin() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(lastName, streetAddress, count().as("count")) .from(person, "p").join(address, "a").on(person.addressId, equalTo(address.id)) @@ -133,7 +134,7 @@ void testGroupByAfterJoin() { " group by p.last_name, a.street_address"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(2); Map row = rows.get(0); assertThat(row).containsEntry("LAST_NAME", "Flintstone"); @@ -150,7 +151,7 @@ void testGroupByAfterJoin() { @Test void testUnionAfterJoin() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(lastName, firstName, streetAddress) .from(person, "p").join(address, "a").on(person.addressId, equalTo(address.id)) @@ -169,7 +170,7 @@ void testUnionAfterJoin() { " order by last_name, first_name"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(10); Map row = rows.get(0); assertThat(row).containsEntry("LAST_NAME", "Flintstone"); @@ -186,7 +187,7 @@ void testUnionAfterJoin() { @Test void testUnionAllAfterJoin() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(lastName, firstName, streetAddress) .from(person, "p").join(address, "a").on(person.addressId, equalTo(address.id)) @@ -205,7 +206,7 @@ void testUnionAllAfterJoin() { " order by last_name, first_name"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(10); Map row = rows.get(0); assertThat(row).containsEntry("LAST_NAME", "Flintstone"); @@ -222,7 +223,7 @@ void testUnionAllAfterJoin() { @Test void testUnionAfterGroupBy() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(stringConstant("Gender"), gender.as("value"), count().as("count")) .from(person) @@ -239,7 +240,7 @@ void testUnionAfterGroupBy() { " select 'Last Name', last_name as value, count(*) as count from Person group by last_name"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(4); Map row = rows.get(0); assertThat(row).containsEntry("C1", "Gender "); @@ -266,7 +267,7 @@ void testUnionAfterGroupBy() { @Test void testUnionAllAfterGroupBy() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(stringConstant("Gender"), gender.as("value"), count().as("count")) .from(person) @@ -283,7 +284,7 @@ void testUnionAllAfterGroupBy() { " select 'Last Name', last_name as value, count(*) as count from Person group by last_name"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(4); Map row = rows.get(0); assertThat(row).containsEntry("C1", "Gender "); @@ -310,7 +311,7 @@ void testUnionAllAfterGroupBy() { @Test void testBasicGroupByOrderByWithAggregateAlias() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(gender, count().as("count")) .from(person) @@ -322,7 +323,7 @@ void testBasicGroupByOrderByWithAggregateAlias() { String expected = "select gender, count(*) as count from Person group by gender order by gender"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(2); Map row = rows.get(0); assertThat(row).containsEntry("GENDER", "Female"); @@ -337,7 +338,7 @@ void testBasicGroupByOrderByWithAggregateAlias() { @Test void testBasicGroupByOrderByWithCalculatedColumnAndTableAlias() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(substring(gender, 1, 1).as("ShortGender"), avg(age).as("AverageAge")) .from(person, "a") @@ -349,7 +350,7 @@ void testBasicGroupByOrderByWithCalculatedColumnAndTableAlias() { String expected = "select substring(a.gender, 1, 1) as ShortGender, avg(a.age) as AverageAge from Person a group by substring(a.gender, 1, 1) order by ShortGender DESC"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(2); Map row = rows.get(0); assertThat(row).containsEntry("SHORTGENDER", "M"); @@ -364,7 +365,7 @@ void testBasicGroupByOrderByWithCalculatedColumnAndTableAlias() { @Test void testGroupByAfterWhere() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(lastName, count().as("count")) .from(person, "a") @@ -376,7 +377,7 @@ void testGroupByAfterWhere() { String expected = "select a.last_name, count(*) as count from Person a where a.gender = #{parameters.p1,jdbcType=VARCHAR} group by a.last_name"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(2); Map row = rows.get(0); assertThat(row).containsEntry("LAST_NAME", "Flintstone"); @@ -391,7 +392,7 @@ void testGroupByAfterWhere() { @Test void testLimitAndOffsetAfterGroupBy() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(lastName, count().as("count")) .from(person) @@ -404,7 +405,7 @@ void testLimitAndOffsetAfterGroupBy() { String expected = "select last_name, count(*) as count from Person group by last_name limit #{parameters.p1} offset #{parameters.p2}"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(1); Map row = rows.get(0); assertThat(row).containsEntry("LAST_NAME", "Rubble"); @@ -415,7 +416,7 @@ void testLimitAndOffsetAfterGroupBy() { @Test void testLimitOnlyAfterGroupBy() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(lastName, count().as("count")) .from(person) @@ -427,7 +428,7 @@ void testLimitOnlyAfterGroupBy() { String expected = "select last_name, count(*) as count from Person group by last_name limit #{parameters.p1}"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(1); Map row = rows.get(0); assertThat(row).containsEntry("LAST_NAME", "Flintstone"); @@ -438,7 +439,7 @@ void testLimitOnlyAfterGroupBy() { @Test void testOffsetOnlyAfterGroupBy() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(lastName, count().as("count")) .from(person) @@ -450,7 +451,7 @@ void testOffsetOnlyAfterGroupBy() { String expected = "select last_name, count(*) as count from Person group by last_name offset #{parameters.p1} rows"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(1); Map row = rows.get(0); assertThat(row).containsEntry("LAST_NAME", "Rubble"); @@ -461,7 +462,7 @@ void testOffsetOnlyAfterGroupBy() { @Test void testOffsetAndFetchFirstAfterGroupBy() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(lastName, count().as("count")) .from(person) @@ -474,7 +475,7 @@ void testOffsetAndFetchFirstAfterGroupBy() { String expected = "select last_name, count(*) as count from Person group by last_name offset #{parameters.p1} rows fetch first #{parameters.p2} rows only"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(1); Map row = rows.get(0); assertThat(row).containsEntry("LAST_NAME", "Rubble"); @@ -485,7 +486,7 @@ void testOffsetAndFetchFirstAfterGroupBy() { @Test void testFetchFirstOnlyAfterGroupBy() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(lastName, count().as("count")) .from(person) @@ -497,7 +498,7 @@ void testFetchFirstOnlyAfterGroupBy() { String expected = "select last_name, count(*) as count from Person group by last_name fetch first #{parameters.p1} rows only"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(1); Map row = rows.get(0); assertThat(row).containsEntry("LAST_NAME", "Flintstone"); @@ -508,7 +509,7 @@ void testFetchFirstOnlyAfterGroupBy() { @Test void testCountDistinct() { try (SqlSession session = sqlSessionFactory.openSession()) { - GroupByMapper mapper = session.getMapper(GroupByMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(countDistinct(lastName).as("count")) .from(person) @@ -518,7 +519,7 @@ void testCountDistinct() { String expected = "select count(distinct last_name) as count from Person"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(1); Map row = rows.get(0); assertThat(row).containsEntry("COUNT", 2L); diff --git a/src/test/java/examples/joins/JoinMapper.java b/src/test/java/examples/joins/JoinMapper.java index b3c63f51d..95d64d6ed 100644 --- a/src/test/java/examples/joins/JoinMapper.java +++ b/src/test/java/examples/joins/JoinMapper.java @@ -16,7 +16,6 @@ package examples.joins; import java.util.List; -import java.util.Map; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.ResultMap; @@ -30,9 +29,6 @@ public interface JoinMapper { @ResultMap("SimpleJoinResult") List selectMany(SelectStatementProvider selectStatement); - @SelectProvider(type=SqlProviderAdapter.class, method="select") - List> generalSelect(SelectStatementProvider selectStatement); - @SelectProvider(type=SqlProviderAdapter.class, method="select") @Results ({ @Result(column="user_id", property="userId"), diff --git a/src/test/java/examples/joins/JoinMapperTest.java b/src/test/java/examples/joins/JoinMapperTest.java index 8da001016..f5f816bed 100644 --- a/src/test/java/examples/joins/JoinMapperTest.java +++ b/src/test/java/examples/joins/JoinMapperTest.java @@ -43,6 +43,7 @@ import org.junit.jupiter.api.Test; import org.mybatis.dynamic.sql.render.RenderingStrategies; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; +import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper; class JoinMapperTest { @@ -65,6 +66,7 @@ void setup() throws Exception { Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); Configuration config = new Configuration(environment); config.addMapper(JoinMapper.class); + config.addMapper(GeneralMapper.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); } @@ -385,7 +387,7 @@ void testMultibleTableJoinNoAliasWithOrderBy() { @Test void testRightJoin() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(orderLine, "ol") @@ -399,7 +401,7 @@ void testRightJoin() { + " order by item_id"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(5); Map row = rows.get(2); @@ -419,7 +421,7 @@ void testRightJoin() { @Test void testRightJoin2() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(orderMaster, "om") @@ -435,7 +437,7 @@ void testRightJoin2() { + " order by order_id, item_id"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(5); Map row = rows.get(0); @@ -455,7 +457,7 @@ void testRightJoin2() { @Test void testRightJoin3() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(orderMaster, "om") @@ -471,7 +473,7 @@ void testRightJoin3() { + " order by order_id, item_id"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(5); Map row = rows.get(0); @@ -491,7 +493,7 @@ void testRightJoin3() { @Test void testRightJoinNoAliases() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(orderMaster) @@ -507,7 +509,7 @@ void testRightJoinNoAliases() { + " order by order_id, item_id"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(5); Map row = rows.get(0); @@ -527,7 +529,7 @@ void testRightJoinNoAliases() { @Test void testLeftJoin() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(itemMaster, "im") @@ -541,7 +543,7 @@ void testLeftJoin() { + " order by item_id"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(5); Map row = rows.get(2); @@ -561,7 +563,7 @@ void testLeftJoin() { @Test void testLeftJoin2() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(orderMaster, "om") @@ -577,7 +579,7 @@ void testLeftJoin2() { + " order by order_id, item_id"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(5); Map row = rows.get(2); @@ -597,7 +599,7 @@ void testLeftJoin2() { @Test void testLeftJoin3() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(orderMaster, "om") @@ -613,7 +615,7 @@ void testLeftJoin3() { + " order by order_id, item_id"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(5); Map row = rows.get(2); @@ -633,7 +635,7 @@ void testLeftJoin3() { @Test void testLeftJoinNoAliases() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(orderMaster) @@ -649,7 +651,7 @@ void testLeftJoinNoAliases() { + " order by order_id, item_id"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(5); Map row = rows.get(2); @@ -669,7 +671,7 @@ void testLeftJoinNoAliases() { @Test void testFullJoin() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, orderLine.itemId.as("ol_itemid"), itemMaster.itemId.as("im_itemid"), itemMaster.description) .from(itemMaster, "im") @@ -683,7 +685,7 @@ void testFullJoin() { + " order by im_itemid"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(6); Map row = rows.get(0); @@ -710,7 +712,7 @@ void testFullJoin() { @Test void testFullJoin2() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(orderMaster, "om") @@ -726,7 +728,7 @@ void testFullJoin2() { + " order by order_id, item_id"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(6); Map row = rows.get(0); @@ -752,7 +754,7 @@ void testFullJoin2() { @Test void testFullJoin3() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(orderMaster, "om") @@ -768,7 +770,7 @@ void testFullJoin3() { + " order by order_id, item_id"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(6); Map row = rows.get(0); @@ -794,7 +796,7 @@ void testFullJoin3() { @Test void testFullJoinNoAliases() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(orderMaster) @@ -810,7 +812,7 @@ void testFullJoinNoAliases() { + " order by order_id, item_id"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(6); Map row = rows.get(0); @@ -864,7 +866,7 @@ void testSelf() { @Test void testLimitAndOffsetAfterJoin() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(itemMaster, "im") @@ -879,7 +881,7 @@ void testLimitAndOffsetAfterJoin() { + " limit #{parameters.p1} offset #{parameters.p2}"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(2); Map row = rows.get(0); @@ -899,7 +901,7 @@ void testLimitAndOffsetAfterJoin() { @Test void testLimitOnlyAfterJoin() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(itemMaster, "im") @@ -913,7 +915,7 @@ void testLimitOnlyAfterJoin() { + " limit #{parameters.p1}"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(2); Map row = rows.get(0); @@ -933,7 +935,7 @@ void testLimitOnlyAfterJoin() { @Test void testOffsetOnlyAfterJoin() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(itemMaster, "im") @@ -947,7 +949,7 @@ void testOffsetOnlyAfterJoin() { + " offset #{parameters.p1} rows"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(3); Map row = rows.get(0); @@ -967,7 +969,7 @@ void testOffsetOnlyAfterJoin() { @Test void testOffsetAndFetchFirstAfterJoin() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(itemMaster, "im") @@ -982,7 +984,7 @@ void testOffsetAndFetchFirstAfterJoin() { + " offset #{parameters.p1} rows fetch first #{parameters.p2} rows only"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(2); Map row = rows.get(0); @@ -1002,7 +1004,7 @@ void testOffsetAndFetchFirstAfterJoin() { @Test void testFetchFirstOnlyAfterJoin() { try (SqlSession session = sqlSessionFactory.openSession()) { - JoinMapper mapper = session.getMapper(JoinMapper.class); + GeneralMapper mapper = session.getMapper(GeneralMapper.class); SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description) .from(itemMaster, "im") @@ -1016,7 +1018,7 @@ void testFetchFirstOnlyAfterJoin() { + " fetch first #{parameters.p1} rows only"; assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment); - List> rows = mapper.generalSelect(selectStatement); + List> rows = mapper.selectManyMappedRows(selectStatement); assertThat(rows).hasSize(2); Map row = rows.get(0); diff --git a/src/test/kotlin/examples/kotlin/mybatis3/joins/JoinMapper.kt b/src/test/kotlin/examples/kotlin/mybatis3/joins/JoinMapper.kt index e20ba25b6..58951eff2 100644 --- a/src/test/kotlin/examples/kotlin/mybatis3/joins/JoinMapper.kt +++ b/src/test/kotlin/examples/kotlin/mybatis3/joins/JoinMapper.kt @@ -19,12 +19,10 @@ import org.apache.ibatis.annotations.ResultMap import org.apache.ibatis.annotations.SelectProvider import org.mybatis.dynamic.sql.select.render.SelectStatementProvider import org.mybatis.dynamic.sql.util.SqlProviderAdapter +import org.mybatis.dynamic.sql.util.mybatis3.GeneralMapper -interface JoinMapper { +interface JoinMapper : GeneralMapper { @SelectProvider(type = SqlProviderAdapter::class, method = "select") @ResultMap("SimpleJoinResult") fun selectMany(selectStatement: SelectStatementProvider): List - - @SelectProvider(type = SqlProviderAdapter::class, method = "select") - fun generalSelect(selectStatement: SelectStatementProvider): List> } diff --git a/src/test/kotlin/examples/kotlin/mybatis3/joins/JoinMapperTest.kt b/src/test/kotlin/examples/kotlin/mybatis3/joins/JoinMapperTest.kt index ccef66ea8..271316236 100644 --- a/src/test/kotlin/examples/kotlin/mybatis3/joins/JoinMapperTest.kt +++ b/src/test/kotlin/examples/kotlin/mybatis3/joins/JoinMapperTest.kt @@ -179,26 +179,39 @@ class JoinMapperTest { assertThat(selectStatement.selectStatement).isEqualTo(expectedStatement) - val rows = mapper.generalSelect(selectStatement) + data class OrderDetail (val itemId: Int?, val orderId: Int?, val quantity: Int?, val description: String?) + + val rows = mapper.selectMany(selectStatement) { + OrderDetail( + it["ITEM_ID"] as Int?, + it["ORDER_ID"] as Int?, + it["QUANTITY"] as Int?, + it["DESCRIPTION"] as String? + ) + } assertThat(rows).hasSize(6) - assertThat(rows[0]).containsExactly( - entry("DESCRIPTION", "Catcher Glove"), - entry("ITEM_ID", 55) - ) + with(rows[0]) { + assertThat(itemId).isEqualTo(55) + assertThat(orderId).isNull() + assertThat(quantity).isNull() + assertThat(description).isEqualTo("Catcher Glove") + } - assertThat(rows[3]).containsExactly( - entry("ORDER_ID", 2), - entry("QUANTITY", 6) - ) + with(rows[3]) { + assertThat(itemId).isNull() + assertThat(orderId).isEqualTo(2) + assertThat(quantity).isEqualTo(6) + assertThat(description).isNull() + } - assertThat(rows[5]).containsExactly( - entry("ORDER_ID", 2), - entry("QUANTITY", 1), - entry("DESCRIPTION", "Outfield Glove"), - entry("ITEM_ID", 44) - ) + with(rows[5]) { + assertThat(itemId).isEqualTo(44) + assertThat(orderId).isEqualTo(2) + assertThat(quantity).isEqualTo(1) + assertThat(description).isEqualTo("Outfield Glove") + } } } @@ -225,7 +238,7 @@ class JoinMapperTest { assertThat(selectStatement.selectStatement).isEqualTo(expectedStatement) - val rows = mapper.generalSelect(selectStatement) + val rows = mapper.selectManyMappedRows(selectStatement) assertThat(rows).hasSize(6) @@ -271,7 +284,7 @@ class JoinMapperTest { assertThat(selectStatement.selectStatement).isEqualTo(expectedStatement) - val rows = mapper.generalSelect(selectStatement) + val rows = mapper.selectManyMappedRows(selectStatement) assertThat(rows).hasSize(5) @@ -312,7 +325,7 @@ class JoinMapperTest { assertThat(selectStatement.selectStatement).isEqualTo(expectedStatement) - val rows = mapper.generalSelect(selectStatement) + val rows = mapper.selectManyMappedRows(selectStatement) assertThat(rows).hasSize(5) @@ -353,7 +366,7 @@ class JoinMapperTest { assertThat(selectStatement.selectStatement).isEqualTo(expectedStatement) - val rows = mapper.generalSelect(selectStatement) + val rows = mapper.selectManyMappedRows(selectStatement) assertThat(rows).hasSize(5) @@ -394,7 +407,7 @@ class JoinMapperTest { assertThat(selectStatement.selectStatement).isEqualTo(expectedStatement) - val rows = mapper.generalSelect(selectStatement) + val rows = mapper.selectManyMappedRows(selectStatement) assertThat(rows).hasSize(5)