[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.
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">
Create an interface for custom operation as referenced
public interface EmployeeRepositoryExtension {
public List<Employee> findByLastName(String lastName);
}
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);
}
}
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);
}
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).
https://github.com/tac-yacht/Sample-SpringDataJDBC-mix-MyBatis
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.
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