Skip to content

Commit 6c2b614

Browse files
committed
Add tests for the improved Spring support
1 parent bf0c57a commit 6c2b614

11 files changed

+1263
-1
lines changed

src/test/java/examples/generated/always/spring/SpringTest.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@
2525
import org.junit.jupiter.api.BeforeEach;
2626
import org.junit.jupiter.api.Test;
2727
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
28+
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
29+
import org.mybatis.dynamic.sql.insert.InsertModel;
2830
import org.mybatis.dynamic.sql.insert.render.BatchInsert;
2931
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
3032
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
3133
import org.mybatis.dynamic.sql.render.RenderingStrategies;
3234
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
3335
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
36+
import org.mybatis.dynamic.sql.util.Buildable;
37+
import org.mybatis.dynamic.sql.util.spring.NamedParameterJdbcTemplateExtensions;
3438
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
3539
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
3640
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
@@ -145,6 +149,30 @@ void testInsert() {
145149
assertThat(generatedKey).isEqualTo("Bob Jones");
146150
}
147151

152+
@Test
153+
void testInsertWithExtensions() {
154+
GeneratedAlwaysRecord record = new GeneratedAlwaysRecord();
155+
record.setId(100);
156+
record.setFirstName("Bob");
157+
record.setLastName("Jones");
158+
159+
Buildable<InsertModel<GeneratedAlwaysRecord>> insertStatement = insert(record)
160+
.into(generatedAlways)
161+
.map(id).toProperty("id")
162+
.map(firstName).toProperty("firstName")
163+
.map(lastName).toProperty("lastName");
164+
165+
NamedParameterJdbcTemplateExtensions extensions = new NamedParameterJdbcTemplateExtensions(template);
166+
167+
KeyHolder keyHolder = new GeneratedKeyHolder();
168+
169+
int rows = extensions.insert(insertStatement, keyHolder);
170+
String generatedKey = (String) keyHolder.getKeys().get("FULL_NAME");
171+
172+
assertThat(rows).isEqualTo(1);
173+
assertThat(generatedKey).isEqualTo("Bob Jones");
174+
}
175+
148176
@Test
149177
void testGeneralInsert() {
150178
GeneralInsertStatementProvider insertStatement = insertInto(generatedAlways)
@@ -178,6 +206,23 @@ void testGeneralInsertWithGeneratedKey() {
178206
assertThat(generatedKey).isEqualTo("Bob Jones");
179207
}
180208

209+
@Test
210+
void testGeneralInsertWithGeneratedKeyAndExtensions() {
211+
Buildable<GeneralInsertModel> insertStatement = insertInto(generatedAlways)
212+
.set(id).toValue(100)
213+
.set(firstName).toValue("Bob")
214+
.set(lastName).toValue("Jones");
215+
216+
NamedParameterJdbcTemplateExtensions extensions = new NamedParameterJdbcTemplateExtensions(template);
217+
KeyHolder keyHolder = new GeneratedKeyHolder();
218+
219+
int rows = extensions.generalInsert(insertStatement, keyHolder);
220+
String generatedKey = (String) keyHolder.getKeys().get("FULL_NAME");
221+
222+
assertThat(rows).isEqualTo(1);
223+
assertThat(generatedKey).isEqualTo("Bob Jones");
224+
}
225+
181226
@Test
182227
void testInsertBatch() {
183228
List<GeneratedAlwaysRecord> records = new ArrayList<>();
@@ -223,6 +268,5 @@ void testUpdate() {
223268
int rows = template.update(updateStatement.getUpdateStatement(), parameterSource);
224269

225270
assertThat(rows).isEqualTo(2);
226-
227271
}
228272
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2016-2020 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.spring;
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-2020 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.spring;
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+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright 2016-2020 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.spring;
17+
18+
public class LastName {
19+
private String name;
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
public static LastName of(String name) {
30+
LastName lastName = new LastName();
31+
lastName.setName(name);
32+
return lastName;
33+
}
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+
}
59+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2016-2020 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.spring;
17+
18+
import org.mybatis.dynamic.sql.ParameterTypeConverter;
19+
import org.springframework.core.convert.converter.Converter;
20+
21+
public class LastNameParameterConverter implements ParameterTypeConverter<LastName>, Converter<LastName, String> {
22+
@Override
23+
public String convert(LastName source) {
24+
return source == null ? null : source.getName();
25+
}
26+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2016-2020 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.spring;
17+
18+
import java.sql.JDBCType;
19+
import java.util.Date;
20+
21+
import org.mybatis.dynamic.sql.SqlColumn;
22+
import org.mybatis.dynamic.sql.SqlTable;
23+
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;
33+
34+
public static final class Person extends SqlTable {
35+
public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER);
36+
public final SqlColumn<String> firstName = column("first_name", JDBCType.VARCHAR);
37+
public final SqlColumn<LastName> lastName = column("last_name", JDBCType.VARCHAR)
38+
.withParameterTypeConverter(new LastNameParameterConverter());
39+
public final SqlColumn<Date> birthDate = column("birth_date", JDBCType.DATE);
40+
public final SqlColumn<Boolean> employed = column("employed", JDBCType.VARCHAR)
41+
.withParameterTypeConverter(new YesNoParameterConverter());
42+
public final SqlColumn<String> occupation = column("occupation", JDBCType.VARCHAR);
43+
public final SqlColumn<Integer> addressId = column("address_id", JDBCType.INTEGER);
44+
45+
public Person() {
46+
super("Person");
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)