Java tutorial
/* * WebimHistoryDao.java * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package webim.dao.ibatis; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.session.SqlSession; import webim.model.WebimHistory; /** * Webim??? * * MySQL Table:<br> * * <pre> * CREATE TABLE webim_histories ( * `id` int(11) unsigned NOT NULL AUTO_INCREMENT, * `send` tinyint(1) DEFAULT NULL, * `type` varchar(20) DEFAULT NULL, * `to` varchar(50) NOT NULL, * `from` varchar(50) NOT NULL, * `nick` varchar(20) DEFAULT NULL COMMENT 'from nick', * `body` text, * `style` varchar(150) DEFAULT NULL, * `timestamp` double DEFAULT NULL, * `todel` tinyint(1) NOT NULL DEFAULT '0', * `fromdel` tinyint(1) NOT NULL DEFAULT '0', * `createdat` date DEFAULT NULL, * `updatedat` date DEFAULT NULL, * PRIMARY KEY (`id`), * KEY `timestamp` (`timestamp`), * KEY `to` (`to`), * KEY `from` (`from`), * KEY `send` (`send`) * ) ENGINE=MyISAM; * </pre> * * Oracle Table: <br> * * <pre> * * ---------------------------- * * create table WEBIM_HISTORIES * ( * ID NUMBER(19) not null, * MSG_TYPE VARCHAR2(20), * SEND NUMBER(1), * TO_USER VARCHAR2(50), * FROM_USER VARCHAR2(50), * NICK VARCHAR2(20), * MSG_BODY VARCHAR2(4000), * STYLE VARCHAR2(150), * MSG_TIMESTAMP FLOAT, * ISDEL VARCHAR2(1), * CREATED DATE, * UPDATED DATE, * TODEL VARCHAR2(1), * FROMDEL VARCHAR2(1) * ); * -- Add comments to the columns * comment on column WEBIM_HISTORIES.ID is ''; * comment on column WEBIM_HISTORIES.MSG_TYPE is '?'; * comment on column WEBIM_HISTORIES.SEND is '??'; * comment on column WEBIM_HISTORIES.TO_USER is ''; * comment on column WEBIM_HISTORIES.FROM_USER is '??'; * comment on column WEBIM_HISTORIES.NICK is ''; * comment on column WEBIM_HISTORIES.MSG_BODY is '?'; * comment on column WEBIM_HISTORIES.STYLE is ''; * comment on column WEBIM_HISTORIES.MSG_TIMESTAMP is ''; * comment on column WEBIM_HISTORIES.ISDEL is ''; * comment on column WEBIM_HISTORIES.CREATED is ''; * comment on column WEBIM_HISTORIES.UPDATED is ''; * -- Create/Recreate primary, unique and foreign key constraints * alter table WEBIM_HISTORIES * add constraint WEBIM_HISTORIES_PK primary key (ID); * * ---------------------------- * </pre> */ //@Repository("webimHistoryDao") public class WebimHistoryDao extends WebimDaoSupport { /** * ?with?MySQL:<br> * * <pre> * if (type == "chat") * { * * "SELECT * FROM webim_Histories WHERE `type` = 'chat' * AND ((`to`=%s AND `from`=%s AND `fromdel` != 1) * OR (`send` = 1 AND `from`=%s AND `to`=%s AND `todel` != 1)) * ORDER BY timestamp DESC LIMIT %d", $with, $uid, $with, $uid, $limit ); * * } * else * { * * "SELECT * FROM webim_histories * WHERE `to`=%s AND `type`='grpchat' AND send = 1 * ORDER BY timestamp DESC LIMIT %d", , $with, $limit); * * } * </pre> * * @param uid * ?id * @param with * id????long * @param type * chat | grpchat * * @param limit * ? * @return ? */ public List<WebimHistory> getHistories(String uid, String with, String type, int limit) { List<WebimHistory> histories = null; Map<String, String> params = new HashMap<String, String>(); //with -> to params.put("to", with); //uid -> from params.put("from", uid); RowBounds rb = new RowBounds(0, limit); SqlSession session = sessionFactory.openSession(); try { if (type.equals("chat")) { histories = session.selectList("HistoryMapper.selectChat", params, rb); } else if (type.equals("grpchat")) { histories = session.selectList("HistoryMapper.selectGrpChat", params, rb); } else { } } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } if (histories == null) histories = new ArrayList<WebimHistory>(); Collections.reverse(histories); return histories; } /** * ??MySQL:<br> * * "SELECT * FROM webim_histories WHERE `to` = ? and send != 1 ORDER BY timestamp DESC LIMIT %d" * , limit; * * @param uid * uid * @return ? */ public List<WebimHistory> getOfflineHistories(String uid, int limit) { List<WebimHistory> histories = null; SqlSession session = sessionFactory.openSession(); try { histories = session.selectList("HistoryMapper.selectOffline", uid, new RowBounds(0, limit)); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } if (histories == null) histories = new ArrayList<WebimHistory>(); Collections.reverse(histories); return histories; } public void insertHistory(WebimHistory history) { SqlSession session = sessionFactory.openSession(); try { session.insert("HistoryMapper.insertHistory", history); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } } /** * with?MySQL:<br> * * "UPDATE webim_histories SET fromdel = 1 Where from = @0 and to = @1 and type = 'chat'" * <br> * "UPDATE webim_histories SET todel = 1 Where to = @0 and from = @1 and type = 'chat'" * <br> * "DELETE FROM webim_histories WHERE fromdel = 1 AND todel = 1" * * @param uid * uid * @param with * id,????long */ public void clearHistories(String uid, String with) { Map<String, String> params = new HashMap<String, String>(); params.put("uid", uid); params.put("with", with); SqlSession session = sessionFactory.openSession(); try { session.update("HistoryMapper.clearFromHistory", params); session.update("HistoryMapper.clearToHistory", params); session.delete("HistoryMapper.deleteHistory"); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } } /** * ????MySQL:<br> * * "UPDATE webim_histories SET send = 1 where to = ? and send = 0"); * * @param uid * uid */ public void offlineHistoriesReaded(String uid) { SqlSession session = sessionFactory.openSession(); try { session.update("HistoryMapper.offlineReaded", uid); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } } public void clearAll() { SqlSession session = sessionFactory.openSession(); try { session.delete("HistoryMapper.clearAll"); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } } }