当前位置:网站首页>General paging (1)

General paging (1)

2022-06-23 08:58:00 An Li Jiu Ge

Catalog

Preface

One 、PageBean

Two 、 Reflect generic background queries

3、 ... and 、 General paging background query

Four 、junit4

summary


Preface

Last time we shared custom tags ——jsp Label enhancements , The content shared today is general paging .


One 、PageBean

package com.zhw.util;

public class PageBean {
	private int page = 1;//  Page number 

	private int rows = 10;//  Page size 

	private int total = 0;//  Total number of records 

	private boolean pagination = true;//  Pagination or not 

	public PageBean() {
		super();
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public void setTotal(String total) {
		this.total = Integer.parseInt(total);
	}

	public boolean isPagination() {
		return pagination;
	}

	public void setPagination(boolean pagination) {
		this.pagination = pagination;
	}

	/**
	 *  Get the subscript of the starting record 
	 * 
	 * @return
	 */
	public int getStartIndex() {
		return (this.page - 1) * this.rows;
	}

	@Override
	public String toString() {
		return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
	}
}

Two 、 Reflect generic background queries  

Native :
public List<Book> list(Book book,PageBean pageBean) throws Exception{
		List<Book> list  = new ArrayList<Book>();
		Connection con = DBAccess.getConnection();
		String sql = "select * from t_mvc_book where 1=1 ";
		String bname = book.getBname();
		if(StringUtils.isNotBlank(bname)) {
			sql += "and bname like '%"+bname+"%' ";
		}
		PreparedStatement pst = con.prepareStatement(sql);
		ResultSet rs = pst.executeQuery();
		while(rs.next()) {
			list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
		}
		return list;
	}
Reflection :
public List<T> list(String sql,Class<T> clz) throws Exception{
		List<T> list  = new ArrayList<T>();
		Connection con = DBAccess.getConnection();
        String sql = "select * from t_mvc_book where 1=1 ";
		String bname = book.getBname();
		if(StringUtils.isNotBlank(bname)) {
			sql += "and bname like '%"+bname+"%' ";
		}
		PreparedStatement pst = con.prepareStatement(sql);
		ResultSet rs = pst.executeQuery();
		while(rs.next()) {
//			list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
			T t = clz.newInstance();
			Field[] fields = clz.getDeclaredFields();
			for (Field f : fields) {
				f.setAccessible(true);
				f.set(t, rs.getObject(f.getName()));
			}
			list.add(t);
		}
		return list;
	}

3、 ... and 、 General paging background query

public List<T> executeQuery(String sql,PageBean pageBean,Class<T> clz) throws Exception{
		List<T> list  = new ArrayList<T>();
		Connection con = DBAccess.getConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		
		String countSQL = getCountSQL(sql);
		String pageSQL = getPageSQL(sql,pageBean);
		
		if(pageBean != null && pageBean.isPagination()) {
			pst = con.prepareStatement(countSQL);
			rs = pst.executeQuery();
			
			if(rs.next()) {
				pageBean.setTotal(String.valueOf(rs.getObject(1)));
			}
			
			pst = con.prepareStatement(pageSQL);
			rs = pst.executeQuery();
		}else {
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
		}
		
		
		while(rs.next()) {
//			list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
			T t = clz.newInstance();
			Field[] fields = clz.getDeclaredFields();
			for (Field f : fields) {
				f.setAccessible(true);
				f.set(t, rs.getObject(f.getName()));
			}
			list.add(t);
		}
		return list;
	}

	private String getPageSQL(String sql, PageBean pageBean) {
		return sql + " limit "+pageBean.getStartIndex()+","+pageBean.getRows();
	}

	private String getCountSQL(String sql) {
		return "select count(1) FROM ("+sql+") t";
	}

Four 、junit4

   We often test in main Method to print . There are more and more testing methods ,main Methods are becoming more and more complicated .

   You can use Junit.

Right click the test class ,Ctrl+N,  Search for junit, Select first .

  choice junit 4, And check the setup and teardown 

next, Choose Test method of the current class

finish. The test class is finished .

 

package com.zhw.dao;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.zhw.entity.Book;

public class BookDaoTest {
	@Before
	public void setUp() throws Exception {
		System.out.println(" The method under test is called before execution ");
	}

	@After
	public void tearDown() throws Exception {
		System.out.println(" Call after the tested method is executed ");
	}

	@Test
	public void testList() {
		List<Book> list;
		try {
			list = new BookDao().list(new Book(), null);
			for (Book book : list) {
				System.out.println(book);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}







Running results :

 

If we want to test other methods

  Write a new one :

 

package com.zhw.dao;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.zhw.entity.Book;

public class BookDaoTest {
	@Before
	public void setUp() throws Exception {
		System.out.println(" The method under test is called before execution ");
	}

	@After
	public void tearDown() throws Exception {
		System.out.println(" Call after the tested method is executed ");
	}

	@Test
	public void testList() {
		List<Book> list;
		try {
			list = new BookDao().list(new Book(), null);
			for (Book book : list) {
				System.out.println(book);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void testList2() {
		System.out.println(" Test code 2");
	}
}







give the result as follows :


summary

The content shared this time is the General tab page , Easy to use , If there are multiple types that need to be paged, there is no need to write duplicate code , Just call the written generic paging code , Shorten development time , Reduce the amount of code .

I hope it can help you , General page for next issue preview (2).

I'm nine songs , A programmer who likes programming .

If there is any mistake, please correct it , Thank you very much! .

原网站

版权声明
本文为[An Li Jiu Ge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230839533921.html