您的位置:首页 > 编程语言 > Java开发

简单的多条件分页查询

2017-08-03 18:36 399 查看
/**dao接口*/
package dao;

import java.util.ArrayList;
import java.util.List;

import com.trs.client.TRSConnection;
import com.trs.client.TRSException;
import com.trs.client.TRSResultSet;

import entity.Demo1;
import entity.Demo2;

public interface PageBase {

public long getPages(String dbName,boolean flag,Demo2 demo2);

public List<Demo2> fenye(String dbName,Demo2 demo2,boolean flag,Long page);
}


/**
*dao的实现类*
**/
package daoImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;

import com.trs.client.TRSConnection;
import com.trs.client.TRSException;
import com.trs.client.TRSResultSet;

import dao.PageBase;
import entity.Demo2;

public class PageDaoImpl implements PageBase{
static TRSConnection connection=null;
static TRSResultSet resultSet=new TRSResultSet();
public static void openCon(){
try {
connection = new TRSConnection();
connection.connect("localhost", "8888", "pub", "");
} catch (TRSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void closeCon(){
if(resultSet!=null){
resultSet.close();
}
if(connection!=null){
connection.close();
connection=null;
}
}

public String getWhere(Demo2 demo){
String where="";
System.out.println("pblishname"+demo.getPublishName());
if(!StringUtils.isEmpty(demo.getPublishName())){
where+="版名 ="+demo.getPublishName();
}
if(!StringUtils.isEmpty(demo.getAuthor())){
if(where.length()>2){
where+=" and 作者="+demo.getAuthor();
}
else{
where+="作者="+demo.getAuthor();
}
}
if(!StringUtils.isEmpty(demo.getAuthor())){
if(where.length()>2){
where+=" and 版次="+demo.getPublishId();
}
else{
where+="版次="+demo.getPublishId();
}
}
if(!StringUtils.isEmpty(demo.getTitle())){
if(where.length()>2){
where+=" and 标题  'like %"+demo.getTitle()+"%'";
}
else{
where+="标题  'like %"+demo.getTitle()+"%'";
}
}
S
4000
ystem.out.println("where2;"+where);
return where;
}

public long getPages(String dbName,boolean flag,Demo2 demo2){
openCon();
long count = 0;
try {
System.out.println("dbName:"+dbName+",where:"+getWhere(demo2)+",flag:"+flag);
resultSet=connection.executeSelect(dbName, getWhere(demo2), flag);
count = resultSet.getRecordCount();
} catch (TRSException e) {
e.printStackTrace();
}finally {
closeCon();
}
if(count%20==0){
return count/20;
}
return count/20+1;
}

public List<Demo2> fenye(String dbName,Demo2 demo2,boolean flag,Long page){
openCon();
List<Demo2> demoList=new ArrayList<Demo2>();
try{
resultSet=connection.executeSelect(dbName, getWhere(demo2), flag);
long len=resultSet.getRecordCount();
System.out.println("len:"+len);
connection.executeSelect(dbName, getWhere(demo2), flag);
for(long i=(page-1)*20;i<(page*20)&&i<len;i++){
Demo2 demo=new Demo2();
resultSet.moveTo(0,i);
demo.setAuthor(resultSet.getString("作者"));
demo.setDate(resultSet.getString("日期"));
demo.setPublishId(resultSet.getInt("版次"));
demo.setPublishName(resultSet.getString("版名"));
demo.setTitle(resultSet.getString("标题"));
demoList.add(demo);
}
} catch (TRSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
closeCon();
}
return demoList;
}
public static void main(String[] args) {
openCon();
System.getProperty("java.version"); //判断JDK版本
System.getProperty("sun.arch.data.model"); //判断是32位还是64位
}
}


/**biz,bizImpl略*/
/**查询的实体类*/
package entity;

public class Demo2 {
private int recordId;
private String date;
private int publishId;
private String publishName;
private String title;
private String author;
/**
* @return the recordId
*/
public int getRecordId() {
return recordId;
}
/**
* @param recordId the recordId to set
*/
public void setRecordId(int recordId) {
this.recordId = recordId;
}
/**
* @return the date
*/
public String getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(String date) {
this.date = date;
}
/**
* @return the publishId
*/
public int getPublishId() {
return publishId;
}
/**
* @param publishId the publishId to set
*/
public void setPublishId(int publishId) {
this.publishId = publishId;
}
/**
* @return the publishName
*/
public String getPublishName() {
return publishName;
}
/**
* @param publishName the publishName to set
*/
public void setPublishName(String publishName) {
this.publishName = publishName;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the author
*/
public String getAuthor() {
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author) {
this.author = author;
}

}


/**封装的实体类*/
package entity;

import java.util.List;

public class Page {
private Long page;
private Long nextPage;
private Long backPage;
private Long pageCount;
private List<Demo2> demoList;
/**
* @return the page
*/
public Long getPage() {
return page;
}
/**
* @param page the page to set
*/
public void setPage(Long page) {
this.page = page;
}
/**
* @return the nextPage
*/
public Long getNextPage() {
return nextPage;
}
/**
* @param nextPage the nextPage to set
*/
public void setNextPage(Long nextPage) {
this.nextPage = nextPage;
}
/**
* @return the backPage
*/
public Long getBackPage() {
return backPage;
}
/**
* @param backPage the backPage to set
*/
public void setBackPage(Long backPage) {
this.backPage = backPage;
}
/**
* @return the pageCount
*/
public Long getPageCount() {
return pageCount;
}
/**
* @param pageCount the pageCount to set
*/
public void setPageCount(Long pageCount) {
this.pageCount = pageCount;
}
/**
* @return the demoList
*/
public List<Demo2> getDemoList() {
return demoList;
}
/**
* @param demoList the demoList to set
*/
public void setDemoList(List<Demo2> demoList) {
this.demoList = demoList;
}

}


/**action层*/
package action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import biz.PageBiz;
import entity.Demo2;
import entity.Page;

public class FenyeAction extends ActionSupport{
private PageBiz pageBiz;
private Page pageL=new Page();
private Demo2 demo2=new Demo2();
Long i=(long) 1;
public String fenye() {
if(pageL.getPage()==null){
pageL.setPage(i);
}
pageL.setBackPage(pageL.getPage()-1);
pageL.setNextPage(pageL.getPage()+1);
pageL.setPageCount(pageBiz.getPages("Demo2", false, demo2));
if(pageL.getBackPage()<1){
pageL.setBackPage(i);
}
if(pageL.getNextPage()>pageL.getPageCount()){
pageL.setNextPage(pageL.getPageCount());
}
pageL.setDemoList(pageBiz.fenye("Demo2", demo2, false, pageL.getPage()));
return "fenye";
}
/**
* @return the pageBiz
*/
public PageBiz getPageBiz() {
return pageBiz;
}
/**
* @param pageBiz the pageBiz to set
*/
public void setPageBiz(PageBiz pageBiz) {
this.pageBiz = pageBiz;
}
/**
* @return the pageL
*/
public Page getPageL() {
return pageL;
}
/**
* @param pageL the pageL to set
*/
public void setPageL(Page pageL) {
this.pageL = pageL;
}
/**
* @return the demo2
*/
public Demo2 getDemo2() {
return demo2;
}
/**
* @param demo2 the demo2 to set
*/
public void setDemo2(Demo2 demo2) {
this.demo2 = demo2;
}

}


/**applicati
f105
onContext.xml文件中*/
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> 
<!-- 庄庆瑜 -->
<bean id="pageDao" class="daoImpl.PageDaoImpl">
</bean>

<!-- 配置BIZ -->

<!-- 庄庆瑜 -->
<bean id="pageBiz" class="bizImpl.PageBizImpl">
<property name="pageDao" ref="pageDao"></property>
</bean>

<!-- 配置Action -->

<!-- 庄庆瑜  -->
<bean id="fenyeAction" class="action.FenyeAction" scope="prototype">
<property name="pageBiz" ref="pageBiz"></property>
</bean>

</beans>


/**struts.xml文件中*/
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<!-- 庄庆瑜 -->
<action name="*Action" class="action.FenyeAction" method="{1}">
<result name="fenye">fenye.jsp</result>
</action>
</package>
</struts>


/**JSP页面*/
/**fp.jsp*/
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="fenyeAction.action">点击</a>

</body>
</html>


/**fenye.jsp未修改版,例如name应改为name=${demo2.recordId}*/
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--  <%@ taglib prefix="c" uri="" %> --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
function changePage(page){
document.getElementById("page").value=page;
myForm.submit();
}
</script>
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<body>
<form name="myForm" id="myForm" action="fenyeAction.action">
<!-- 隐藏域,当前页码 -->
<input type="hidden" id="pageL.page" name="pageL.page" value="${pageL.page }">
<input type="hidden" id="pageL.pageCount" name="pageL.pageCount" value="${pageL.pageCount }">
<div>
<div class="page-bar">
共${pageL.pageCount }条记录   ${pageL.page }/${pageL.pageCount }页  
<a href="javascript:changePage(1)">首页</a>  
<a href="javascript:changePage(${pageL.backPage })">上一页</a>  
<a href="javascript:changePage(${pageL.nextPage })">下一页</a>  
<a href="javascript:changePage(${pageL.pageCount })">尾页</a>
</div>
<!-- 包都没有导 -->
</div>
<div id="content" name="content">
输入记录号:<input type="text" name="recordId" id="recordId">  
输入分类号:<input type="text" name="sortId" id="sortId">  
输入公告号:<input type="text" name="publishId" id="publishId">  
输入公告名:<input type="text" name="publishName" id="publishName">  
输入作者:  <input type="text" name="author">
<table id="myTab" name="myTab">
<tr>
<td>记录号</td>
<td>公告日</td>
<td>公告号</td>
<td>作者</td>
<td>标题</td>
</tr>
<c:forEach var="demo" items="${pageL.demoList }">
<tr>
<td>${demo.recordId }</td>
<td>${demo.date }</td>
<td>${demo.publishId }</td>
<td>${demo.author }</td>
<td>${demo.title }</td>
</tr>
</c:forEach>
</table>
</div>
</form>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  分页 java strut2 spring