#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>
<description>Sample project demonstrating Spring Data JDBC features</description>
<properties>
<mybatis.version>1.3.1</mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>1.3.1</version>
<version>${mybatis.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>

View File

@@ -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<String, Model> 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();

View File

@@ -22,6 +22,4 @@ import org.springframework.data.repository.CrudRepository;
*
* @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
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;
}

View File

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

View File

@@ -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<String, Model> {
private final String key;
@@ -32,6 +35,7 @@ public class ModelMapEntry implements Map.Entry<String, Model> {
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<String, Model> {
}
}
@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");

View File

@@ -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
mybatis.config-location=mybatis-config.xml

View File

@@ -1,13 +1,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.LegoSetMapper">
<select id="findAllByProperty-models" resultType="ModelMapEntry"
parameterType="org.springframework.data.jdbc.mybatis.MyBatisContext">
SELECT
name,
description
<select id="findAllByProperty-models" resultType="ModelMapEntry" parameterType="org.springframework.data.jdbc.mybatis.MyBatisContext">
SELECT name, description
FROM Model
WHERE legoset = #{id}
</select>

View File

@@ -1,11 +1,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">
<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>
</mapper>
</mapper>

View File

@@ -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");

View File

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