1
1
/*
2
- * Copyright 2009-2021 the original author or authors.
2
+ * Copyright 2009-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .apache .ibatis .submitted .record_type ;
17
17
18
+ import static org .junit .jupiter .api .Assertions .*;
19
+
18
20
import java .io .Reader ;
19
- import java .util .List ;
20
- import java .util .function .Supplier ;
21
21
22
22
import org .apache .ibatis .BaseDataTest ;
23
- import org .apache .ibatis .annotations .Arg ;
24
- import org .apache .ibatis .annotations .ConstructorArgs ;
25
- import org .apache .ibatis .annotations .Select ;
26
- import org .apache .ibatis .annotations .Update ;
27
23
import org .apache .ibatis .io .Resources ;
28
24
import org .apache .ibatis .session .SqlSession ;
29
25
import org .apache .ibatis .session .SqlSessionFactory ;
30
26
import org .apache .ibatis .session .SqlSessionFactoryBuilder ;
31
- import org .junit .jupiter .api .Assertions ;
32
27
import org .junit .jupiter .api .BeforeAll ;
33
28
import org .junit .jupiter .api .Test ;
34
29
35
- public class RecordTypeTest {
36
-
37
- static record Property (int id , String value ) {
38
- }
39
-
40
- static record Item (int id , List <Property > properties ) {
41
- }
42
-
43
- static record ItemId (Long get ) implements Supplier <Long > {
44
- }
45
- public static class ItemIdTypeHandler extends IdTypeHandler <Long , ItemId > {
46
- public ItemIdTypeHandler () {
47
- super (Long .class , ItemId ::new );
48
- }
49
- }
50
-
51
- static record PropId (Integer get ) implements Supplier <Integer > {
52
- }
53
- public static class PropIdTypeHandler extends IdTypeHandler <Integer , PropId > {
54
- public PropIdTypeHandler () {
55
- super (Integer .class , PropId ::new );
56
- }
57
- }
58
-
59
- static record Property2 (PropId id , String value ) {
60
- }
61
-
62
- static record Item2 (ItemId id , List <Property2 > properties ) {
63
- public String idStr () {
64
- return String .valueOf (id );
65
- }
66
- }
67
-
68
- static interface Mapper {
69
-
70
- @ Update ("""
71
- <script>
72
- insert into properties (item_id, property_id, value) values
73
- <foreach collection="properties" item="prop" close=";" separator=",">
74
- (#{id},
75
- #{prop.id},
76
- #{prop.value})
77
- </foreach>
78
- </script>
79
- """ )
80
- void updateProps (Item item );
81
-
82
- @ Update ("""
83
- <script>
84
- insert into properties (item_id, property_id, value) values
85
- <foreach collection="properties" item="prop" close=";" separator=",">
86
- (#{id, jdbcType=NUMERIC, typeHandler=org.apache.ibatis.submitted.record_type.RecordTypeTest$ItemIdTypeHandler},
87
- #{prop.id, jdbcType=NUMERIC, typeHandler=org.apache.ibatis.submitted.record_type.RecordTypeTest$PropIdTypeHandler},
88
- #{prop.value})
89
- </foreach>
90
- </script>
91
- """ )
92
- void updateProps2 (Item2 item );
93
-
94
- @ Select ("select property_id, value from properties order by property_id" )
95
- @ ConstructorArgs (value = {
96
- @ Arg (column = "property_id" , javaType = int .class ),
97
- @ Arg (column = "value" , javaType = String .class ),
98
- })
99
- List <Property > allProps ();
100
-
101
- @ Select ("select property_id, value from properties order by property_id" )
102
- @ ConstructorArgs (value = {
103
- @ Arg (column ="property_id" , javaType =PropId .class , typeHandler =PropIdTypeHandler .class ),
104
- @ Arg (column ="value" , javaType =String .class ),
105
- })
106
- List <Property2 > allProps2 ();
107
- }
30
+ class RecordTypeTest {
108
31
109
32
private static SqlSessionFactory sqlSessionFactory ;
110
33
@@ -114,41 +37,42 @@ static void setUp() throws Exception {
114
37
try (Reader reader = Resources .getResourceAsReader ("org/apache/ibatis/submitted/record_type/mybatis-config.xml" )) {
115
38
sqlSessionFactory = new SqlSessionFactoryBuilder ().build (reader );
116
39
}
117
-
118
40
// populate in-memory database
119
41
BaseDataTest .runScript (sqlSessionFactory .getConfiguration ().getEnvironment ().getDataSource (),
120
- "org/apache/ibatis/submitted/record_type/CreateDB.sql" );
42
+ "org/apache/ibatis/submitted/record_type/CreateDB.sql" );
121
43
}
122
44
123
45
@ Test
124
- void test1 () {
46
+ void testSelectRecord () {
125
47
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
126
- Mapper mapper = sqlSession .getMapper (Mapper .class );
127
-
128
- mapper .updateProps (new Item (10 , List .of (
129
- new Property (11 , "value11" ),
130
- new Property (12 , "value12" ))));
131
-
132
- mapper .updateProps2 (new Item2 (new ItemId (20L ), List .of (
133
- new Property2 (new PropId (21 ), "value21" ),
134
- new Property2 (new PropId (22 ), "value22" ))));
48
+ RecordTypeMapper mapper = sqlSession .getMapper (RecordTypeMapper .class );
49
+ Property prop = mapper .selectProperty (1 );
50
+ assertEquals ("Val1" , prop .value ());
51
+ }
52
+ }
135
53
136
- var l1 = mapper .allProps ();
137
- Assertions .assertEquals (4 , l1 .size ());
138
- Assertions .assertEquals (l1 .get (0 ), new Property (11 , "value11" ));
139
- Assertions .assertEquals (l1 .get (1 ), new Property (12 , "value12" ));
140
- Assertions .assertEquals (l1 .get (2 ), new Property (21 , "value21" ));
141
- Assertions .assertEquals (l1 .get (3 ), new Property (22 , "value22" ));
54
+ @ Test
55
+ void testSelectRecordAutomapping () {
56
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
57
+ RecordTypeMapper mapper = sqlSession .getMapper (RecordTypeMapper .class );
58
+ Property prop = mapper .selectPropertyAutomapping (1 );
59
+ assertEquals ("Val1" , prop .value ());
60
+ assertEquals ("https://www.google.com" , prop .URL ());
61
+ }
62
+ }
142
63
143
- var l2 = mapper . allProps2 ();
144
- Assertions . assertEquals ( 4 , l2 . size ());
145
- Assertions . assertEquals ( l2 . get ( 0 ), new Property2 ( new PropId ( 11 ), "value11" ));
146
- Assertions . assertEquals ( l2 . get ( 1 ), new Property2 ( new PropId ( 12 ), "value12" ) );
147
- Assertions . assertEquals (l2 . get ( 2 ), new Property2 (new PropId ( 21 ) , "value21" ));
148
- Assertions . assertEquals ( l2 . get ( 3 ), new Property2 ( new PropId ( 22 ), "value22" ) );
64
+ @ Test
65
+ void testInsertRecord () {
66
+ try ( SqlSession sqlSession = sqlSessionFactory . openSession ()) {
67
+ RecordTypeMapper mapper = sqlSession . getMapper ( RecordTypeMapper . class );
68
+ assertEquals (1 , mapper . insertProperty (new Property ( 2 , "Val2" , "https://mybatis.org" ) ));
69
+ sqlSession . commit ( );
149
70
}
150
- catch (Exception e ) {
151
- e .printStackTrace ();
71
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
72
+ RecordTypeMapper mapper = sqlSession .getMapper (RecordTypeMapper .class );
73
+ Property prop = mapper .selectProperty (2 );
74
+ assertEquals ("Val2" , prop .value ());
75
+ assertEquals ("https://mybatis.org" , prop .URL ());
152
76
}
153
77
}
154
78
0 commit comments