Skip to content

Commit 5f2cc70

Browse files
authored
Merge pull request #23 from jeffgbutler/master
Small Test Improvements
2 parents 41758b6 + 49ff0c8 commit 5f2cc70

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

src/test/java/examples/simple/LastNameTypeHandler.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ public LastName getResult(CallableStatement cs, int columnIndex) throws SQLExcep
4646
}
4747

4848
private LastName toLastName(String s) {
49-
LastName lastName = null;
50-
51-
if (s != null) {
52-
return null;
53-
}
54-
55-
return lastName;
49+
return s == null ? null : LastName.of(s);
5650
}
5751
}

src/test/java/examples/simple/SimpleTableAnnotatedMapper.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.mybatis.dynamic.sql.SqlBuilder.*;
2020

2121
import java.util.List;
22+
import java.util.function.Function;
2223

2324
import org.apache.ibatis.annotations.DeleteProvider;
2425
import org.apache.ibatis.annotations.InsertProvider;
@@ -28,6 +29,7 @@
2829
import org.apache.ibatis.annotations.Results;
2930
import org.apache.ibatis.annotations.SelectProvider;
3031
import org.apache.ibatis.annotations.UpdateProvider;
32+
import org.apache.ibatis.session.RowBounds;
3133
import org.apache.ibatis.type.JdbcType;
3234
import org.mybatis.dynamic.sql.SqlBuilder;
3335
import org.mybatis.dynamic.sql.delete.DeleteDSL;
@@ -44,6 +46,11 @@
4446
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
4547
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
4648

49+
/**
50+
*
51+
* Note: this is the canonical mapper and represents the desired output for MyBatis Generator
52+
*
53+
*/
4754
@Mapper
4855
public interface SimpleTableAnnotatedMapper {
4956

@@ -64,6 +71,10 @@ public interface SimpleTableAnnotatedMapper {
6471
})
6572
List<SimpleTableRecord> selectMany(SelectStatementProvider selectStatement);
6673

74+
@SelectProvider(type=SqlProviderAdapter.class, method="select")
75+
@ResultMap("SimpleTableResult")
76+
List<SimpleTableRecord> selectManyWithRowbounds(SelectStatementProvider selectStatement, RowBounds rowBounds);
77+
6778
@SelectProvider(type=SqlProviderAdapter.class, method="select")
6879
@ResultMap("SimpleTableResult")
6980
SimpleTableRecord selectOne(SelectStatementProvider selectStatement);
@@ -74,6 +85,10 @@ public interface SimpleTableAnnotatedMapper {
7485
@SelectProvider(type=SqlProviderAdapter.class, method="select")
7586
long count(SelectStatementProvider selectStatement);
7687

88+
default Function<SelectStatementProvider, List<SimpleTableRecord>> selectManyWithRowbounds(RowBounds rowBounds) {
89+
return selectStatement -> selectManyWithRowbounds(selectStatement, rowBounds);
90+
}
91+
7792
default QueryExpressionDSL<MyBatis3SelectModelAdapter<Long>> countByExample() {
7893
return SelectDSL.selectWithMapper(this::count, SqlBuilder.count())
7994
.from(simpleTable);
@@ -121,11 +136,21 @@ default QueryExpressionDSL<MyBatis3SelectModelAdapter<List<SimpleTableRecord>>>
121136
.from(simpleTable);
122137
}
123138

139+
default QueryExpressionDSL<MyBatis3SelectModelAdapter<List<SimpleTableRecord>>> selectByExample(RowBounds rowBounds) {
140+
return SelectDSL.selectWithMapper(selectManyWithRowbounds(rowBounds), id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
141+
.from(simpleTable);
142+
}
143+
124144
default QueryExpressionDSL<MyBatis3SelectModelAdapter<List<SimpleTableRecord>>> selectDistinctByExample() {
125145
return SelectDSL.selectDistinctWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
126146
.from(simpleTable);
127147
}
128148

149+
default QueryExpressionDSL<MyBatis3SelectModelAdapter<List<SimpleTableRecord>>> selectDistinctByExample(RowBounds rowBounds) {
150+
return SelectDSL.selectDistinctWithMapper(selectManyWithRowbounds(rowBounds), id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
151+
.from(simpleTable);
152+
}
153+
129154
default SimpleTableRecord selectByPrimaryKey(Integer id_) {
130155
return SelectDSL.selectWithMapper(this::selectOne, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
131156
.from(simpleTable)
@@ -156,7 +181,6 @@ default UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> updateByExampleSelective(
156181

157182
default int updateByPrimaryKey(SimpleTableRecord record) {
158183
return UpdateDSL.updateWithMapper(this::update, simpleTable)
159-
.set(id).equalTo(record::getId)
160184
.set(firstName).equalTo(record::getFirstName)
161185
.set(lastName).equalTo(record::getLastName)
162186
.set(birthDate).equalTo(record::getBirthDate)
@@ -169,7 +193,6 @@ default int updateByPrimaryKey(SimpleTableRecord record) {
169193

170194
default int updateByPrimaryKeySelective(SimpleTableRecord record) {
171195
return UpdateDSL.updateWithMapper(this::update, simpleTable)
172-
.set(id).equalToWhenPresent(record::getId)
173196
.set(firstName).equalToWhenPresent(record::getFirstName)
174197
.set(lastName).equalToWhenPresent(record::getLastName)
175198
.set(birthDate).equalToWhenPresent(record::getBirthDate)

src/test/java/examples/simple/SimpleTableAnnotatedMapperTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.ibatis.jdbc.ScriptRunner;
3131
import org.apache.ibatis.mapping.Environment;
3232
import org.apache.ibatis.session.Configuration;
33+
import org.apache.ibatis.session.RowBounds;
3334
import org.apache.ibatis.session.SqlSession;
3435
import org.apache.ibatis.session.SqlSessionFactory;
3536
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
@@ -85,6 +86,25 @@ public void testSelectByExample() {
8586
}
8687
}
8788

89+
@Test
90+
public void testSelectByExampleWithRowbounds() {
91+
SqlSession session = sqlSessionFactory.openSession();
92+
try {
93+
SimpleTableAnnotatedMapper mapper = session.getMapper(SimpleTableAnnotatedMapper.class);
94+
RowBounds rowBounds = new RowBounds(2, 2);
95+
96+
List<SimpleTableRecord> rows = mapper.selectByExample(rowBounds)
97+
.where(id, isEqualTo(1))
98+
.or(occupation, isNull())
99+
.build()
100+
.execute();
101+
102+
assertThat(rows.size()).isEqualTo(1);
103+
} finally {
104+
session.close();
105+
}
106+
}
107+
88108
@Test
89109
public void testSelectDistinctByExample() {
90110
SqlSession session = sqlSessionFactory.openSession();
@@ -103,6 +123,25 @@ public void testSelectDistinctByExample() {
103123
}
104124
}
105125

126+
@Test
127+
public void testSelectDistinctByExampleWithRowbounds() {
128+
SqlSession session = sqlSessionFactory.openSession();
129+
try {
130+
SimpleTableAnnotatedMapper mapper = session.getMapper(SimpleTableAnnotatedMapper.class);
131+
RowBounds rowBounds = new RowBounds(2, 2);
132+
133+
List<SimpleTableRecord> rows = mapper.selectDistinctByExample(rowBounds)
134+
.where(id, isGreaterThan(1))
135+
.or(occupation, isNull())
136+
.build()
137+
.execute();
138+
139+
assertThat(rows.size()).isEqualTo(2);
140+
} finally {
141+
session.close();
142+
}
143+
}
144+
106145
@Test
107146
public void testSelectByExampleWithTypeHandler() {
108147
SqlSession session = sqlSessionFactory.openSession();
@@ -137,6 +176,8 @@ public void testFirstNameIn() {
137176
.execute();
138177

139178
assertThat(rows.size()).isEqualTo(2);
179+
assertThat(rows.get(0).getLastName().getName()).isEqualTo("Flintstone");
180+
assertThat(rows.get(1).getLastName().getName()).isEqualTo("Rubble");
140181
} finally {
141182
session.close();
142183
}

0 commit comments

Comments
 (0)