#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.
This commit is contained in:
Oliver Gierke
2018-03-09 16:36:45 +01:00
parent 047610df5f
commit fdb545fd18
11 changed files with 32 additions and 50 deletions

View File

@@ -16,18 +16,24 @@
<name>Spring Data JDBC - Using MyBatis for defining queries</name> <name>Spring Data JDBC - Using MyBatis for defining queries</name>
<description>Sample project demonstrating Spring Data JDBC features</description> <description>Sample project demonstrating Spring Data JDBC features</description>
<properties>
<mybatis.version>1.3.1</mybatis.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId> <groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId> <artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version> <version>${mybatis.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId> <groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId> <artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>1.3.1</version> <version>${mybatis.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -17,8 +17,6 @@ package example.springdata.jdbc.mybatis;
import lombok.Data; import lombok.Data;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -34,18 +32,11 @@ public class LegoSet {
// You can build multiple models from one LegoSet // You can build multiple models from one LegoSet
private final Map<String, Model> models = new HashMap<>(); private final Map<String, Model> models = new HashMap<>();
@Id private Integer id;
private @Id Integer id;
private String name; private String name;
private Manual manual; 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) { public void addModel(String name, String description) {
Model model = new Model(); Model model = new Model();

View File

@@ -22,6 +22,4 @@ import org.springframework.data.repository.CrudRepository;
* *
* @author Jens Schauder * @author Jens Schauder
*/ */
public interface LegoSetRepository extends CrudRepository<LegoSet, Integer> { public interface LegoSetRepository extends CrudRepository<LegoSet, Integer> {}
}

View File

@@ -27,13 +27,12 @@ import org.springframework.data.annotation.Id;
@Data @Data
public class Manual { public class Manual {
@Id private Long id; private @Id Long id = null;
private String author; private String author;
private String text; private String text;
Manual(String text, String author) { Manual(String text, String author) {
this.id = null;
this.author = author; this.author = author;
this.text = text; this.text = text;
} }

View File

@@ -30,9 +30,7 @@ import org.springframework.lang.Nullable;
@ToString @ToString
public class Model implements Persistable<String> { public class Model implements Persistable<String> {
String name; String name, description;
String description;
@Nullable @Nullable
@Override @Override

View File

@@ -15,6 +15,8 @@
*/ */
package example.springdata.jdbc.mybatis; package example.springdata.jdbc.mybatis;
import lombok.Getter;
import java.sql.Clob; import java.sql.Clob;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Map; import java.util.Map;
@@ -22,6 +24,7 @@ import java.util.Map;
/** /**
* @author Jens Schauder * @author Jens Schauder
*/ */
@Getter
public class ModelMapEntry implements Map.Entry<String, Model> { public class ModelMapEntry implements Map.Entry<String, Model> {
private final String key; private final String key;
@@ -32,6 +35,7 @@ public class ModelMapEntry implements Map.Entry<String, Model> {
key = name; key = name;
value = new Model(); value = new Model();
value.name = name; value.name = name;
try { try {
value.description = description.getSubString(1, (int) description.length()); value.description = description.getSubString(1, (int) description.length());
} catch (SQLException se) { } catch (SQLException se) {
@@ -39,16 +43,6 @@ public class ModelMapEntry implements Map.Entry<String, Model> {
} }
} }
@Override
public String getKey() {
return key;
}
@Override
public Model getValue() {
return value;
}
@Override @Override
public Model setValue(Model value) { public Model setValue(Model value) {
throw new UnsupportedOperationException("can't set the value of a ModelMapEntry"); throw new UnsupportedOperationException("can't set the value of a ModelMapEntry");

View File

@@ -1,4 +1,4 @@
logging.level.org.springframework.data=INFO logging.level.org.springframework.data=INFO
logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
logging.level.example.springdata.jdbc.mybatis=TRACE logging.level.example.springdata.jdbc.mybatis=TRACE
mybatis.config-location=mybatis-config.xml mybatis.config-location=mybatis-config.xml

View File

@@ -1,13 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="example.springdata.jdbc.mybatis.LegoSetMapper"> <mapper namespace="example.springdata.jdbc.mybatis.LegoSetMapper">
<select id="findAllByProperty-models" resultType="ModelMapEntry" <select id="findAllByProperty-models" resultType="ModelMapEntry" parameterType="org.springframework.data.jdbc.mybatis.MyBatisContext">
parameterType="org.springframework.data.jdbc.mybatis.MyBatisContext"> SELECT name, description
SELECT
name,
description
FROM Model FROM Model
WHERE legoset = #{id} WHERE legoset = #{id}
</select> </select>

View File

@@ -1,11 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="example.springdata.jdbc.mybatis.ModelMapper"> <mapper namespace="example.springdata.jdbc.mybatis.ModelMapper">
<insert id="insert" parameterType="org.springframework.data.jdbc.mybatis.MyBatisContext"> <insert id="insert" parameterType="org.springframework.data.jdbc.mybatis.MyBatisContext">
<bind name="parentId" value="_parameter.get('LegoSet')"/> <bind name="parentId" value="_parameter.get('LegoSet')" />
INSERT INTO Model (name, description, legoset) VALUES (#{instance.name}, #{instance.description}, #{parentId}) INSERT INTO Model (name, description, legoset) VALUES (#{instance.name}, #{instance.description}, #{parentId})
</insert> </insert>
</mapper> </mapper>

View File

@@ -36,17 +36,19 @@ import org.springframework.test.context.junit4.SpringRunner;
@MybatisTest @MybatisTest
public class MyBatisTests { public class MyBatisTests {
@Autowired private LegoSetRepository repository; @Autowired LegoSetRepository repository;
@Test @Test
public void exerciseSomewhatComplexEntity() { public void exerciseSomewhatComplexEntity() {
LegoSet smallCar = createLegoSet(); LegoSet smallCar = createLegoSet();
smallCar.setManual(new Manual("Just put all the pieces together in the right order", "Jens Schauder")); 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("suv", "SUV with sliding doors.");
smallCar.addModel("roadster", "Slick red roadster."); smallCar.addModel("roadster", "Slick red roadster.");
repository.save(smallCar); repository.save(smallCar);
assertThat(smallCar.getId()).isNotNull(); assertThat(smallCar.getId()).isNotNull();
assertThat(repository.findById(smallCar.getId()).get().getModels()).hasSize(2); 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."); smallCar.addModel("pickup", "A pickup truck with some tools in the back.");
repository.save(smallCar); repository.save(smallCar);
Output.list(repository.findAll(), "Updated"); Output.list(repository.findAll(), "Updated");
smallCar.setManual(new Manual("One last attempt: Just build a car! Ok?", "Jens Schauder")); smallCar.setManual(new Manual("One last attempt: Just build a car! Ok?", "Jens Schauder"));
repository.save(smallCar); repository.save(smallCar);
Output.list(repository.findAll(), "Manual replaced"); Output.list(repository.findAll(), "Manual replaced");
} }
private LegoSet createLegoSet() { private static LegoSet createLegoSet() {
LegoSet smallCar = new LegoSet(); LegoSet smallCar = new LegoSet();
smallCar.setName("Small Car 01"); smallCar.setName("Small Car 01");

View File

@@ -18,7 +18,7 @@
<modules> <modules>
<module>basics</module> <module>basics</module>
<module>mybatis</module> <module>mybatis</module>
</modules> </modules>
<properties> <properties>