Java tutorial
/* * WebimModel.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.Date; import java.util.HashMap; import java.util.Map; import org.apache.ibatis.session.SqlSession; /** * Webim? <br> * * MySQL?: <br> * * <pre> * DROP TABLE IF EXISTS webim_settings; * CREATE TABLE webim_settings ( * `id` int(11) unsigned NOT NULL AUTO_INCREMENT, * `uid` varchar(40) NOT NULL DEFAULT '', * `data` text, * `created` datetime DEFAULT NULL, * `updated` datetime DEFAULT NULL, * UNIQUE KEY `webim_setting_uid` (`uid`), * PRIMARY KEY (`id`) * ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; * * Oracle?: * * create table WEBIM_SETTINGS ( * USERNAME VARCHAR2(32), * SETTING VARCHAR2(1000) * ); * -- Add comments to the columns * comment on column WEBIM_SETTINGS.USERNAME is '??'; * comment on column WEBIM_SETTINGS.SETTING is '??'; * * </pre> * * @author Feng Lee <feng.lee@nextalk.im> * * @since 5.7 */ // @Repository("webimSettingDao") public class WebimSettingDao extends WebimDaoSupport { /** * ???MySQL?SQL<br> * * "select data from webim_settings where uid = ?"<br> * * data: "{}" * * @param uid * uid * @return ??JSON? */ public String get(String uid) { String data = null; SqlSession session = sessionFactory.openSession(); try { data = session.selectOne("SettingMapper.getSetting", uid); } finally { session.close(); } if (data == null) data = "{}"; return data; } /** * ??MySQL?: <br> * * "update webim_settings set data = ? where uid = ?" <br> * * ?????? * * @param uid * uid * @param data * ??JSON? */ public void set(String uid, String data) { Map<String, Object> map = new HashMap<String, Object>(); Date now = new Date(); map.put("uid", uid); map.put("data", data); map.put("created", now); map.put("updated", now); SqlSession session = sessionFactory.openSession(); try { String oldData = (String) session.selectOne("SettingMapper.getSetting", uid); if (oldData == null) { session.insert("SettingMapper.insertSetting", map); } else { session.update("SettingMapper.updateSetting", map); } session.commit(); } finally { session.close(); } } }