Skip to content

Commit d4c1086

Browse files
committed
Add Helper Methods for Inserts
1 parent dc34fcd commit d4c1086

File tree

2 files changed

+59
-40
lines changed

2 files changed

+59
-40
lines changed

src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3Utils.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,26 @@
1515
*/
1616
package org.mybatis.dynamic.sql.util.mybatis3;
1717

18+
import java.util.Collection;
1819
import java.util.List;
1920
import java.util.Optional;
2021
import java.util.function.Function;
2122
import java.util.function.ToIntFunction;
2223
import java.util.function.ToLongFunction;
24+
import java.util.function.UnaryOperator;
2325

2426
import org.mybatis.dynamic.sql.BasicColumn;
2527
import org.mybatis.dynamic.sql.SqlBuilder;
2628
import org.mybatis.dynamic.sql.SqlTable;
2729
import org.mybatis.dynamic.sql.delete.DeleteDSL;
2830
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
31+
import org.mybatis.dynamic.sql.insert.InsertDSL;
32+
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
33+
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
34+
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
2935
import org.mybatis.dynamic.sql.render.RenderingStrategy;
3036
import org.mybatis.dynamic.sql.select.CompletableQuery;
37+
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
3138
import org.mybatis.dynamic.sql.select.SelectDSL;
3239
import org.mybatis.dynamic.sql.select.SelectModel;
3340
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
@@ -45,10 +52,12 @@ private MyBatis3Utils() {}
4552

4653
public static long count(ToLongFunction<SelectStatementProvider> mapper,
4754
SqlTable table, MyBatis3SelectHelper helper) {
48-
return mapper.applyAsLong(
49-
helper.apply(SelectDSL.select(SqlBuilder.count()).from(table))
50-
.build()
51-
.render(RenderingStrategy.MYBATIS3));
55+
return count(mapper, SelectDSL.select(SqlBuilder.count()).from(table), helper);
56+
}
57+
58+
public static long count(ToLongFunction<SelectStatementProvider> mapper,
59+
QueryExpressionDSL<SelectModel> start, MyBatis3SelectHelper helper) {
60+
return mapper.applyAsLong(helper.apply(start).build().render(RenderingStrategy.MYBATIS3));
5261
}
5362

5463
public static int deleteFrom(ToIntFunction<DeleteStatementProvider> mapper,
@@ -59,6 +68,18 @@ public static int deleteFrom(ToIntFunction<DeleteStatementProvider> mapper,
5968
.render(RenderingStrategy.MYBATIS3));
6069
}
6170

71+
public static <R> int insert(ToIntFunction<InsertStatementProvider<R>> mapper, R record,
72+
SqlTable table, UnaryOperator<InsertDSL<R>> helper) {
73+
return mapper.applyAsInt(helper.apply(
74+
InsertDSL.insert(record).into(table)).build().render(RenderingStrategy.MYBATIS3));
75+
}
76+
77+
public static <R> int insertMultiple(ToIntFunction<MultiRowInsertStatementProvider<R>> mapper,
78+
Collection<R> records, SqlTable table, UnaryOperator<MultiRowInsertDSL<R>> helper) {
79+
return mapper.applyAsInt(helper.apply(
80+
MultiRowInsertDSL.insert(records).into(table)).build().render(RenderingStrategy.MYBATIS3));
81+
}
82+
6283
public static <R> List<R> selectDistinct(Function<SelectStatementProvider, List<R>> mapper,
6384
BasicColumn[] selectList, SqlTable table, MyBatis3SelectHelper helper) {
6485
return selectDistinct(mapper, SelectDSL.selectDistinct(selectList).from(table), helper);

src/test/java/examples/simple/PersonMapper.java

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import static examples.simple.PersonDynamicSqlSupport.*;
1919
import static org.mybatis.dynamic.sql.SqlBuilder.*;
2020

21+
import java.util.Arrays;
22+
import java.util.Collection;
2123
import java.util.List;
2224
import java.util.Optional;
2325

@@ -31,11 +33,9 @@
3133
import org.apache.ibatis.annotations.UpdateProvider;
3234
import org.apache.ibatis.type.JdbcType;
3335
import org.mybatis.dynamic.sql.BasicColumn;
34-
import org.mybatis.dynamic.sql.SqlBuilder;
3536
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
3637
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
3738
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
38-
import org.mybatis.dynamic.sql.render.RenderingStrategy;
3939
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
4040
import org.mybatis.dynamic.sql.update.UpdateDSL;
4141
import org.mybatis.dynamic.sql.update.UpdateModel;
@@ -101,45 +101,43 @@ default int deleteByPrimaryKey(Integer id_) {
101101
}
102102

103103
default int insert(PersonRecord record) {
104-
return insert(SqlBuilder.insert(record)
105-
.into(person)
106-
.map(id).toProperty("id")
107-
.map(firstName).toProperty("firstName")
108-
.map(lastName).toProperty("lastName")
109-
.map(birthDate).toProperty("birthDate")
110-
.map(employed).toProperty("employed")
111-
.map(occupation).toProperty("occupation")
112-
.map(addressId).toProperty("addressId")
113-
.build()
114-
.render(RenderingStrategy.MYBATIS3));
104+
return MyBatis3Utils.insert(this::insert, record, person, h ->
105+
h.map(id).toProperty("id")
106+
.map(firstName).toProperty("firstName")
107+
.map(lastName).toProperty("lastName")
108+
.map(birthDate).toProperty("birthDate")
109+
.map(employed).toProperty("employed")
110+
.map(occupation).toProperty("occupation")
111+
.map(addressId).toProperty("addressId")
112+
);
115113
}
116114

117-
default int insertMultiple(List<PersonRecord> records) {
118-
return insertMultiple(SqlBuilder.insertMultiple(records)
119-
.into(person)
120-
.map(id).toProperty("id")
121-
.map(firstName).toProperty("firstName")
122-
.map(lastName).toProperty("lastName")
123-
.map(birthDate).toProperty("birthDate")
124-
.map(employed).toProperty("employed")
125-
.map(occupation).toProperty("occupation")
126-
.map(addressId).toProperty("addressId")
127-
.build()
128-
.render(RenderingStrategy.MYBATIS3));
115+
default int insertMultiple(PersonRecord...records) {
116+
return insertMultiple(Arrays.asList(records));
117+
}
118+
119+
default int insertMultiple(Collection<PersonRecord> records) {
120+
return MyBatis3Utils.insertMultiple(this::insertMultiple, records, person, h ->
121+
h.map(id).toProperty("id")
122+
.map(firstName).toProperty("firstName")
123+
.map(lastName).toProperty("lastName")
124+
.map(birthDate).toProperty("birthDate")
125+
.map(employed).toProperty("employed")
126+
.map(occupation).toProperty("occupation")
127+
.map(addressId).toProperty("addressId")
128+
);
129129
}
130130

131131
default int insertSelective(PersonRecord record) {
132-
return insert(SqlBuilder.insert(record)
133-
.into(person)
134-
.map(id).toPropertyWhenPresent("id", record::getId)
135-
.map(firstName).toPropertyWhenPresent("firstName", record::getFirstName)
136-
.map(lastName).toPropertyWhenPresent("lastName", record::getLastName)
137-
.map(birthDate).toPropertyWhenPresent("birthDate", record::getBirthDate)
138-
.map(employed).toPropertyWhenPresent("employed", record::getEmployed)
139-
.map(occupation).toPropertyWhenPresent("occupation", record::getOccupation)
140-
.map(addressId).toPropertyWhenPresent("addressId", record::getAddressId)
141-
.build()
142-
.render(RenderingStrategy.MYBATIS3));
132+
return MyBatis3Utils.insert(this::insert, record, person, h ->
133+
h.map(id).toPropertyWhenPresent("id", record::getId)
134+
.map(firstName).toPropertyWhenPresent("firstName", record::getFirstName)
135+
.map(lastName).toPropertyWhenPresent("lastName", record::getLastName)
136+
.map(birthDate).toPropertyWhenPresent("birthDate", record::getBirthDate)
137+
.map(employed).toPropertyWhenPresent("employed", record::getEmployed)
138+
.map(occupation).toPropertyWhenPresent("occupation", record::getOccupation)
139+
.map(addressId).toPropertyWhenPresent("addressId", record::getAddressId)
140+
);
143141
}
144142

145143
static BasicColumn[] selectList =

0 commit comments

Comments
 (0)