Skip to content

Commit b390f1f

Browse files
committed
Use search utils
1 parent 9c82e61 commit b390f1f

File tree

4 files changed

+60
-91
lines changed

4 files changed

+60
-91
lines changed

Diff for: src/test/java/examples/custom_render/CustomRenderingTest.java

+33-53
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import org.apache.ibatis.session.SqlSessionFactory;
4141
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
4242
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
43-
import org.junit.jupiter.api.BeforeAll;
43+
import org.junit.jupiter.api.BeforeEach;
4444
import org.junit.jupiter.api.Test;
4545
import org.mybatis.dynamic.sql.SqlColumn;
4646
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
@@ -63,10 +63,10 @@ class CustomRenderingTest {
6363
new PostgreSQLContainer<>(TestContainersConfiguration.POSTGRES_LATEST)
6464
.withInitScript("examples/custom_render/dbInit.sql");
6565

66-
private static SqlSessionFactory sqlSessionFactory;
66+
private SqlSessionFactory sqlSessionFactory;
6767

68-
@BeforeAll
69-
static void setUp() {
68+
@BeforeEach
69+
void setUp() {
7070
UnpooledDataSource ds = new UnpooledDataSource(postgres.getDriverClassName(), postgres.getJdbcUrl(),
7171
postgres.getUsername(), postgres.getPassword());
7272
Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
@@ -81,10 +81,8 @@ void testInsertRecord() {
8181
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
8282
JsonTestMapper mapper = sqlSession.getMapper(JsonTestMapper.class);
8383

84-
JsonTestRecord row = new JsonTestRecord();
85-
row.setId(1);
86-
row.setDescription("Fred");
87-
row.setInfo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
84+
JsonTestRecord row = new JsonTestRecord(1, "Fred",
85+
"{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
8886

8987
InsertStatementProvider<JsonTestRecord> insertStatement = insert(row).into(jsonTest)
9088
.map(id).toProperty("id")
@@ -102,10 +100,8 @@ void testInsertRecord() {
102100
int rows = mapper.insert(insertStatement);
103101
assertThat(rows).isEqualTo(1);
104102

105-
row = new JsonTestRecord();
106-
row.setId(2);
107-
row.setDescription("Wilma");
108-
row.setInfo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
103+
row = new JsonTestRecord(2, "Wilma",
104+
"{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
109105

110106
insertStatement = insert(row).into(jsonTest)
111107
.map(id).toProperty("id")
@@ -125,8 +121,8 @@ void testInsertRecord() {
125121

126122
List<JsonTestRecord> records = mapper.selectMany(selectStatement);
127123
assertThat(records).hasSize(2);
128-
assertThat(records.get(0).getInfo()).isEqualTo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
129-
assertThat(records.get(1).getInfo()).isEqualTo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
124+
assertThat(records.get(0).info()).isEqualTo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
125+
assertThat(records.get(1).info()).isEqualTo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
130126
}
131127
}
132128

@@ -169,8 +165,8 @@ void testGeneralInsert() {
169165

170166
List<JsonTestRecord> records = mapper.selectMany(selectStatement);
171167
assertThat(records).hasSize(2);
172-
assertThat(records.get(0).getInfo()).isEqualTo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
173-
assertThat(records.get(1).getInfo()).isEqualTo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
168+
assertThat(records.get(0).info()).isEqualTo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
169+
assertThat(records.get(1).info()).isEqualTo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
174170
}
175171
}
176172

@@ -179,15 +175,11 @@ void testInsertMultiple() {
179175
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
180176
JsonTestMapper mapper = sqlSession.getMapper(JsonTestMapper.class);
181177

182-
JsonTestRecord record1 = new JsonTestRecord();
183-
record1.setId(1);
184-
record1.setDescription("Fred");
185-
record1.setInfo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
178+
JsonTestRecord record1 = new JsonTestRecord(1, "Fred",
179+
"{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
186180

187-
JsonTestRecord record2 = new JsonTestRecord();
188-
record2.setId(2);
189-
record2.setDescription("Wilma");
190-
record2.setInfo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
181+
JsonTestRecord record2 = new JsonTestRecord(2, "Wilma",
182+
"{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
191183

192184
MultiRowInsertStatementProvider<JsonTestRecord> insertStatement = insertMultiple(record1, record2)
193185
.into(jsonTest)
@@ -216,8 +208,8 @@ void testInsertMultiple() {
216208

217209
List<JsonTestRecord> records = mapper.selectMany(selectStatement);
218210
assertThat(records).hasSize(2);
219-
assertThat(records.get(0).getInfo()).isEqualTo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
220-
assertThat(records.get(1).getInfo()).isEqualTo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
211+
assertThat(records.get(0).info()).isEqualTo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
212+
assertThat(records.get(1).info()).isEqualTo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
221213
}
222214
}
223215

@@ -226,15 +218,11 @@ void testUpdate() {
226218
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
227219
JsonTestMapper mapper = sqlSession.getMapper(JsonTestMapper.class);
228220

229-
JsonTestRecord record1 = new JsonTestRecord();
230-
record1.setId(1);
231-
record1.setDescription("Fred");
232-
record1.setInfo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
221+
JsonTestRecord record1 = new JsonTestRecord(1, "Fred",
222+
"{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
233223

234-
JsonTestRecord record2 = new JsonTestRecord();
235-
record2.setId(2);
236-
record2.setDescription("Wilma");
237-
record2.setInfo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
224+
JsonTestRecord record2 = new JsonTestRecord(2, "Wilma",
225+
"{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
238226

239227
MultiRowInsertStatementProvider<JsonTestRecord> insertStatement = insertMultiple(record1, record2)
240228
.into(jsonTest)
@@ -270,8 +258,8 @@ void testUpdate() {
270258

271259
List<JsonTestRecord> records = mapper.selectMany(selectStatement);
272260
assertThat(records).hasSize(2);
273-
assertThat(records.get(0).getInfo()).isEqualTo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
274-
assertThat(records.get(1).getInfo()).isEqualTo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 26}");
261+
assertThat(records.get(0).info()).isEqualTo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
262+
assertThat(records.get(1).info()).isEqualTo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 26}");
275263
}
276264
}
277265

@@ -280,15 +268,11 @@ void testDeReference() {
280268
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
281269
JsonTestMapper mapper = sqlSession.getMapper(JsonTestMapper.class);
282270

283-
JsonTestRecord record1 = new JsonTestRecord();
284-
record1.setId(1);
285-
record1.setDescription("Fred");
286-
record1.setInfo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
271+
JsonTestRecord record1 = new JsonTestRecord(1, "Fred",
272+
"{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
287273

288-
JsonTestRecord record2 = new JsonTestRecord();
289-
record2.setId(2);
290-
record2.setDescription("Wilma");
291-
record2.setInfo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
274+
JsonTestRecord record2 = new JsonTestRecord(2, "Wilma",
275+
"{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
292276

293277
MultiRowInsertStatementProvider<JsonTestRecord> insertStatement = insertMultiple(record1, record2)
294278
.into(jsonTest)
@@ -316,7 +300,7 @@ void testDeReference() {
316300
Optional<JsonTestRecord> row = mapper.selectOne(selectStatement);
317301

318302
assertThat(row).hasValueSatisfying( r ->
319-
assertThat(r.getInfo())
303+
assertThat(r.info())
320304
.isEqualTo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}")
321305
);
322306
}
@@ -327,15 +311,11 @@ void testDereference2() {
327311
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
328312
JsonTestMapper mapper = sqlSession.getMapper(JsonTestMapper.class);
329313

330-
JsonTestRecord record1 = new JsonTestRecord();
331-
record1.setId(1);
332-
record1.setDescription("Fred");
333-
record1.setInfo("{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
314+
JsonTestRecord record1 = new JsonTestRecord(1, "Fred",
315+
"{\"firstName\": \"Fred\", \"lastName\": \"Flintstone\", \"age\": 30}");
334316

335-
JsonTestRecord record2 = new JsonTestRecord();
336-
record2.setId(2);
337-
record2.setDescription("Wilma");
338-
record2.setInfo("{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
317+
JsonTestRecord record2 = new JsonTestRecord(2, "Wilma",
318+
"{\"firstName\": \"Wilma\", \"lastName\": \"Flintstone\", \"age\": 25}");
339319

340320
MultiRowInsertStatementProvider<JsonTestRecord> insertStatement = insertMultiple(record1, record2)
341321
.into(jsonTest)

Diff for: src/test/java/examples/custom_render/JsonTestMapper.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import java.util.List;
1919
import java.util.Optional;
2020

21-
import org.apache.ibatis.annotations.Result;
22-
import org.apache.ibatis.annotations.ResultMap;
23-
import org.apache.ibatis.annotations.Results;
21+
import org.apache.ibatis.annotations.Arg;
2422
import org.apache.ibatis.annotations.SelectProvider;
2523
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2624
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
@@ -32,14 +30,14 @@
3230
public interface JsonTestMapper extends CommonDeleteMapper, CommonInsertMapper<JsonTestRecord>, CommonSelectMapper,
3331
CommonUpdateMapper {
3432
@SelectProvider(type = SqlProviderAdapter.class, method = "select")
35-
@Results(id = "JsonTestResult", value = {
36-
@Result(column = "id", property = "id", id = true),
37-
@Result(column = "description", property = "description"),
38-
@Result(column = "info", property = "info")
39-
})
33+
@Arg(column = "id", javaType = int.class, id = true)
34+
@Arg(column = "description", javaType = String.class)
35+
@Arg(column = "info", javaType = String.class)
4036
List<JsonTestRecord> selectMany(SelectStatementProvider selectStatement);
4137

4238
@SelectProvider(type = SqlProviderAdapter.class, method = "select")
43-
@ResultMap("JsonTestResult")
39+
@Arg(column = "id", javaType = int.class, id = true)
40+
@Arg(column = "description", javaType = String.class)
41+
@Arg(column = "info", javaType = String.class)
4442
Optional<JsonTestRecord> selectOne(SelectStatementProvider selectStatement);
4543
}

Diff for: src/test/java/examples/custom_render/JsonTestRecord.java

+1-29
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,4 @@
1515
*/
1616
package examples.custom_render;
1717

18-
public class JsonTestRecord {
19-
private int id;
20-
private String description;
21-
private String info;
22-
23-
public int getId() {
24-
return id;
25-
}
26-
27-
public void setId(int id) {
28-
this.id = id;
29-
}
30-
31-
public String getDescription() {
32-
return description;
33-
}
34-
35-
public void setDescription(String description) {
36-
this.description = description;
37-
}
38-
39-
public String getInfo() {
40-
return info;
41-
}
42-
43-
public void setInfo(String info) {
44-
this.info = info;
45-
}
46-
}
18+
public record JsonTestRecord (int id, String description, String info) {}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2016-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
@NullMarked
17+
package examples.custom_render;
18+
19+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)