We have summarized the 1.3 changes released on April 10, 2017.
In addition, it should be noted
Please see.
Note: ** 2017/8/21: Addendum ** Added changes in version 1.3.1. ** 2019/5/7: Addendum ** Reflected the changes in versions 1.3.2, 1.3.3 and 1.3.4.
To use version 1.3, the following versions are required.
Starting with version 1.3, the @MybatisTest
annotation, which supports the Bean definition required when testing the functionality of MyBatis, has been added, and that annotation depends on the API supported by Spring Boot 1.5, so Spring Boot 1.5+ Is required. If you want to use mybatis-spring-boot-starter with Spring Boot 1.4 series, please use version 1.2 series.
In version 1.3, the following library versions have been updated.
Library name | 1.2.0 version | 1.3.0 version | 1.3.1 version | 1.3.Version 2 | 1.3.Version 3 | 1.3.Version of 4 |
---|---|---|---|---|---|---|
MyBatis | 3.4.2 | 3.4.4 | 3.4.5 | 3.4.6 | 3.4.6 | 3.4.6 |
MyBatis Spring | 1.3.1 | 1.3.1 | 1.3.1 | 1.3.2 | 1.3.2 | 1.3.2 |
Spring Boot | 1.4.3.RELEASE | 1.5.2.RELEASE | 1.5.6.RELEASE | 1.5.10.RELEASE | 1.5.19.RELEASE | 1.5.20.RELEASE |
Note: Compared to version 1.2.1 released at the same time as version 1.3.0, only the Spring Boot version has been updated (1.4.5.RELEASE-> 1.5.2.RELEASE).
ConfigurationCustomizer
interfaceCallback interface (ʻorg.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer) for customizing the Bean that holds MyBatis configuration information (Bean of ʻorg.apache.ibatis.session.Configuration
) with Java code ) Has been added. If you define a bean that implements this interface as shown below, the configure
method of ConfigurationCustomizer
will be called back, and you can use Java code to complete the configuration of MyBayis. Can be customized to.
Bean definition example of ConfigurationCustomizer using lambda expression
@Configuration
public class MyBatisConfiguration {
@Bean
ConfigurationCustomizer mybatisConfigurationCustomizer() {
return (configuration) -> {
// aplication.Implemented customization code that cannot be specified / expressed by properties
configuration.getTypeHandlerRegistry().register(RoundingMode.class, EnumOrdinalTypeHandler.class);
};
}
}
Note:
The
ConfigurationCustomizer
interface is also backported to version 1.2.1, which was released at the same time as version 1.3.0.
@MybatisTest
(mybatis-spring-boot-starter-test)Starting with version 1.3, the @MybatisTest
annotation has been added to support the bean definition required when testing the functionality of MyBatis. This is [@DataJpaTest
](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-testing-spring- provided by Spring Boot. boot-applications-testing-autoconfigured-jpa-test) and [@JdbcTest
](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features -testing-spring-boot-applications-testing-autoconfigured-jdbc-test) will be the MyBatis version (of course, the usage is the same as the annotation provided by Spring Boot !!).
Along with this support, mybatis-spring-boot-starter-test
has been newly established to solve the libraries required when testing MyBatis, so if you want to test the functions provided by MyBatis, , Let's add the following definition to pom.xml
! !!
pom.xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>1.3.4</version>
<scope>test</scope>
</dependency>
For example, if you want to test the following Mapper interface,
src/main/java/com/example/mapper/TodoMapper.java
package com.example.mapper;
import com.example.domain.Todo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface TodoMapper {
@Insert("INSERT INTO todo (title, details, finished) VALUES (#{title}, #{details}, #{finished})")
@Options(useGeneratedKeys = true)
void insert(Todo todo);
@Select("SELECT id, title, details, finished FROM todo WHERE id = #{id}")
Todo select(int id);
}
It is OK if you create the following test case class using @MybatisTest
.
src/main/java/com/example/mapper/TodoMapperTests.java
package com.example.mapper;
import com.example.domain.Todo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@MybatisTest // (1)
public class TodoMapperTests {
@Autowired
private TodoMapper todoMapper; // (2)
@Autowired
private NamedParameterJdbcOperations jdbcOperations; // (3)
@Test
public void insert() {
// setup
// none
// perform test and assertions
{
// (4)
Todo newTodo = new Todo();
newTodo.setTitle("drinking party");
newTodo.setDetails("Ginza 19:00");
todoMapper.insert(newTodo);
// (5)
Todo actualTodo =
jdbcOperations.queryForObject("SELECT * FROM todo WHERE id = :id",
new MapSqlParameterSource("id", newTodo.getId()),
new BeanPropertyRowMapper<>(Todo.class));
assertThat(actualTodo.getId()).isEqualTo(newTodo.getId());
assertThat(actualTodo.getTitle()).isEqualTo("drinking party");
assertThat(actualTodo.getDetails()).isEqualTo("Ginza 19:00");
assertThat(actualTodo.isFinished()).isEqualTo(false);
}
}
@Test
public void select() {
// (6)
// setup
Todo newTodo = new Todo();
newTodo.setTitle("drinking party");
newTodo.setDetails("Ginza 19:00");
GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
jdbcOperations.update(
"INSERT INTO todo (title, details, finished) VALUES(:title, :details, :finished)",
new BeanPropertySqlParameterSource(newTodo), keyHolder);
// perform test and assertions
{
// (7)
Todo actualTodo = todoMapper.select(keyHolder.getKey().intValue());
assertThat(actualTodo.getId()).isEqualTo(keyHolder.getKey().intValue());
assertThat(actualTodo.getTitle()).isEqualTo("drinking party");
assertThat(actualTodo.getDetails()).isEqualTo("Ginza 19:00");
assertThat(actualTodo.isFinished()).isEqualTo(false);
}
}
}
Item number | Description |
---|---|
(1) | In class@MybatisTest Is given. By adding this annotation, only the AutoConfigure class required to run MyBatis will be enabled. The default behavior is to disable component scanning, which prevents untitled beans from being registered in the DI container during testing. |
(2) | Inject the Mapper to be tested.@MybatisTest If you give, AutoConfigure provided by MyBatis will generate a Bean of Mapper. |
(3) | To validate registration data and register test dataNamedParameterJdbcTemplate Inject the Bean of.@MybatisTest If you giveJdbcTemplate WhenNamedParameterJdbcTemplate Bean is registered in the DI container. |
(4) | To be testedinsert Call the method. |
(5) | insert Validate the method call result. here,NamedParameterJdbcOperations The registered data is acquired via the method of and the validity of the registered data is verified. |
(6) | select Register test data to test the method. here,NamedParameterJdbcOperations Test data is registered via the method of. In addition, provided by Spring Framework@Sql You can also register test data using annotations. |
(7) | To be testedselect Call the method. |
(8) | select Validate the method call result. |
If you execute the test in this state, the Bean definition defined in the Spring Boot application class (class with @SpringBootApplication
) existing in the upper package is also read, so it is necessary for MyBatis test. Missing beans may be registered in the DI container.
If you do not want to register beans that are not needed for MyBatis testing in the DI container, create an empty Spring Boot application class like the one below in the same package as the test case class. By doing this, you can disable the Spring Boot application class in the upper package.
src/test/java/com/example/mapper/MapperTestApplication.java
package com.example.mapper;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MapperTestApplication {
}
For more information, see the Official Reference (http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-test-autoconfigure/) and the [@DataJpaTest
](@DataJpaTest
] provided by Spring Boot. http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test) and [ @JdbcTest
](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-jdbc- See the test) reference.
Note: The
@MybatisTest
annotation (andmybatis-spring-boot-starter-test
) is also backported to version 1.2.1, which was released at the same time as version 1.3.
Until 1.3.0, input completion for some properties (mybatis.configuration.default-scripting-language
) did not work, but" [Adding additional meta-data] provided by Spring Boot (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#configuration-metadata-additional-metadata) ", input completion will be effective from 1.3.1. Has been improved.
typeAliasesSuperType
in configuration properties[Changes in version 2.0](https://qiita.com/kazuki43zoo/items/38a24e051359e39c012c#%E3%82%B3%E3%83%B3%E3%83%95%E3%82%AE%E3%83 % A5% E3% 83% AC% E3% 83% BC% E3% 82% B7% E3% 83% A7% E3% 83% B3% E3% 83% 97% E3% 83% AD% E3% 83% 91 % E3% 83% 86% E3% 82% A3% E3% 81% A7typealiasessupertype% E3% 81% AE% E6% 8C% 87% E5% AE% 9A% E3% 82% 92% E3% 82% B5% E3 See% 83% 9D% E3% 83% BC% E3% 83% 88). (Supported from version 1.3.3)
Recommended Posts