/** * * @param site * @param currentPageNo 起始位置 * @param pageSize 页面容量 * @return */ public List<Website> selectWebsite(@Param("site") Website site, @Param("from") Integer currentPageNo, @Param("pageSize") Integer pageSize);相比原来的 selectWebsite 方法,增加了两个参数,起始位置(from)和页面容量(pageSize),用于实现分页查询。
<select id="selectWebsite" resultType="net.biancheng.po.Website"> SELECT id,name,url,age,country FROM website <trim prefix="where" prefixOverrides="and"> <if test="site.name != null and site.name !=''"> AND name LIKE CONCAT ('%',#{site.name},'%') </if> <if test="site.url!= null and site.url !=''"> AND url LIKE CONCAT ('%',#{site.url},'%') </if> ORDER BY id limit #{from},#{pageSize} </trim> </select>测试类代码如下。
public static void main(String[] args) throws IOException { // 读取配置文件mybatis-config.xml InputStream config = Resources.getResourceAsStream("mybatis-config.xml"); // 根据配置文件构建 SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config); // 通过SqlSessionFactory创建SqlSession SqlSession ss = ssf.openSession(); Website site = new Website(); // site.setname("编程"); site.setUrl("http"); Integer pageSize = 3; Integer currentPageNo = 0; List<Website> siteList = new ArrayList<Website>(); siteList = ss.getMapper(WebsiteMapper.class).selectWebsite(site, currentPageNo, pageSize); for (Website ws : siteList) { System.out.println(ws); } }运行结果如下。
DEBUG [main] - ==> Preparing: SELECT id,name,url,age,country FROM website where url like concat ('%',?,'%') ORDER BY id limit ?,?
DEBUG [main] - ==> Parameters: http(String), 0(Integer), 3(Integer)
DEBUG [main] - <== Total: 3
Website[id=1,name=编程帮,url=https://www.lmcjl.com/,age=10,country=CN]
Website[id=2,name=C语言中文网,url=http://task.lmcjl.com/,age=12,country=CN]
Website[id=3,name=百度,url=https://www.baidu.com/,age=18,country=CN]
本文链接:http://task.lmcjl.com/news/18795.html