spring data jdbc extend mybatis dynamic sql
spring data jdbc extend mybatis dynamic sql
It aims at being conceptually easy. In order to achieve this it does NOT offer caching, lazy loading,QueryDSL, write behind or many other features of JPA. This makes a simple, limited, opinionated ORM.
use mybatis dynamic SQL(not dependency mybatis),it is good for complex SQL
SQL is written in Markdown
-- findUserByIds
SELECT [@id column] FROM user_base
<where>
[@@and id in #{idList:in} and user_name like #{userName:like}]
[@and user_name like userName%]
[@and id in idList]
<if test="null!=createTime"> and create_time < #{createTime} </if>
</where>
public interface UserInfoRepository extends CrudJdbcRepository<UserInfoDO, Long> {
List<UserInfoDO> findByUserCodes(@Param("userCodes") List<String> userCodes);
List<UserInfoDO> findUserBySearchParam(@Param("param") SearchParam searchParam);
}
define sql in markdown UserInfoRepository.md
need @EnableJdbcRepositories
@SpringBootApplication
@EnableJdbcRepositories
public class JdbcMybatisTestApplication {
public static void main(String[] args) {
SpringApplication.run(JdbcMybatisTestApplication.class, args);
}
}
maven
<!-- spring boot 2.x -->
<dependency>
<groupId>com.vonchange.common</groupId>
<artifactId>spring-data-jdbc-mybatis</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>