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>
userInfoMethodDao.findAll(UserExample.builder()
.userCodeIn(Arrays.asList("u001","u002"))
.userNameLike("ch%")
.createTimeDesc(true).build());
public interface UserInfoRepository extends CrudExtendRepository<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>
see spring-data-jdbc-demo
configuration
@Configuration
public class MybatisQuerySupportConfig {
@Bean
public NamedParameterJdbcOperations namedParameterJdbcOperations(DataSource dataSource) {
return new MybatisJdbcTemplate(dataSource) {@Override protected Dialect dialect() {return new MySQLDialect();}};
}
}
use
@Query("user.queryByUserCode")
List<UserDTO> queryByUserCode(@Param("userCode") String userCode);
but SpEL support became available with Spring Data JDBC 3.0 RC1
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。