[JAVA] Compatibility of Spring JDBC and MyBatis with Spring Data JDBC (provisional)

[JSUG Study Group 2019 Part 1 Spring Data JDBC Official Release Commemoration! Listen to Quick Spring Data JDBC at Data Access Special and support Mybatis I decided to verify it by saying that I am doing it.

In the investigation, the goal is to leave the standard such as findById to be automatically generated, and to manage the complicated parts separately with a file outside MyBatis.

Use custom operations

  1. Create a mapper At this time, the namespace may be arbitrary. The reason is that this method uses the default "Spring JDBC" implementation for DataAccessStrategy.

    <mapper namespace="com.example.mapper">
    <select id="findByName" resultType="Employee">
    
  2. Create an interface for custom operation as referenced

    public interface EmployeeRepositoryExtension {
        public List<Employee> findByLastName(String lastName);
    }
    
  3. Create an implementation class for custom operations as referenced There is no problem if the argument "statement" specified here matches the definition of the mapper.

    public class EmployeeRepositoryExtensionImpl implements EmployeeRepositoryExtension {
    
        private final SqlSession sqlSession;
    
        public EmployeeRepositoryExtensionImpl(SqlSession sqlSession) {
            this.sqlSession = sqlSession;
        }
    
    	@Override
    	public List<Employee> findByLastName(String lastName) {
    		return this.sqlSession.selectList("com.example.mapper.findByName", lastName);
    	}
    }
    
  4. Inherit to other repository interfaces as referenced

    public interface EmployeeRepository extends CrudRepository<Employee, Long>, EmployeeRepositoryExtension {
    	@Query("SELECT * FROM employee WHERE first_name = :firstName")
    	public List<Employee> findByFirstName(@Param("firstName") String firstName);
    }
    

Play with DataAccessStrategy

As described in [Reference "Combined implementation"](https://qiita.com/kazuki43zoo/items/bd63d28dc2348aa21719#Combined implementation), it seems that DataAccessStrategy can be defined nicely. (Unverified)

[MyBatisDataAccessStrategy](https://github.com/spring-projects/spring-data-jdbc/blob/master/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy. There is an atmosphere that seems to be possible by using MyBatisDataAccessStrategy # createCombinedAccessStrategy (...) as if looking at java).

Reference material

Source

https://github.com/tac-yacht/Sample-SpringDataJDBC-mix-MyBatis

Impressions

If you look at Query Creation, you can see the method name. As a basis for generation, there is already an atmosphere that seems to automatically generate queries, so if you can use it, basically it will be automatically generated, and if it is too complicated to generate, I want to scratch the query with MyBatis. (Cannot be used as of 1.0.4) However, I'm happy personally because I don't have to write findById etc. at this point.

Appendix: Execution result log

When Mybatis


2019-02-02 12:04:38.176 DEBUG 3108 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/findByLastName?name=doe", parameters={masked}
2019-02-02 12:04:38.646 DEBUG 3108 --- [nio-8080-exec-1] com.example.mapper.findByName            : ==>  Preparing: SELECT * FROM employee WHERE last_name = ? 
2019-02-02 12:04:38.670 DEBUG 3108 --- [nio-8080-exec-1] com.example.mapper.findByName            : ==> Parameters: doe(String)
2019-02-02 12:04:38.704 DEBUG 3108 --- [nio-8080-exec-1] com.example.mapper.findByName            : <==      Total: 1

@When custom querying with Query


2019-02-02 12:06:04.184 DEBUG 3108 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : GET "/findByFirstName?name=jone", parameters={masked}
2019-02-02 12:06:04.185 DEBUG 3108 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public java.lang.Object com.example.Controller.findByFirstName(java.lang.String)
2019-02-02 12:06:04.195 DEBUG 3108 --- [nio-8080-exec-3] o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL query
2019-02-02 12:06:04.196 DEBUG 3108 --- [nio-8080-exec-3] o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL statement [SELECT * FROM employee WHERE first_name = ?]

When generating a built-in query


2019-02-02 12:06:07.882 DEBUG 3108 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : GET "/findById?id=1", parameters={masked}
2019-02-02 12:06:07.882 DEBUG 3108 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public java.lang.Object com.example.Controller.findById(java.lang.Long)
2019-02-02 12:06:07.896 DEBUG 3108 --- [nio-8080-exec-4] o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL query
2019-02-02 12:06:07.896 DEBUG 3108 --- [nio-8080-exec-4] o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL statement [SELECT employee.id AS id, employee.first_name AS first_name, employee.last_name AS last_name FROM employee WHERE employee.id = ?]

Recommended Posts

Compatibility of Spring JDBC and MyBatis with Spring Data JDBC (provisional)
Spring with Kotorin --2 RestController and Data Class
Implementation method for multi-data source with Spring boot (Mybatis and Spring Data JPA)
Until data acquisition with Spring Boot + MyBatis + PostgreSQL
Periodically update DB with Spring Batch and MyBatis
Spring Data JDBC Preview
Until the use of Spring Data and JPA Part 2
Until the use of Spring Data and JPA Part 1
Use Spring JDBC with Spring Boot
Window aggregation of sensor data with Apache Flink and Java 8
How to use Spring Data JDBC
Use JDBC with Java and Scala.
Note that Insert could not be done with Spring Data JDBC
Data linkage with Spark and Cassandra
See the behavior of entity update with Spring Boot + Spring Data JPA
[With sample code] Basics of Spring JDBC to learn with Blog app
Creating REST APIs with Spring JPA Data with REST and Lombok incredibly easy.
Determine the device type of smartphones, tablets, and PCs with Spring Mobile
I want to display images with REST Controller of Java and Spring!
OR search with Spring Data Jpa Specification
After 3 months of Java and Spring training
HTTPS with Spring Boot and Let's Encrypt
I tried Spring Data JDBC 1.0.0.BUILD-SNAPSHOT (-> 1.0.0.RELEASE)
Acquisition of JSON data and rotation of values
JDBC promises and examples of how to write
Just input and output images with Spring MVC
[Ruby] Arguments with keywords and default values of arguments
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
Various correspondence table of Spring Framework and Spring Boot
Spring Data JPA Entity cross-reference and its notes
Collective handling of Spring validation errors with @ControllerAdvice
[Beginner] Upload images and files with Spring [Self-satisfaction]
Create CRUD apps with Spring Boot 2 + Thymeleaf + MyBatis
Get Body part of HttpResponse with Filter of Spring
[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
Sort by Spring Data JPA (with compound key sort)
You can eliminate @Param with Kotlin 1.1 and MyBatis 3.4.1+! !!
Creating a common repository with Spring Data JPA
I need validation of Spring Data for Pageable ~
Create dummy data of portfolio with Faker [Note]
Check the behavior of getOne, findById, and query methods in Spring Boot + Spring Data JPA
Form and process file and String data at the same time with Spring Boot + Java