Skip to content

Commit a364dae

Browse files
committed
Add shortcut for creating columns and update docs
1 parent 51c27cb commit a364dae

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

README.md

+9-13
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,16 @@ import org.mybatis.dynamic.sql.SqlColumn;
109109
import org.mybatis.dynamic.sql.SqlTable;
110110

111111
public interface SimpleTableDynamicSqlSupport {
112-
SqlTable simpleTable = SqlTable.of("SimpleTable").withAlias("a");
113-
SqlColumn<Integer> id = SqlColumn.of("id", JDBCType.INTEGER).inTable(simpleTable).withAlias("A_ID");
114-
SqlColumn<String> firstName = SqlColumn.of("first_name", JDBCType.VARCHAR).inTable(simpleTable);
115-
SqlColumn<String> lastName = SqlColumn.of("last_name", JDBCType.VARCHAR).inTable(simpleTable);
116-
SqlColumn<Date> birthDate = SqlColumn.of("birth_date", JDBCType.DATE).inTable(simpleTable);
117-
SqlColumn<Boolean> employed = SqlColumn.of("employed", JDBCType.VARCHAR).withTypeHandler("examples.simple.YesNoTypeHandler").inTable(simpleTable);
118-
SqlColumn<String> occupation = SqlColumn.of("occupation", JDBCType.VARCHAR).inTable(simpleTable);
112+
SqlTable simpleTable = SqlTable.of("SimpleTable");
113+
SqlColumn<Integer> id = simpleTable.column("id", JDBCType.INTEGER).withAlias("A_ID");
114+
SqlColumn<String> firstName = simpleTable.column("first_name", JDBCType.VARCHAR);
115+
SqlColumn<String> lastName = simpleTable.column("last_name", JDBCType.VARCHAR);
116+
SqlColumn<Date> birthDate = simpleTable.column("birth_date", JDBCType.DATE);
117+
SqlColumn<Boolean> employed = simpleTable.column("employed", JDBCType.VARCHAR).withTypeHandler("examples.simple.YesNoTypeHandler");
118+
SqlColumn<String> occupation = simpleTable.column("occupation", JDBCType.VARCHAR);
119119
}
120120
```
121121

122-
Note that the table definition is not required unless you want to give an alias to the table in the
123-
SQL. In that case, the table definition allows you to specify a table alias. The library will ignore
124-
the alias for DELETE, INSERT, and UPDATE statements, but will honor the alias for SELECT statements.
125-
126122
### Second - Write XML or annotated mappers that will use the generated where clause
127123
The library will create support classes that will be used as input to an annotated or XML mapper. These classes include the generated where clause, as well as a parameter set that will match the generated clause. Both are required by MyBatis3. It is intended that these objects be the one and only parameter to a MyBatis method.
128124

@@ -199,11 +195,11 @@ For example, a very simple condition can be defined like this:
199195
.render(RenderingStrategy.MYBATIS3);
200196
```
201197

202-
Or this:
198+
Or this (also note that you can give a table an alias):
203199

204200
```java
205201
SelectSupport selectSupport = select(count())
206-
.from(simpleTable)
202+
.from(simpleTable, "a")
207203
.where(id, isNull())
208204
.build()
209205
.render(RenderingStrategy.MYBATIS3);

src/main/java/org/mybatis/dynamic/sql/SqlTable.java

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.mybatis.dynamic.sql;
1717

18+
import java.sql.JDBCType;
19+
1820
public class SqlTable {
1921

2022
private String name;
@@ -26,6 +28,10 @@ private SqlTable(String name) {
2628
public String name() {
2729
return name;
2830
}
31+
32+
public <T> SqlColumn<T> column(String name, JDBCType jdbcType) {
33+
return SqlColumn.of(this, name, jdbcType);
34+
}
2935

3036
public static SqlTable of(String name) {
3137
return new SqlTable(name);

src/test/java/examples/animal/data/AnimalDataDynamicSqlSupport.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
public interface AnimalDataDynamicSqlSupport {
2424
SqlTable animalData = SqlTable.of("AnimalData");
25-
SqlColumn<Integer> id = SqlColumn.of(animalData, "id", JDBCType.INTEGER);
26-
SqlColumn<String> animalName = SqlColumn.of(animalData, "animal_name", JDBCType.VARCHAR);
27-
SqlColumn<Double> bodyWeight = SqlColumn.of(animalData, "body_weight", JDBCType.DOUBLE);
28-
SqlColumn<Double> brainWeight = SqlColumn.of(animalData, "brain_weight", JDBCType.DOUBLE);
25+
SqlColumn<Integer> id = animalData.column("id", JDBCType.INTEGER);
26+
SqlColumn<String> animalName = animalData.column("animal_name", JDBCType.VARCHAR);
27+
SqlColumn<Double> bodyWeight = animalData.column("body_weight", JDBCType.DOUBLE);
28+
SqlColumn<Double> brainWeight = animalData.column("brain_weight", JDBCType.DOUBLE);
2929
}

src/test/java/examples/simple/SampleWhereClausesTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public class SampleWhereClausesTest {
3232
@Test
3333
public void simpleClause1() {
3434
SelectSupport selectSupport = select(count())
35-
.from(simpleTable, "a")
35+
.from(simpleTable)
3636
.where(id, isEqualTo(3))
3737
.build()
3838
.render(RenderingStrategy.MYBATIS3);
3939

4040
assertThat(selectSupport.getWhereClause())
41-
.isEqualTo("where a.id = #{parameters.p1,jdbcType=INTEGER}");
41+
.isEqualTo("where id = #{parameters.p1,jdbcType=INTEGER}");
4242
}
4343

4444
@Test

src/test/java/examples/simple/SimpleTableDynamicSqlSupport.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333

3434
public interface SimpleTableDynamicSqlSupport {
3535
SqlTable simpleTable = SqlTable.of("SimpleTable");
36-
SqlColumn<Integer> id = SqlColumn.of(simpleTable, "id", JDBCType.INTEGER).withAlias("A_ID");
37-
SqlColumn<String> firstName = SqlColumn.of(simpleTable, "first_name", JDBCType.VARCHAR);
38-
SqlColumn<String> lastName = SqlColumn.of(simpleTable, "last_name", JDBCType.VARCHAR);
39-
SqlColumn<Date> birthDate = SqlColumn.of(simpleTable, "birth_date", JDBCType.DATE);
40-
SqlColumn<Boolean> employed = SqlColumn.of(simpleTable, "employed", JDBCType.VARCHAR).withTypeHandler("examples.simple.YesNoTypeHandler");
41-
SqlColumn<String> occupation = SqlColumn.of(simpleTable, "occupation", JDBCType.VARCHAR);
36+
SqlColumn<Integer> id = simpleTable.column("id", JDBCType.INTEGER).withAlias("A_ID");
37+
SqlColumn<String> firstName = simpleTable.column("first_name", JDBCType.VARCHAR);
38+
SqlColumn<String> lastName = simpleTable.column("last_name", JDBCType.VARCHAR);
39+
SqlColumn<Date> birthDate = simpleTable.column("birth_date", JDBCType.DATE);
40+
SqlColumn<Boolean> employed = simpleTable.column("employed", JDBCType.VARCHAR).withTypeHandler("examples.simple.YesNoTypeHandler");
41+
SqlColumn<String> occupation = simpleTable.column("occupation", JDBCType.VARCHAR);
4242

4343
static InsertSupport<SimpleTableRecord> buildFullInsertSupport(SimpleTableRecord record) {
4444
return insert(record)

0 commit comments

Comments
 (0)