?? spaceservice.java
字號(hào):
package com.serviceImp;import java.util.*;import com.bean.*;import static com.common.HibernateSessionFactory.*;import com.common.*;import java.sql.*;import org.hibernate.*;/** * 論壇的業(yè)務(wù)邏輯層 * 提供,發(fā)帖,回帖以及按照順序分頁(yè)顯示等功能 */public class SpaceService { //工廠 public static SpaceService getSpaceService(){ return new SpaceService(); } //刷新訪問(wèn)量 public void refreshCount(Talk talk){ //get() //t.setcount(t.getCount()+1); //update set count=count+1; } //發(fā)帖 public boolean add(User user,String talkName,String talkContent,boolean top){ Talk talk=new Talk(); talk.setTalkName(talkName); talk.setTalkContent(talkContent); talk.setUser(user); talk.setTop(top); Session session = getSession(); Transaction trans = session.beginTransaction(); session.save(talk); trans.commit(); session.close(); return true; }//--------------------------------------------------------------------------------------------------------------------- //按照id還原Talk對(duì)象 public Talk getTalk(final Long id ,boolean isCommited){ Session session=getSession(); Transaction trans=session.beginTransaction(); final Talk talk=(Talk)session.get(Talk.class,id); Hibernate.initialize(talk.getUser()); //初始化一定不要忘記(JDBC與hibernate融合時(shí)) Connection conn=session.connection(); String sql="select timestr from zhao_talk_tbl where id=?"; JDBCTemplate temp=new JDBCTemplate(); temp.query(sql,new PreparedStatementSetter(){ public void setter(PreparedStatement pstmt)throws SQLException{ pstmt.setLong(1,id); } },new Handler(){ public void handler(ResultSet rs) throws SQLException { if (rs.next()) talk.setTimeStr(rs.getTimestamp(1).toLocaleString()); } },conn); if(isCommited){ trans.commit(); session.close(); } return talk; }//----------------------------------------------------------------------------------------------------------------- //分頁(yè)功能實(shí)現(xiàn): 第幾頁(yè) 每頁(yè)記錄數(shù) 按照什么排序(置頂不排序) //按照時(shí)間(默認(rèn))-timestr,點(diǎn)擊量-count,回帖量-space //升序還是降序asc/desc public List<Talk> getTalk(int pageCount,int count,String orderKey,String orderType){ Session session=getSession(); Transaction trans=session.beginTransaction(); Connection conn=session.connection(); List<Talk> sublist=null; final List<Talk> ntoplist=new ArrayList<Talk>(); //向集合list中先放置頂?shù)奶?********************************************* final List<Talk> list=new ArrayList<Talk>(); JDBCTemplate temp=new JDBCTemplate(); String sql="select id from zhao_talk_tbl where top=1 order by timestr desc "; temp.query(sql, new Handler(){ public void handler(ResultSet rs)throws SQLException{ while(rs.next()) { list.add(getTalk(rs.getLong(1),false)); } } }, conn); //按照回帖量排序************************************************************ if("space".equals(orderKey)){ //回帖量為0 final List<Talk> ntoplist1=new ArrayList<Talk>(); String ntopsql1="select id from zhao_talk_tbl where id not in(select talkid from zhao_space_tbl) and top=0" +"order by timestr asc"; JDBCTemplate temp2=new JDBCTemplate(); temp2.query(ntopsql1, new Handler(){ public void handler(ResultSet rs)throws SQLException{ while(rs.next()) { ntoplist1.add(getTalk(rs.getLong(1),false)); } } }, conn); //回帖量不為0 final List<Talk> ntoplist2=new ArrayList<Talk>(); String ntopsql2="select talkid,count(*) from zhao_space_tbl group by talkid order by count(*)" +"order by "+orderKey+" "+orderType; JDBCTemplate temp3=new JDBCTemplate(); temp3.query(ntopsql2, new Handler(){ public void handler(ResultSet rs)throws SQLException{ while(rs.next()) { ntoplist2.add(getTalk(rs.getLong(1),false)); } } }, conn); if("asc".equals(orderType)){ ntoplist.addAll(0,ntoplist1); ntoplist.addAll(ntoplist.size(),ntoplist2); }else{ ntoplist.addAll(0,ntoplist2); ntoplist.addAll(ntoplist.size(),ntoplist1); } }else{ //按照時(shí)間或點(diǎn)擊量向集合ntoplist中放置非置頂?shù)奶?********************** String ntopsql="select id from zhao_talk_tbl where top=0 order by "+orderKey+" "+orderType; JDBCTemplate temp2=new JDBCTemplate(); temp2.query(ntopsql, new Handler(){ public void handler(ResultSet rs)throws SQLException{ while(rs.next()) { ntoplist.add(getTalk(rs.getLong(1),false)); } } }, conn); } //通過(guò)以上會(huì)得到兩個(gè)數(shù)組:一個(gè)是list(已經(jīng)放了置頂帖子),一個(gè)是ntoplist(不置頂帖子的集合) int topCount=list.size(); int ntopstep=0; if((count-topCount)*pageCount<=ntoplist.size()){ ntopstep=count-topCount; sublist=ntoplist.subList(ntopstep*(pageCount-1),ntopstep*(pageCount-1)+ntopstep); }else{ ntopstep=count-topCount; int ntopstep2=ntoplist.size()-(count-topCount)*(pageCount-1);//ntopstep2大于0合法還是小于0不合法 sublist=ntoplist.subList(ntopstep*(pageCount-1),ntopstep*(pageCount-1)+ntopstep2); } list.addAll(sublist); trans.commit(); session.close(); return list; } //**********************************space**************************//------------------------------------------------------------------------------------------------------------ //按照id查找某一回帖 public Space getSpace(final Long id,boolean isCommited){ Session session = getSession(); Transaction trans = session.beginTransaction(); final Space space=(Space)session.get(Space.class,id); Connection conn=session.connection(); //整合 String sql="select timestr from zhao_space_tbl where id=?"; JDBCTemplate temp=new JDBCTemplate(); temp.query(sql,new PreparedStatementSetter(){ public void setter(PreparedStatement pstmt)throws SQLException{ pstmt.setLong(1,id); } },new Handler(){ public void handler(ResultSet rs) throws SQLException { if (rs.next()) space.setTimeStr(rs.getTimestamp(1).toLocaleString()); } },conn); Hibernate.initialize(space.getUser()); //初始化一定不要忘記(JDBC與hibernate融合時(shí)) Hibernate.initialize(space.getTalk()); //初始化一定不要忘記(JDBC與hibernate融合時(shí)) if(isCommited){ trans.commit(); session.close(); } return space; }//-------------------------------------------------------------------------------------------------------- //向主題回復(fù)帖子 public boolean addSpace(User user,Talk talk,String spaceContent){ Space space=new Space(); space.setUser(user); space.setTalk(talk); space.setSpaceContent(spaceContent); Session session = getSession(); Transaction trans = session.beginTransaction(); try{ session.save(space); trans.commit(); session.close(); return true; }catch(Exception e){ //trans.rollback(); return false; } }//--------------------------------------------------------------------------------------------------------- //分頁(yè)功能 talkid:針對(duì)某一主題的回帖的分頁(yè)顯示 public List<Space> getSpace(int pageCount,int count,String orderKey,String orderType,final Long talkid){ Session session=getSession(); Transaction trans=session.beginTransaction(); String sql="select id from zhao_space_tbl where talkid=? order by "+orderKey+" "+orderType; //存放回帖 final List<Space> list=new ArrayList<Space>(); Connection conn=session.connection(); JDBCTemplate temp=new JDBCTemplate(); temp.query(sql,new PreparedStatementSetter(){ public void setter(PreparedStatement pstmt)throws SQLException{ pstmt.setLong(1,talkid); } },new Handler(){ public void handler(ResultSet rs) throws SQLException { while(rs.next()){ list.add(getSpace(rs.getLong(1),false)); } } },conn); trans.commit(); session.close(); int last=list.size(); if(pageCount*count<list.size()) last=pageCount*count; return list.subList(count*(pageCount-1),last); } public List<Space> getSpace(int pageCount){ return getSpace(pageCount,10,"timestr","asc",10L); } public List<Space> getSpace(int pageCount,Long talkid){ return getSpace(pageCount,10,"timestr","asc",talkid); }//---------------------------------------以下為測(cè)試用代碼-------------------------------------------------------------- public static void main(String[] args) { User user=new User(); user.setId(new Long(32)); Talk talk=new Talk(); talk.setId(new Long(50)); SpaceService service=new SpaceService(); //service.add(user, "站內(nèi)公告","文明發(fā)帖",true); //service.add(user, "如何過(guò)春節(jié)","在家",false); //service.add(user, "如何找個(gè)好工作","不知道呀",false); //service.add(user, "北京發(fā)大水了","好慘納",false); //service.add(user, "中國(guó)人必看","支持!!",false); //service.add(user, "aa","11",false); //service.add(user, "bb","22",true); //service.add(user, "cc","33",false); //service.add(user, "dd","44",false); //service.add(user, "ee","55",false); //service.add(user, "ff","66",false); service.add(user, "gg","77",true); service.add(user, "hh","88",false); service.add(user, "ii","99",false); service.add(user, "ii","110",false); //System.out.println(service.add(user, "主題","主題內(nèi)容kjkjjkkjk",false)); //System.out.println(service.getTalk(new Long(50),true)); //service.addSpace(user, talk, "yyyyyyyyyyy"); //for(Space space:service.getSpace(1,new Long(50))){ // System.out.println(talk); //} }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -