Skip to content

Commit 2e867d0

Browse files
committed
Add tests for a join in the new MBG style
1 parent d6178b9 commit 2e867d0

15 files changed

+649
-211
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2016-2019 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+
* http://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+
package examples.simple;
17+
18+
import java.sql.JDBCType;
19+
20+
import org.mybatis.dynamic.sql.SqlColumn;
21+
import org.mybatis.dynamic.sql.SqlTable;
22+
23+
public final class AddressDynamicSqlSupport {
24+
public static final Address address = new Address();
25+
public static final SqlColumn<Integer> id = address.id;
26+
public static final SqlColumn<String> streetAddress = address.streetAddress;
27+
public static final SqlColumn<String> city = address.city;
28+
public static final SqlColumn<String> state = address.state;
29+
30+
public static final class Address extends SqlTable {
31+
public final SqlColumn<Integer> id = column("address_id", JDBCType.INTEGER);
32+
public final SqlColumn<String> streetAddress = column("street_address", JDBCType.VARCHAR);
33+
public final SqlColumn<String> city = column("city", JDBCType.VARCHAR);
34+
public final SqlColumn<String> state = column("state", JDBCType.VARCHAR);
35+
36+
public Address() {
37+
super("Address");
38+
}
39+
}
40+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2016-2019 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+
* http://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+
package examples.simple;
17+
18+
public class AddressRecord {
19+
private Integer id;
20+
private String streetAddress;
21+
private String city;
22+
private String state;
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getStreetAddress() {
33+
return streetAddress;
34+
}
35+
36+
public void setStreetAddress(String streetAddress) {
37+
this.streetAddress = streetAddress;
38+
}
39+
40+
public String getCity() {
41+
return city;
42+
}
43+
44+
public void setCity(String city) {
45+
this.city = city;
46+
}
47+
48+
public String getState() {
49+
return state;
50+
}
51+
52+
public void setState(String state) {
53+
this.state = state;
54+
}
55+
}

src/test/java/examples/simple/LastName.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,4 +31,29 @@ public static LastName of(String name) {
3131
lastName.setName(name);
3232
return lastName;
3333
}
34+
35+
@Override
36+
public int hashCode() {
37+
final int prime = 31;
38+
int result = 1;
39+
result = prime * result + ((name == null) ? 0 : name.hashCode());
40+
return result;
41+
}
42+
43+
@Override
44+
public boolean equals(Object obj) {
45+
if (this == obj)
46+
return true;
47+
if (obj == null)
48+
return false;
49+
if (getClass() != obj.getClass())
50+
return false;
51+
LastName other = (LastName) obj;
52+
if (name == null) {
53+
if (other.name != null)
54+
return false;
55+
} else if (!name.equals(other.name))
56+
return false;
57+
return true;
58+
}
3459
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/test/java/examples/simple/SimpleTableDynamicSqlSupport.java renamed to src/test/java/examples/simple/PersonDynamicSqlSupport.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,25 +21,27 @@
2121
import org.mybatis.dynamic.sql.SqlColumn;
2222
import org.mybatis.dynamic.sql.SqlTable;
2323

24-
public final class SimpleTableDynamicSqlSupport {
25-
public static final SimpleTable simpleTable = new SimpleTable();
26-
public static final SqlColumn<Integer> id = simpleTable.id;
27-
public static final SqlColumn<String> firstName = simpleTable.firstName;
28-
public static final SqlColumn<LastName> lastName = simpleTable.lastName;
29-
public static final SqlColumn<Date> birthDate = simpleTable.birthDate;
30-
public static final SqlColumn<Boolean> employed = simpleTable.employed;
31-
public static final SqlColumn<String> occupation = simpleTable.occupation;
24+
public final class PersonDynamicSqlSupport {
25+
public static final Person person = new Person();
26+
public static final SqlColumn<Integer> id = person.id;
27+
public static final SqlColumn<String> firstName = person.firstName;
28+
public static final SqlColumn<LastName> lastName = person.lastName;
29+
public static final SqlColumn<Date> birthDate = person.birthDate;
30+
public static final SqlColumn<Boolean> employed = person.employed;
31+
public static final SqlColumn<String> occupation = person.occupation;
32+
public static final SqlColumn<Integer> addressId = person.addressId;
3233

33-
public static final class SimpleTable extends SqlTable {
34+
public static final class Person extends SqlTable {
3435
public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER);
3536
public final SqlColumn<String> firstName = column("first_name", JDBCType.VARCHAR);
3637
public final SqlColumn<LastName> lastName = column("last_name", JDBCType.VARCHAR, "examples.simple.LastNameTypeHandler");
3738
public final SqlColumn<Date> birthDate = column("birth_date", JDBCType.DATE);
3839
public final SqlColumn<Boolean> employed = column("employed", JDBCType.VARCHAR, "examples.simple.YesNoTypeHandler");
3940
public final SqlColumn<String> occupation = column("occupation", JDBCType.VARCHAR);
41+
public final SqlColumn<Integer> addressId = column("address_id", JDBCType.INTEGER);
4042

41-
public SimpleTable() {
42-
super("SimpleTable");
43+
public Person() {
44+
super("Person");
4345
}
4446
}
4547
}

src/test/java/examples/simple/SimpleTableMapperNewStyle.java renamed to src/test/java/examples/simple/PersonMapper.java

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package examples.simple;
1717

18-
import static examples.simple.SimpleTableDynamicSqlSupport.*;
18+
import static examples.simple.PersonDynamicSqlSupport.*;
1919
import static org.mybatis.dynamic.sql.SqlBuilder.*;
2020

2121
import java.util.List;
@@ -55,13 +55,13 @@
5555
*
5656
*/
5757
@Mapper
58-
public interface SimpleTableMapperNewStyle {
58+
public interface PersonMapper {
5959

6060
@InsertProvider(type=SqlProviderAdapter.class, method="insert")
61-
int insert(InsertStatementProvider<SimpleTableRecord> insertStatement);
61+
int insert(InsertStatementProvider<PersonRecord> insertStatement);
6262

6363
@InsertProvider(type=SqlProviderAdapter.class, method="insertMultiple")
64-
int insertMultiple(MultiRowInsertStatementProvider<SimpleTableRecord> insertStatement);
64+
int insertMultiple(MultiRowInsertStatementProvider<PersonRecord> insertStatement);
6565

6666
@UpdateProvider(type=SqlProviderAdapter.class, method="update")
6767
int update(UpdateStatementProvider updateStatement);
@@ -73,13 +73,14 @@ public interface SimpleTableMapperNewStyle {
7373
@Result(column="last_name", property="lastName", jdbcType=JdbcType.VARCHAR, typeHandler=LastNameTypeHandler.class),
7474
@Result(column="birth_date", property="birthDate", jdbcType=JdbcType.DATE),
7575
@Result(column="employed", property="employed", jdbcType=JdbcType.VARCHAR, typeHandler=YesNoTypeHandler.class),
76-
@Result(column="occupation", property="occupation", jdbcType=JdbcType.VARCHAR)
76+
@Result(column="occupation", property="occupation", jdbcType=JdbcType.VARCHAR),
77+
@Result(column="address_id", property="addressId", jdbcType=JdbcType.INTEGER)
7778
})
78-
List<SimpleTableRecord> selectMany(SelectStatementProvider selectStatement);
79+
List<PersonRecord> selectMany(SelectStatementProvider selectStatement);
7980

8081
@SelectProvider(type=SqlProviderAdapter.class, method="select")
8182
@ResultMap("SimpleTableResult")
82-
Optional<SimpleTableRecord> selectOne(SelectStatementProvider selectStatement);
83+
Optional<PersonRecord> selectOne(SelectStatementProvider selectStatement);
8384

8485
@DeleteProvider(type=SqlProviderAdapter.class, method="delete")
8586
int delete(DeleteStatementProvider deleteStatement);
@@ -89,13 +90,13 @@ public interface SimpleTableMapperNewStyle {
8990

9091
default long count(MyBatis3CountHelper helper) {
9192
return helper.apply(SelectDSL.selectWithMapper(this::count, SqlBuilder.count())
92-
.from(simpleTable))
93+
.from(person))
9394
.build()
9495
.execute();
9596
}
9697

9798
default int delete(MyBatis3DeleteHelper helper) {
98-
return helper.apply(MyBatis3Utils.deleteFrom(this::delete, simpleTable))
99+
return helper.apply(MyBatis3Utils.deleteFrom(this::delete, person))
99100
.build()
100101
.execute();
101102
}
@@ -106,116 +107,123 @@ default int deleteByPrimaryKey(Integer id_) {
106107
);
107108
}
108109

109-
default int insert(SimpleTableRecord record) {
110+
default int insert(PersonRecord record) {
110111
return insert(SqlBuilder.insert(record)
111-
.into(simpleTable)
112+
.into(person)
112113
.map(id).toProperty("id")
113114
.map(firstName).toProperty("firstName")
114115
.map(lastName).toProperty("lastName")
115116
.map(birthDate).toProperty("birthDate")
116117
.map(employed).toProperty("employed")
117118
.map(occupation).toProperty("occupation")
119+
.map(addressId).toProperty("addressId")
118120
.build()
119121
.render(RenderingStrategy.MYBATIS3));
120122
}
121123

122-
default int insertMultiple(List<SimpleTableRecord> records) {
124+
default int insertMultiple(List<PersonRecord> records) {
123125
return insertMultiple(SqlBuilder.insertMultiple(records)
124-
.into(simpleTable)
126+
.into(person)
125127
.map(id).toProperty("id")
126128
.map(firstName).toProperty("firstName")
127129
.map(lastName).toProperty("lastName")
128130
.map(birthDate).toProperty("birthDate")
129131
.map(employed).toProperty("employed")
130132
.map(occupation).toProperty("occupation")
133+
.map(addressId).toProperty("addressId")
131134
.build()
132135
.render(RenderingStrategy.MYBATIS3));
133136
}
134137

135-
default int insertSelective(SimpleTableRecord record) {
138+
default int insertSelective(PersonRecord record) {
136139
return insert(SqlBuilder.insert(record)
137-
.into(simpleTable)
140+
.into(person)
138141
.map(id).toPropertyWhenPresent("id", record::getId)
139142
.map(firstName).toPropertyWhenPresent("firstName", record::getFirstName)
140143
.map(lastName).toPropertyWhenPresent("lastName", record::getLastName)
141144
.map(birthDate).toPropertyWhenPresent("birthDate", record::getBirthDate)
142145
.map(employed).toPropertyWhenPresent("employed", record::getEmployed)
143146
.map(occupation).toPropertyWhenPresent("occupation", record::getOccupation)
147+
.map(addressId).toPropertyWhenPresent("addressId", record::getAddressId)
144148
.build()
145149
.render(RenderingStrategy.MYBATIS3));
146150
}
147151

148-
default Optional<SimpleTableRecord> selectOne(MyBatis3SelectOneHelper<SimpleTableRecord> helper) {
149-
return helper.apply(SelectDSL.selectWithMapper(this::selectOne, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
150-
.from(simpleTable))
152+
default Optional<PersonRecord> selectOne(MyBatis3SelectOneHelper<PersonRecord> helper) {
153+
return helper.apply(SelectDSL.selectWithMapper(this::selectOne, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
154+
.from(person))
151155
.build()
152156
.execute();
153157
}
154158

155-
default List<SimpleTableRecord> select(MyBatis3SelectListHelper<SimpleTableRecord> helper) {
156-
return helper.apply(SelectDSL.selectWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
157-
.from(simpleTable))
159+
default List<PersonRecord> select(MyBatis3SelectListHelper<PersonRecord> helper) {
160+
return helper.apply(SelectDSL.selectWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
161+
.from(person))
158162
.build()
159163
.execute();
160164
}
161165

162-
default List<SimpleTableRecord> selectDistinct(MyBatis3SelectListHelper<SimpleTableRecord> helper) {
163-
return helper.apply(SelectDSL.selectDistinctWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
164-
.from(simpleTable))
166+
default List<PersonRecord> selectDistinct(MyBatis3SelectListHelper<PersonRecord> helper) {
167+
return helper.apply(SelectDSL.selectDistinctWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
168+
.from(person))
165169
.build()
166170
.execute();
167171
}
168172

169-
default Optional<SimpleTableRecord> selectByPrimaryKey(Integer id_) {
173+
default Optional<PersonRecord> selectByPrimaryKey(Integer id_) {
170174
return selectOne(h ->
171175
h.where(id, isEqualTo(id_))
172176
);
173177
}
174178

175179
default int update(MyBatis3UpdateHelper helper) {
176-
return helper.apply(MyBatis3Utils.update(this::update, simpleTable))
180+
return helper.apply(MyBatis3Utils.update(this::update, person))
177181
.build()
178182
.execute();
179183
}
180184

181-
static UpdateDSL<MyBatis3UpdateModelToIntAdapter> setAll(SimpleTableRecord record,
185+
static UpdateDSL<MyBatis3UpdateModelToIntAdapter> setAll(PersonRecord record,
182186
UpdateDSL<MyBatis3UpdateModelToIntAdapter> dsl) {
183187
return dsl.set(id).equalTo(record::getId)
184188
.set(firstName).equalTo(record::getFirstName)
185189
.set(lastName).equalTo(record::getLastName)
186190
.set(birthDate).equalTo(record::getBirthDate)
187191
.set(employed).equalTo(record::getEmployed)
188-
.set(occupation).equalTo(record::getOccupation);
192+
.set(occupation).equalTo(record::getOccupation)
193+
.set(addressId).equalTo(record::getAddressId);
189194
}
190195

191-
static UpdateDSL<MyBatis3UpdateModelToIntAdapter> setSelective(SimpleTableRecord record,
196+
static UpdateDSL<MyBatis3UpdateModelToIntAdapter> setSelective(PersonRecord record,
192197
UpdateDSL<MyBatis3UpdateModelToIntAdapter> dsl) {
193198
return dsl.set(id).equalToWhenPresent(record::getId)
194199
.set(firstName).equalToWhenPresent(record::getFirstName)
195200
.set(lastName).equalToWhenPresent(record::getLastName)
196201
.set(birthDate).equalToWhenPresent(record::getBirthDate)
197202
.set(employed).equalToWhenPresent(record::getEmployed)
198-
.set(occupation).equalToWhenPresent(record::getOccupation);
203+
.set(occupation).equalToWhenPresent(record::getOccupation)
204+
.set(addressId).equalToWhenPresent(record::getAddressId);
199205
}
200206

201-
default int updateByPrimaryKey(SimpleTableRecord record) {
207+
default int updateByPrimaryKey(PersonRecord record) {
202208
return update(h ->
203209
h.set(firstName).equalTo(record::getFirstName)
204210
.set(lastName).equalTo(record::getLastName)
205211
.set(birthDate).equalTo(record::getBirthDate)
206212
.set(employed).equalTo(record::getEmployed)
207213
.set(occupation).equalTo(record::getOccupation)
214+
.set(addressId).equalTo(record::getAddressId)
208215
.where(id, isEqualTo(record::getId))
209216
);
210217
}
211218

212-
default int updateByPrimaryKeySelective(SimpleTableRecord record) {
219+
default int updateByPrimaryKeySelective(PersonRecord record) {
213220
return update(h ->
214221
h.set(firstName).equalToWhenPresent(record::getFirstName)
215222
.set(lastName).equalToWhenPresent(record::getLastName)
216223
.set(birthDate).equalToWhenPresent(record::getBirthDate)
217224
.set(employed).equalToWhenPresent(record::getEmployed)
218225
.set(occupation).equalToWhenPresent(record::getOccupation)
226+
.set(addressId).equalToWhenPresent(record::getAddressId)
219227
.where(id, isEqualTo(record::getId))
220228
);
221229
}

0 commit comments

Comments
 (0)