From fdb545fd185c6a4acf64e9b7c996867bbf21fa0e Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 9 Mar 2018 16:36:45 +0100 Subject: [PATCH] #314 - Polishing. Extracted MyBatis Boot Starter version int property in pom.xml. Removed unused code, more use of Lombok, formatting. Added new lines at end of files where missing. Original pull request: #345. --- jdbc/mybatis/pom.xml | 12 +++++++++--- .../example/springdata/jdbc/mybatis/LegoSet.java | 13 ++----------- .../springdata/jdbc/mybatis/LegoSetRepository.java | 4 +--- .../example/springdata/jdbc/mybatis/Manual.java | 3 +-- .../example/springdata/jdbc/mybatis/Model.java | 4 +--- .../springdata/jdbc/mybatis/ModelMapEntry.java | 14 ++++---------- .../src/main/resources/application.properties | 2 +- .../example/springdata/jdbc/mybatis/LegoSet.xml | 11 +++-------- .../example/springdata/jdbc/mybatis/Model.xml | 9 +++------ .../springdata/jdbc/mybatis/MyBatisTests.java | 8 ++++++-- jdbc/pom.xml | 2 +- 11 files changed, 32 insertions(+), 50 deletions(-) diff --git a/jdbc/mybatis/pom.xml b/jdbc/mybatis/pom.xml index 0ff45643..f890ecdc 100644 --- a/jdbc/mybatis/pom.xml +++ b/jdbc/mybatis/pom.xml @@ -16,18 +16,24 @@ Spring Data JDBC - Using MyBatis for defining queries Sample project demonstrating Spring Data JDBC features + + 1.3.1 + + + org.mybatis.spring.boot mybatis-spring-boot-starter - 1.3.1 + ${mybatis.version} + org.mybatis.spring.boot mybatis-spring-boot-starter-test - 1.3.1 + ${mybatis.version} test - \ No newline at end of file + diff --git a/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/LegoSet.java b/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/LegoSet.java index 6128e046..c77a0fc8 100644 --- a/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/LegoSet.java +++ b/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/LegoSet.java @@ -17,8 +17,6 @@ package example.springdata.jdbc.mybatis; import lombok.Data; -import java.time.Period; -import java.time.temporal.ChronoUnit; import java.util.HashMap; import java.util.Map; @@ -34,18 +32,11 @@ public class LegoSet { // You can build multiple models from one LegoSet private final Map models = new HashMap<>(); - @Id private Integer id; + + private @Id Integer id; private String name; private Manual manual; - private static int toInt(Period period) { - return (int) (period == null ? 0 : period.get(ChronoUnit.YEARS)); - } - - private static Period toPeriod(int years) { - return Period.ofYears(years); - } - public void addModel(String name, String description) { Model model = new Model(); diff --git a/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/LegoSetRepository.java b/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/LegoSetRepository.java index 356ca1bf..6b2f1458 100644 --- a/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/LegoSetRepository.java +++ b/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/LegoSetRepository.java @@ -22,6 +22,4 @@ import org.springframework.data.repository.CrudRepository; * * @author Jens Schauder */ -public interface LegoSetRepository extends CrudRepository { - -} +public interface LegoSetRepository extends CrudRepository {} diff --git a/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/Manual.java b/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/Manual.java index dee2fd32..95d21609 100644 --- a/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/Manual.java +++ b/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/Manual.java @@ -27,13 +27,12 @@ import org.springframework.data.annotation.Id; @Data public class Manual { - @Id private Long id; + private @Id Long id = null; private String author; private String text; Manual(String text, String author) { - this.id = null; this.author = author; this.text = text; } diff --git a/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/Model.java b/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/Model.java index b7e8f409..a2b33f5d 100644 --- a/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/Model.java +++ b/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/Model.java @@ -30,9 +30,7 @@ import org.springframework.lang.Nullable; @ToString public class Model implements Persistable { - String name; - - String description; + String name, description; @Nullable @Override diff --git a/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/ModelMapEntry.java b/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/ModelMapEntry.java index da61ea98..a0541365 100644 --- a/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/ModelMapEntry.java +++ b/jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/ModelMapEntry.java @@ -15,6 +15,8 @@ */ package example.springdata.jdbc.mybatis; +import lombok.Getter; + import java.sql.Clob; import java.sql.SQLException; import java.util.Map; @@ -22,6 +24,7 @@ import java.util.Map; /** * @author Jens Schauder */ +@Getter public class ModelMapEntry implements Map.Entry { private final String key; @@ -32,6 +35,7 @@ public class ModelMapEntry implements Map.Entry { key = name; value = new Model(); value.name = name; + try { value.description = description.getSubString(1, (int) description.length()); } catch (SQLException se) { @@ -39,16 +43,6 @@ public class ModelMapEntry implements Map.Entry { } } - @Override - public String getKey() { - return key; - } - - @Override - public Model getValue() { - return value; - } - @Override public Model setValue(Model value) { throw new UnsupportedOperationException("can't set the value of a ModelMapEntry"); diff --git a/jdbc/mybatis/src/main/resources/application.properties b/jdbc/mybatis/src/main/resources/application.properties index 1fce1e07..1fb23164 100644 --- a/jdbc/mybatis/src/main/resources/application.properties +++ b/jdbc/mybatis/src/main/resources/application.properties @@ -1,4 +1,4 @@ logging.level.org.springframework.data=INFO logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG logging.level.example.springdata.jdbc.mybatis=TRACE -mybatis.config-location=mybatis-config.xml \ No newline at end of file +mybatis.config-location=mybatis-config.xml diff --git a/jdbc/mybatis/src/main/resources/example/springdata/jdbc/mybatis/LegoSet.xml b/jdbc/mybatis/src/main/resources/example/springdata/jdbc/mybatis/LegoSet.xml index b174a89e..350756db 100644 --- a/jdbc/mybatis/src/main/resources/example/springdata/jdbc/mybatis/LegoSet.xml +++ b/jdbc/mybatis/src/main/resources/example/springdata/jdbc/mybatis/LegoSet.xml @@ -1,13 +1,8 @@ - + - + SELECT name, description FROM Model WHERE legoset = #{id} diff --git a/jdbc/mybatis/src/main/resources/example/springdata/jdbc/mybatis/Model.xml b/jdbc/mybatis/src/main/resources/example/springdata/jdbc/mybatis/Model.xml index 66cc3c4b..4f448bae 100644 --- a/jdbc/mybatis/src/main/resources/example/springdata/jdbc/mybatis/Model.xml +++ b/jdbc/mybatis/src/main/resources/example/springdata/jdbc/mybatis/Model.xml @@ -1,11 +1,8 @@ - - + - + INSERT INTO Model (name, description, legoset) VALUES (#{instance.name}, #{instance.description}, #{parentId}) - \ No newline at end of file + diff --git a/jdbc/mybatis/src/test/java/example/springdata/jdbc/mybatis/MyBatisTests.java b/jdbc/mybatis/src/test/java/example/springdata/jdbc/mybatis/MyBatisTests.java index f3f402ec..668da354 100644 --- a/jdbc/mybatis/src/test/java/example/springdata/jdbc/mybatis/MyBatisTests.java +++ b/jdbc/mybatis/src/test/java/example/springdata/jdbc/mybatis/MyBatisTests.java @@ -36,17 +36,19 @@ import org.springframework.test.context.junit4.SpringRunner; @MybatisTest public class MyBatisTests { - @Autowired private LegoSetRepository repository; + @Autowired LegoSetRepository repository; @Test public void exerciseSomewhatComplexEntity() { LegoSet smallCar = createLegoSet(); + smallCar.setManual(new Manual("Just put all the pieces together in the right order", "Jens Schauder")); smallCar.addModel("suv", "SUV with sliding doors."); smallCar.addModel("roadster", "Slick red roadster."); repository.save(smallCar); + assertThat(smallCar.getId()).isNotNull(); assertThat(repository.findById(smallCar.getId()).get().getModels()).hasSize(2); @@ -56,15 +58,17 @@ public class MyBatisTests { smallCar.addModel("pickup", "A pickup truck with some tools in the back."); repository.save(smallCar); + Output.list(repository.findAll(), "Updated"); smallCar.setManual(new Manual("One last attempt: Just build a car! Ok?", "Jens Schauder")); repository.save(smallCar); + Output.list(repository.findAll(), "Manual replaced"); } - private LegoSet createLegoSet() { + private static LegoSet createLegoSet() { LegoSet smallCar = new LegoSet(); smallCar.setName("Small Car 01"); diff --git a/jdbc/pom.xml b/jdbc/pom.xml index 0b50b4b9..c4a5fa1f 100644 --- a/jdbc/pom.xml +++ b/jdbc/pom.xml @@ -18,7 +18,7 @@ basics - mybatis + mybatis