编程知识 cdmana.com

MySQL + mybatis paging query - database series learning notes

One 、 First, make a query and display all

dao

public interface ProductDAO {
    
	public List<Product> list();
}

mapper

<mapper namespace="hust.mm.dao.ProductDAO">
	<select id="list" resultType="Product">
		select * from product
	</select>
</mapper>

Controller

@RequestMapping("/list.do")
public ModelAndView productlist(){
    
	ModelAndView mav = new ModelAndView();
		
	List<Product> products = productDao.list();
		
	mav.addObject("products", products);
	mav.setViewName("productList");
		
	return mav;
}

jsp

<table align="center">
		<th>
			<td>id</td>
			<td>name</td>
			<td>price</td>
		</th>
		<c:forEach items="${products }" var="p" varStatus="st">
			<tr>
				<td>${p.id }</td>
				<td>${p.name }</td>
				<td>${p.price }</td>
			</tr>
		</c:forEach>
</table>

All the data in a table are briefly given above


Two 、 Pagination display

modify dao: Enter the starting query location and the number of queries to be queried each time

public interface ProductDAO {
    
	public List<Product> list();
	public List<Product> list(@Param("start") int start, @Param("count") int count);
}

modify mapper: Limit the number of queries per query , Every time from the database start Start query with data , Inquire about count strip .

<mapper namespace="hust.mm.dao.ProductDAO">
    <select id="list" resultType="Product">
		select * from product
		<if test="start!=null and count!=null">
			limit #{
    start},#{
    count}
		</if>
	</select>
</mapper>

modify Controller

@RequestMapping("/list.do")
public ModelAndView productlist(int start){
    
	ModelAndView mav = new ModelAndView();
		
	List<Product> products = productDao.list(start,3);
		
	mav.addObject("products", products);
	mav.addObject("start", start);
	mav.setViewName("productList");
		
	return mav;
}

modify jsp: Three pieces of data per page , Support page turning

<table align="center">
		<th>
			<td>id</td>
			<td>name</td>
			<td>price</td>
		</th>
		<c:forEach items="${products }" var="p" varStatus="st">
			<tr>
				<td>${p.id }</td>
				<td>${p.name }</td>
				<td>${p.price }</td>
			</tr>
		</c:forEach>
        
        <tr>
			<td><a href="list.do?start=${start-3 }"> The previous page </a></td>
			<td><a href="list.do?start=${start+3 }"> The next page </a></td>
		</tr>
</table>

版权声明
本文为[Old fellow iron has dried up the code.]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002145845272k.html

Scroll to Top