Skip to content

Commit bc17552

Browse files
committed
Documentation
1 parent 0b8c93e commit bc17552

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ Runtime behavior changes:
107107
are not supported by the library out of the box. The statement renderers now call methods `renderCondition` and
108108
`renderLeftColumn` that you can override to implement any rendering you need. In addition, we've made `filter` and
109109
`map` support optional if you implement custom conditions
110+
- Added support for configuring a Java property name to be associated with an `SqlColumn`. This property name can be
111+
used with the record based insert methods to reduce the boilerplate code for mapping columns to Java properties.
110112

111113
## Release 1.5.2 - June 3, 2024
112114

src/site/markdown/docs/insert.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,66 @@ Notice the `map` method. It is used to map a database column to an attribute of
4444
5. `map(column).toPropertyWhenPresent(property, Supplier<?> valueSupplier)` will insert a value from the record into a column if the value is non-null. The value of the property will be bound to the SQL statement as a prepared statement parameter. This is used to generate a "selective" insert as defined in MyBatis Generator.
4545
6. `map(column).toRow()` will insert the record itself into a column. This is appropriate when the "record" is a simple class like Integer or String.
4646

47+
### Mapped Columns
48+
Starting in version 2.0.0 there are two new methods:
49+
50+
1. `withMappedColumn(SqlColumn)` that will map a database column to a Java property based on a property name that can
51+
be configured in an `SQLColumn`.
52+
2. `withMappedColumnWhenPresent(SqlColumn, Supplier<T>)` that will map a database column to a Java property based on a
53+
property name that can be configured in an `SQLColumn`. The insert statement will only contain the mapped column when
54+
the Supplier returns a non-null value (this method is for single record inserts only).
55+
56+
This will allow you to configure mappings in a single place (the `SqlColumn`) and reuse them in multiple insert
57+
statements. For example:
58+
59+
```java
60+
public final class PersonDynamicSqlSupport {
61+
public static final Person person = new Person();
62+
public static final SqlColumn<Integer> id = person.id;
63+
public static final SqlColumn<String> firstName = person.firstName;
64+
public static final SqlColumn<LastName> lastName = person.lastName;
65+
66+
public static final class Person extends SqlTable {
67+
public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER).withJavaProperty("id");
68+
public final SqlColumn<String> firstName = column("first_name", JDBCType.VARCHAR)
69+
.withJavaProperty("firstName");
70+
public final SqlColumn<LastName> lastName =
71+
column("last_name", JDBCType.VARCHAR).withJavaProperty("lastName");
72+
73+
public Person() {
74+
super("Person");
75+
}
76+
}
77+
}
78+
```
79+
80+
In this support class, each `SqlColumn` has a configured Java property. This property can be accessed in record based
81+
inserts in the following way:
82+
83+
```java
84+
@Test
85+
void testRawInsert() {
86+
try (SqlSession session = sqlSessionFactory.openSession()) {
87+
PersonMapper mapper = session.getMapper(PersonMapper.class);
88+
PersonRecord row = new PersonRecord(100, "Joe", "Jones");
89+
90+
InsertStatementProvider<PersonRecord> insertStatement = insert(row).into(person)
91+
.withMappedColumn(id)
92+
.withMappedColumn(firstName)
93+
.withMappedColumn(lastName)
94+
.build().render(RenderingStrategies.MYBATIS3);
95+
96+
int rows = mapper.insert(insertStatement);
97+
assertThat(rows).isEqualTo(1);
98+
}
99+
}
100+
```
101+
102+
In this test, the mapping between a column and the property of a record is calculated by reading the configured Java
103+
property for each column.
104+
105+
These new methods are available for the record based insert statements (`insert`, `insertMultiple`, `insertBatch`).
106+
47107
### Annotated Mapper for Single Row Insert Statements
48108
The InsertStatementProvider object can be used as a parameter to a MyBatis mapper method directly. If you
49109
are using an annotated mapper, the insert method should look like this (with @Options added for generated values if necessary):

src/test/java/examples/simple/PersonMapperTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
5454
import org.mybatis.dynamic.sql.exception.NonRenderingWhereClauseException;
5555
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
56+
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
5657
import org.mybatis.dynamic.sql.render.RenderingStrategies;
5758
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
5859
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
@@ -338,6 +339,27 @@ void testInsert() {
338339
}
339340
}
340341

342+
@Test
343+
void testRawInsert() {
344+
try (SqlSession session = sqlSessionFactory.openSession()) {
345+
PersonMapper mapper = session.getMapper(PersonMapper.class);
346+
PersonRecord row = new PersonRecord(100, "Joe", new LastName("Jones"), new Date(), true, "Developer", 1);
347+
348+
InsertStatementProvider<PersonRecord> insertStatement = insert(row).into(person)
349+
.withMappedColumn(id)
350+
.withMappedColumn(firstName)
351+
.withMappedColumn(lastName)
352+
.withMappedColumn(birthDate)
353+
.withMappedColumn(employed)
354+
.withMappedColumn(occupation)
355+
.withMappedColumn(addressId)
356+
.build().render(RenderingStrategies.MYBATIS3);
357+
358+
int rows = mapper.insert(insertStatement);
359+
assertThat(rows).isEqualTo(1);
360+
}
361+
}
362+
341363
@Test
342364
void testGeneralInsert() {
343365
try (SqlSession session = sqlSessionFactory.openSession()) {

0 commit comments

Comments
 (0)