mybatis 使用注解写动态 sql 语句,需要在 sql 字符串首尾添加一对 <script></script> 标签,代码如下
package com.che.pri.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.RowBounds;
import com.che.pri.entity.Book;
public interface DataPageMapper {
@Select("<script>" + "select * from t_book where 1 = 1" +
"<if test = 'name != null'>" +
"and INSTR(name, #{name}) > 0" +
"</if>" +
"<if test = 'author != null'>" +
"and INSTR(author, #{author}) > 0" +
"</if>" +
"order by date" +
"</script>")
List<Book> getPageList(RowBounds rowBounds, Map<String, Object> map);
@Select("<script>select count(*) from t_book where 1=1" +
"<if test = 'name != null'>" +
"and INSTR(name, #{name}) > 0" +
"</if>" +
"<if test = 'author != null'>" +
"and INSTR(author, #{author}) > 0" +
"</if>" +
"</script>")
int getCountPageBookList(Map<String, Object> map);
}
MyBatis注解动态SQL
本文介绍在MyBatis中使用注解方式编写动态SQL语句的方法,通过示例展示了如何利用<script>标签及<if>条件语句实现参数化的SQL查询,适用于分页和条件筛选场景。
927

被折叠的 条评论
为什么被折叠?



