博客暂时存在 bug,即有等号出现不能发布文章。暂时使用eq代替。
在 Mybatis 中,很多动态 SQL 都是由 xml 配置实现的。而随着 SpringBoot 的逐渐发展,越来越多的配置由配置文件转成注解的形式。其中动态 SQL 成为了注解方式的一个棘手问题。
Mybatis 的注解中,@Select 标签中使用太过长的语句看起来很不美观,但是为了统一格式不得不在其中堆砌大量代码。
@Select("SELECT title" +
"from job where " +
"job_id eq #{job_id}")
如果想要在 mapper 中进行非必要关键字的查询,就不得不使用动态 SQL,与 xml 配置不同的是,@Select注解中 SQL 语句必须以 <script>标签包裹。
@Select("<script>SELECT job_id " +
"from job" +
"<where>" +
"<if test 'company_id!eq null'>" +
"and company_id eq#{company_id}" +
"</if>" +
"<if test 'title!eq null'>" +
"and title like CONCAT('%',#{title},'%')" +
"</if>" +
"</where>" +
"ORDER BY create_time desc,rank desc" +
"</script>")
Zhou Yang
转载请注明出处
最后更新于: 2019/02/02 15:49