1. 程式人生 > >91---Criteria的內連結,分頁,查詢唯一物件,條件查詢,分組統計,投影查詢,DetachedCriteria

91---Criteria的內連結,分頁,查詢唯一物件,條件查詢,分組統計,投影查詢,DetachedCriteria

================Criteria分頁加動態查詢方法======================================
public PageInfo<Auction> select(Auction condition, int pageIndex){

DetachedCriteria dc = DetachedCriteria.forClass(Auction.class);
Criteria c = session.createCriteria(Auction.class);//建立一次查詢
Criteria c1 = session.createCriteria(Auction.class);//建立一次查詢

c.addOrder(Order.desc("auctionstarttime"));//新增查詢條件-----------注意非空處理----------排序
c.add(Restrictions.ge("auctionstartprice",condition.getAuctionstartprice()));----------比較大小
c.add(Restrictions.ilike("auctiondesc", condition.getAuctiondesc(),MatchMode.ANYWHERE));----------模糊查詢

Set<Integer> settype=new HashSet<Integer>();c.add(Restrictions.in( "movId",settype)); //--------編號在集合陣列中

PageInfo<Auction> pageInfo = new PageInfo<Auction>();//例項分頁方法
int count = (Integer) c1.setProjection(Projections.rowCount()).uniqueResult();//得到總條數
pageInfo.setCount(count);//設定總條數
pageInfo.setPageIndex(pageIndex);//設定當前頁碼
c.setMaxResults(PageInfo.PAGESIZE);//設定每次的查詢數量
c.setFirstResult((pageIndex - 1) * PageInfo.PAGESIZE);//設定起始條數
List<Auction> list = c.list();
pageInfo.setPageList(list);//將查詢的結果付給查詢的實體類
}
===============================分頁實體類======================================
public class PageInfo<T> { //利用泛型宣告一個分頁的實體類
public static final int PAGESIZE = 10;
private Integer count;// 總記錄數
private List<T> pageList;// 當前頁的記錄集合
private Integer pageIndex;// 當前頁號
private Integer totalPages;// 總頁數

public Integer getTotalPages() {//特殊方法,通過總條數和每頁的條數得到頁數
this.totalPages = this.count / this.PAGESIZE;
if (this.count % this.PAGESIZE != 0)
this.totalPages++;
return this.totalPages;
}
}
===============================分頁關於時間的處理======================================
---對映檔案-------關於時間的處理---------------
         <property name="auctionstarttime" type="java.sql.Timestamp">
            <column name="AUCTIONSTARTTIME" length="11" not-null="true" />
        </property>

---實體類-------關於時間的處理---------------
        private Timestamp auctionstarttime;、

---資料庫-------關於時間的處理---------------
        AUCTIONSTARTTIME             TIMESTAMP(6)
---方法內使用-------關於時間的處理---------------
======表單賦值
<label for="time">開始時間</label>
<input name="auctionStartTime" type="text" id="time" class="nwinput"/>
======提取:輸入的時間按字串轉化為date,後提取毫秒數,轉化為java.sql.Timestamp
           if(request.getParameter("auctionStartTime")!=null&&!"".equals(request.getParameter("auctionStartTime"))){
condition.setAuctionstarttime(new java.sql.Timestamp(Tool.strToDate(request.getParameter("auctionStartTime"), "yyyy-MM-dd HH:mm:dd").getTime()));
}
======底層
     if (condition.getAuctionstarttime() != null) {
c.add(Restrictions.ge("auctionstarttime",
condition.getAuctionstarttime()));
c1.add(Restrictions.ge("auctionstarttime",
condition.getAuctionstarttime()));
}


===============================servlet的呼叫方法以及獲得返回值======================================


int pageIndex=1;//登陸介面進入
if(request.getParameter("pageIndex")!=null){
pageIndex=new Integer(request.getParameter("pageIndex")).intValue();//
}Auction condition=new Auction();

PageInfo<Auction> auctionPageinfo = biz.find(condition,pageIndex);
request.setAttribute("auctionPageInfo", auctionPageinfo);
request.getRequestDispatcher("auctionList.jsp").forward(request, response);




===============================jsp頁面======================================


---------頁碼顯示------------------------------------------------------------------------------
       <a href="javascript:goToPage(1)">首頁</a>
        <c:if test="${auctionPageinfo.pageIndex!=1}">
        <a href="javascript:goToPage(${auctionPageinfo.pageIndex-1 })">上一頁</a>
        </c:if>&nbsp;&nbsp;&nbsp;&nbsp;
         當前頁:${auctionPageinfo.pageIndex}&nbsp;&nbsp;&nbsp;&nbsp;
         
        <c:forEach step="1" begin="1" end="${auctionPageInfo.totalPages}" var="pageIndex">
        <a href="javascript:goToPage(${pageIndex})">${pageIndex}</a>
        </c:forEach> 
      
        <c:if test="${auctionPageinfo.pageIndex!=auctionPageinfo.totalPages}">
        <a href="javascript:goToPage(${auctionPageinfo.pageIndex+1 })">下一頁</a>
        </c:if>
        <a href="javascript:goToPage(${auctionPageinfo.totalPages})" >尾頁</a>


------------------
<script type="text/javascript">
function goToPage(pageIndex){
document.forms[0].action=document.forms[0].action+"?pageIndex="+pageIndex;
document.forms[0].submit();
}
</script>
---------動態查詢表單------------------------------------------------------------------------------
<form action="goodservlet?opr=list&ad=u" method="post">
    <label for="name">名稱</label>
   <input name="auctionname" type="text" class="nwinput" id="name" value="${param.auctionname}"/>
       <label for="names">描述</label>
       <input name="auctiondesc" type="text" id="names" class="nwinput" value="${param.auctiondesc}"/>
       <label for="time">開始時間</label>
       <input name="auctionstarttime" type="text" id="time" class="nwinput"  value="${param.auctionstarttime}"/>
       <label for="end-time">結束時間</label>
       <input name="auctionendtime" type="text" id="end-time" class="nwinput"  value="${param.auctionendtime}"/>
       <label for="price">起拍價</label>
   <input name="auctionstartprice" type="text" id="price" class="nwinput" value="${param.auctionstartprice}"/>
   <input name="" type="submit"  value="查詢" class="spbg buttombg f14  sale-buttom"/>
    </form>


---------詳細資訊顯示------------------------------------------------------------------------------
 <c:forEach items="${requestScope.auctionPageInfo.pageList }" var="auction">
      <ul class="rows">
        <li>${auction.auctionname }</li>
        <li class="list-wd">${auction.auctiondesc }</li>
        <li>${auction.auctionstarttime }</li>
        <li>${auction.auctionendtime }</li>
        <li>${auction.auctionstartprice }</li>
        <li class="borderno red">
        <c:if test="${sessionScope.user.userisadmin==true }">
           修改|
          刪除
          </c:if>
        <c:if test="${sessionScope.user.userisadmin==false }">
          <a href="auctionDetail?auctionId=${auction.auctionid }">競拍</a>
          </c:if>
        </li>
      </ul>
      </c:forEach>