要进行模糊分页查询书籍表books如下:
首先,先得要一个用于放分页相关信息的实体类
1.在codewen.bookcool.entity下创建一个Page类(注意这个Page类持久层没啥作用,主要是业务逻辑层会将分页的总数据数totaldata,总页数totalpage,页面大小pagesize,当前页面currentpage,页码currentpageNum,接收分页查询的一页结果集和pageLists封装到Page类中然后返给上层的servlet,servlet再做出响应将Page里的数据传给jsp,jsp将数据取出并显示)
import java.util.List;
/**
* 用于分页的实体类
* @author codewen
*
*/
public class Page<T> {
private int totaldata;//总数据数
private int pagesize;//页面大小
private int totalpage;//总页数
private int currentpage;//当前页面
private int currentpageNum;//当前页码
private List<T> pageLists;//接收分页查询的一页结果集
public Page() {
}
public Page(int totaldata, int pagesize, int totalpage, int currentpage,
int currentpageNum, List<T> pageLists) {
this.totaldata = totaldata;
this.pagesize = pagesize;
this.totalpage = totalpage;
this.currentpage = currentpage;
this.currentpageNum = currentpageNum;
this.pageLists = pageLists;
}
public List<T> getPageLists() {
return pageLists;
}
public void setPageLists(List<T> pageLists) {
this.pageLists = pageLists;
}
public int getTotaldata() {
return totaldata;
}
public void setTotaldata(int totaldata) {
this.totaldata = totaldata;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getTotalpage() {
return totalpage;
}
public void setTotalpage(int totalpage) {
this.totalpage = totalpage;
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public int getCurrentpageNum() {
return currentpageNum;
}
public void setCurrentpageNum(int currentpageNum) {
this.currentpageNum = currentpageNum;
}
@Override
public String toString() {
return "Page [totaldata=" + totaldata + ", pagesize=" + pagesize
+ ", totalpage=" + totalpage + ", currentpage=" + currentpage
+ ", currentpageNum=" + currentpageNum + ", pageLists="
+ pageLists + "]";
}
}
2.在codewen.bookcool.mapper接口包中创建一个接口BookMapper
并在BookMapper接口中加queryTotalBook和queryBookByPage方法
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import codewen.bookcool.entity.Book;
public interface BookMapper {
//查询书籍数
public int queryTotalBook(@Param("parameterMap")Map<String, String[]> parameterMap);
//查询一页书籍
public List<Book> queryBookByPage(@Param("currentpage")int currentpage
,@Param("pagesize")int pagesize
,@Param("parameterMap")Map<String, String[]> parameterMap);
}
3.在conf文件夹下的codewen,bookcool.mapper包下新建BookMapper.xml文件
并在BookMapper.xml加上刚刚那两个方法的SQL语句
具体SQL语句是:SELECT * FROM books WHERE CONCAT(bname,bauthor,bcategory) LIKE '%?%' LIMIT ?,?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="codewen.bookcool.mapper.BookMapper">
<!-- 可重用的查询所有书的sql -->
<sql id="queryAllBook">
select * from books
</sql>
<!-- 可重用的通过书籍参数查询sql -->
<sql id="queryBookCondition">
<!-- 先判断Map集合是否为空然后再判断value是否为空 -->
<if test="parameterMap !=null ">
<if test="parameterMap.fparam !=null and parameterMap.fparam !='' ">
<bind name="binidfparam" value=" '%'+parameterMap.fparam[0]+'%' "/>
<where>
concat(bname,bauthor,bcategory) like #{binidfparam}
</where>
</if>
</if>
</sql>
<!-- //查询书籍数
public int queryTotalBook(@Param("parameterMap")Map<String, String[]> parameterMap); -->
<select id="queryTotalBook" resultType="int">
select count(1) from books
<include refid="queryBookCondition"/>
</select>
<!-- //查询一页书籍
public List<Book> queryBookByPage(@Param("currentpage")int currentpage
,@Param("pagesize")int pagesize
,@Param("parameterMap")Map<String, String[]> parameterMap); -->
<select id="queryBookByPage" resultType="codewen.bookcool.entity.Book">
<include refid="queryAllBook"/>
<include refid="queryBookCondition"/>
limit #{currentpage},#{pagesize}
</select>
</mapper>
4.到test包的新建BookTest测试类并在其中测试即可(只是测试语句啥的有没有写错)
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import codewen.bookcool.entity.Book;
import codewen.bookcool.mapper.BookMapper;
import codewen.bookcool.until.MyBatisUtil;
public class BookTest {
@Test
public void test5() {
SqlSession session = MyBatisUtil.getSession();
BookMapper mapper = session.getMapper(BookMapper.class);
Map<String, String[]> parameterMap = new HashMap<>();
String[] fparam = {"天"};
parameterMap.put("fparam", fparam);
List<Book> books = mapper.queryBookByPage(0, 10, parameterMap);
MyBatisUtil.closeSession();
for (Book book : books) {
System.out.println(book);
}
}
}
结果如下: