diff --git a/pom.xml b/pom.xml index 6d98ded..dc4007b 100644 --- a/pom.xml +++ b/pom.xml @@ -54,7 +54,7 @@ 1.4.1 3.2.1 3.5.0 - 3.1.3 + 3.2.0 diff --git a/spring-data-criteria-apt/src/main/java/io/github/holmofy/data/apt/CriteriaGenerator.java b/spring-data-criteria-apt/src/main/java/io/github/holmofy/data/apt/CriteriaGenerator.java index 5012daf..800593b 100644 --- a/spring-data-criteria-apt/src/main/java/io/github/holmofy/data/apt/CriteriaGenerator.java +++ b/spring-data-criteria-apt/src/main/java/io/github/holmofy/data/apt/CriteriaGenerator.java @@ -9,6 +9,7 @@ import org.springframework.data.relational.core.mapping.Column; import org.springframework.data.relational.core.mapping.Embedded; import org.springframework.data.relational.core.mapping.Table; +import org.springframework.data.relational.core.sql.SQL; import org.springframework.util.StringUtils; import javax.annotation.processing.Generated; @@ -74,7 +75,8 @@ public class TableModel { public static final String SPRING_DATA_PACKAGE = "org.springframework.data.relational.core.sql"; - private static final Converter columnConverter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE); + private static final Converter LOWER_UNDERSCORE = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE); + private static final Converter UPPER_UNDERSCORE = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_UNDERSCORE); private PackageElement packageElement; @@ -97,8 +99,11 @@ public JavaFile generateSource() { // table name String tableName = getTableName(); builder.addField(FieldSpec.builder( - ClassName.get("java.lang", "String"), "TABLE", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL + ClassName.get(String.class), "TABLE_NAME", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL ).initializer("$S", tableName).build()); + builder.addField(FieldSpec.builder( + ClassName.get(org.springframework.data.relational.core.sql.Table.class), "TABLE", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL + ).initializer("$T.table(TABLE_NAME)", SQL.class).build()); addFields(builder, this.fields, ""); @@ -135,10 +140,14 @@ private void addField(TypeSpec.Builder builder, VariableElement field, String co String column_name = Optional.of(field).map(f -> f.getAnnotation(Column.class)) .map(Column::value) .filter(s -> !s.isBlank()) - .orElse(columnConverter.convert(fieldName)); + .orElse(LOWER_UNDERSCORE.convert(fieldName)); builder.addField(FieldSpec.builder( - ClassName.get("java.lang", "String"), fieldName, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL + ClassName.get(String.class), fieldName, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL ).initializer("$S", columnPrefix + column_name).build()); + String COLUMN_NAME = Objects.requireNonNull(UPPER_UNDERSCORE.convert(fieldName)); + builder.addField(FieldSpec.builder( + ClassName.get(org.springframework.data.relational.core.sql.Column.class), COLUMN_NAME, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL + ).initializer("$T.column($L, TABLE)", SQL.class, fieldName).build()); } public String getGenerateClassName() { @@ -155,7 +164,7 @@ private String getTableName() { return annotation.value(); } } - return columnConverter.convert(classElement.getSimpleName().toString()); + return LOWER_UNDERSCORE.convert(classElement.getSimpleName().toString()); } } diff --git a/spring-data-criteria-apt/src/test/resources/TestModel_.java b/spring-data-criteria-apt/src/test/resources/TestModel_.java index 9819d22..2ca4711 100644 --- a/spring-data-criteria-apt/src/test/resources/TestModel_.java +++ b/spring-data-criteria-apt/src/test/resources/TestModel_.java @@ -2,13 +2,27 @@ import java.lang.String; import javax.annotation.processing.Generated; +import org.springframework.data.relational.core.sql.Column; +import org.springframework.data.relational.core.sql.SQL; +import org.springframework.data.relational.core.sql.Table; @Generated("io.github.holmofy.data.apt.SpringDataRelationalAnnotationProcessor") public final class TestModel_ { - public static final String TABLE = "test_model"; + public static final String TABLE_NAME = "test_model"; + public static final Table TABLE = SQL.table(TABLE_NAME); + public static final String id = "id"; + public static final Column ID = SQL.column(id, TABLE); + public static final String withColumn = "named_column"; + public static final Column WITH_COLUMN = SQL.column(withColumn, TABLE); + public static final String lowerCamel = "lower_camel"; + public static final Column LOWER_CAMEL = SQL.column(lowerCamel, TABLE); + public static final String field1 = "embedded_field1"; + public static final Column FIELD1 = SQL.column(field1, TABLE); + public static final String field2 = "embedded_field_2"; + public static final Column FIELD2 = SQL.column(field2, TABLE); } \ No newline at end of file diff --git a/spring-data-criteria-example/spring-data-criteria-jdbc-example/src/main/java/io/github/holmofy/data/jdbc/controller/UserController.java b/spring-data-criteria-example/spring-data-criteria-jdbc-example/src/main/java/io/github/holmofy/data/jdbc/controller/UserController.java index 3515fa5..d42a6a9 100644 --- a/spring-data-criteria-example/spring-data-criteria-jdbc-example/src/main/java/io/github/holmofy/data/jdbc/controller/UserController.java +++ b/spring-data-criteria-example/spring-data-criteria-jdbc-example/src/main/java/io/github/holmofy/data/jdbc/controller/UserController.java @@ -21,9 +21,14 @@ public Page search(UserDao.UserQuery query, Pageable pageable) { return userDao.searchByQuery(query, pageable); } - @GetMapping("/search-list") - public List search(UserDao.UserQuery query) { - return userDao.searchByQuery(query); + @GetMapping("/search-list1") + public List search1(UserDao.UserQuery query) { + return userDao.searchByQuery1(query); + } + + @GetMapping("/search-list2") + public List search2(UserDao.UserQuery query) { + return userDao.searchByQuery2(query); } } diff --git a/spring-data-criteria-example/spring-data-criteria-jdbc-example/src/main/java/io/github/holmofy/data/jdbc/dao/UserDao.java b/spring-data-criteria-example/spring-data-criteria-jdbc-example/src/main/java/io/github/holmofy/data/jdbc/dao/UserDao.java index 699a842..b819a01 100644 --- a/spring-data-criteria-example/spring-data-criteria-jdbc-example/src/main/java/io/github/holmofy/data/jdbc/dao/UserDao.java +++ b/spring-data-criteria-example/spring-data-criteria-jdbc-example/src/main/java/io/github/holmofy/data/jdbc/dao/UserDao.java @@ -9,6 +9,10 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.relational.core.query.Criteria; +import org.springframework.data.relational.core.sql.Conditions; +import org.springframework.data.relational.core.sql.SQL; +import org.springframework.data.relational.core.sql.Select; +import org.springframework.data.relational.core.sql.render.SqlRenderer; import org.springframework.data.repository.ListCrudRepository; import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; @@ -17,7 +21,19 @@ public interface UserDao extends ListCrudRepository, CriteriaExecutor, JdbcSupport { - default List searchByQuery(UserQuery query) { + default List searchByQuery1(UserQuery query) { + Select sql = Select.builder().select() + .from(User_.TABLE) + .where(Conditions.isEqual(User_.PROVINCE, SQL.bindMarker(":province"))) + .and(Conditions.isEqual(User_.CITY, SQL.bindMarker(":city"))) + .and(Conditions.isEqual(User_.AREA, SQL.bindMarker(":area"))) + .and(Conditions.isEqual(User_.NAME, SQL.bindMarker(":nick"))) + .build(); + return namedJdbcTemplate().queryForList(SqlRenderer.toString(sql) + , new BeanPropertySqlParameterSource(query), User.class); + } + + default List searchByQuery2(UserQuery query) { return namedJdbcTemplate().queryForList( "select * " + "from t_user " +