I am currently using myBatis for my project, it is good for complex SQL, but I found that I need to create many redundant SQL for basic CRUD. So I come across spring-data-jdbc
(here), which is a very nice library (similar to spring-data-jpa
but without JPA persistence) to help to generate CRUD SQL statement via method name.
The guide on their official website on how to integrate with myBatis is very vague, I couldn't find any other source which showing how to do that. Basically I am looking for a way to do like below:
@Repository
@Mapper
public interface PersonRepository extends CrudRepository<Person, Long> {
//via spring-data-jdbc
List<Person> findAll();
//via spring-data-jdbc
List<Person> findByFirstName(String name);
//via myBatis, the actual query is in PersonMapper.xml
List<Person> selectAllRow();
}
As you can see, when I call findAll
and findByFirstName
, they will be handled by spring-data-jdbc
. When I call selectAllRow
, it will look for corresponding myBatis mapper file for actual SQL. In this way I can combine two mechanism to handle simple CRUD query and complex query (via myBatis) together.
But above doesn't work, so currently I have to split them into two interface, one for PersonRepository
, and another one for PersonMapper
, which is not a very nice design.
Anyone has done similar before?