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);
}