(四)抽象封装实现多条件组合查询赋值及分页查询

首先我们要对page进行抽象封装

/**
 * 分页查询封装
 *
 */

public class Page {
	private int pageNumber;//当前页数
	
	private int pageSize;//每页显示数量
	
	private int total;//总记录数
	
	private int totalPage;//总页数
	
	private int offset;//对应数据库里的游标
	
	private List content = new ArrayList();//结果集
	
	private List searchProporties = new ArrayList();
	
	public Page(int pageNumber,int pageSize){
		this.pageNumber = pageNumber;
		this.pageSize = pageSize;
		this.offset = (this.pageNumber - 1) * this.pageSize;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public List getContent() {
		return content;
	}

	public void setContent(List content) {
		this.content = content;
	}

	public List getSearchProporties() {
		return searchProporties;
	}

	public void setSearchProporties(List searchProporties) {
		this.searchProporties = searchProporties;
	}

	public int getTotal() {
		return total;
	}

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

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
}

然后拼接多条件查询sql语句:

/**
	 * 构造多条件查询的条件sql语句
	 * @param page
	 * @return
	 */
	private String buildSearchSql(Page page) {
		// TODO Auto-generated method stub
		String searchSql = "";
		List searchProporties = page.getSearchProporties();
		for(SearchProperty searchProperty:searchProporties){
			switch (searchProperty.getOperator()) {
				case GT:{//大于
					searchSql += " and " + searchProperty.getKey() + " > ?";
					break;
				}
				case GTE:{//大于等于
					searchSql += " and " + searchProperty.getKey() + " >= ?";
					break;
				}
				case EQ:{//等于
					searchSql += " and " + searchProperty.getKey() + " = ?";
					break;
				}
				case LT:{//小于
					searchSql += " and " + searchProperty.getKey() + " < ?";
					break;
				}
				case LTE:{//小于等于
					searchSql += " and " + searchProperty.getKey() + " <= ?";
					break;
				}
				case NEQ:{//不等于
					searchSql += " and " + searchProperty.getKey() + " <> ?";
					break;
				}
				case LIKE:{//模糊匹配
					searchSql += " and " + searchProperty.getKey() + " like ?";
					break;
				}
				case IN:{//在范围内
					searchSql += " and " + searchProperty.getKey() + " in(" + searchProperty.getValue() + ")";
					break;
				}
			}
		}
		if(!"".equals(searchSql)){
			searchSql = searchSql.replaceFirst("and", "where");
		}
		System.out.println(searchSql);
		return searchSql;
	}

查询总的页数:


	/**
	 * 返回总的记录数
	 * @param sql
	 * @return
	 */
	private int getTotal(String sql,Page page){
		PreparedStatement prepareStatement;
		try {
			prepareStatement = dbUtil.getConnection().prepareStatement(sql);
			prepareStatement = setSearchPrepareStatement(prepareStatement,page);
			ResultSet executeQuery = prepareStatement.executeQuery();
			if(executeQuery.next()){
				int total = executeQuery.getInt("total");
				dbUtil.releaseConnection();
				return total;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

 

最后进行查询操作:


	/**
	 * 抽象分装多条件分页查询列表操作
	 * @param page
	 * @return
	 */
	public Page findList(Page page){
		String sql = buildSql(CURD_SELECT);
		sql += buildSearchSql(page);
		sql += " limit " + page.getOffset() + "," + page.getPageSize();
		try {
			PreparedStatement prepareStatement = dbUtil.getConnection().prepareStatement(sql);
			prepareStatement = setSearchPrepareStatement(prepareStatement,page);
			ResultSet executeQuery = prepareStatement.executeQuery();
			while(executeQuery.next()){
				T newInstance = (T) t.newInstance();
				newInstance = setParams(newInstance,executeQuery);
				page.getContent().add(newInstance);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		page.setTotal(getTotal(buildSql(CURD_COUNT) + buildSearchSql(page), page));
		int totalPage = 0;
		if(page.getTotal() % page.getPageSize() == 0){
			totalPage = page.getTotal() / page.getPageSize();
		}else{
			totalPage = page.getTotal() / page.getPageSize() + 1;
		}
		page.setTotalPage(totalPage);
		dbUtil.releaseConnection();
		return page;
	}

 

你可能感兴趣的:(封装一个自己的数据库框架)