com.handu.open.dubbo.monitor.dao.base.DubboInvokeBaseDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.handu.open.dubbo.monitor.dao.base.DubboInvokeBaseDAO.java

Source

/**
 * Copyright 2006-2015 handu.com
    
 * <p/>
 * Licensed 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 com.handu.open.dubbo.monitor.dao.base;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * DAO
 *
 * @author Jinkai.Ma
 */
@Repository
public class DubboInvokeBaseDAO extends SqlSessionDaoSupport {

    /**
     * 
     *
     * @param sqlSessionFactory sql
     */
    @Autowired
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }

    /**
     * ?
     *
     * @param prefix ?
     * @param key 
     * @param object ?
     */
    public void insert(String prefix, String key, Object object) {
        getSqlSession().insert(prefix + key, object);
    }

    /**
     * 
     *
     * @param prefix ?
     * @param key 
     * @param object ?
     */
    public void update(String prefix, String key, Object object) {
        getSqlSession().update(prefix + key, object);
    }

    /**
     * 
     *
     * @param prefix ?
     * @param key 
     * @param id ID
     */
    public void delete(String prefix, String key, Serializable id) {
        getSqlSession().delete(prefix + key, id);
    }

    /**
     * 
     *
     * @param key 
     * @param id ID
     */
    public void delete(String key, Serializable id) {
        getSqlSession().delete(key, id);
    }

    /**
     * 
     *
     * @param prefix ?
     * @param key 
     * @param object 
     */
    public void delete(String prefix, String key, Object object) {
        getSqlSession().delete(prefix + key, object);
    }

    /**
     * ???
     *
     * @param <T> 
     * @param prefix ?
     * @param key 
     * @param params ?
     * @return T 
     */
    public <T> T get(String prefix, String key, Object params) {
        return getSqlSession().selectOne(prefix + key, params);
    }

    /**
     * ??
     *
     * @param <T> 
     * @param prefix ?
     * @param key 
     * @return T 
     */
    public <T> List<T> getList(String prefix, String key) {
        return getSqlSession().selectList(prefix + key);
    }

    /**
     * ???
     *
     * @param <T> 
     * @param prefix ?
     * @param key 
     * @param params ?
     * @return T 
     */
    public <T> List<T> getList(String prefix, String key, Object params) {
        return getSqlSession().selectList(prefix + key, params);
    }

    /**
     * ??
     *
     * @param prefix ?
     * @param key 
     * @param params ?
     * @return Integer
     */
    public Integer count(String prefix, String key, Object params) {
        return getSqlSession().selectOne(prefix + key, params);
    }

    /**
     * 
     *
     * @param prefix ?
     * @param pageKey 
     * @param countKey ?
     * @param params ?
     * @param offset ???
     * @param limit ??
     * @return Object[]
     */
    public Object[] page(String prefix, String pageKey, String countKey, Object params, int offset, int limit) {
        return new Object[] { getSqlSession().selectList(prefix + pageKey, params, new RowBounds(offset, limit)),
                getSqlSession().selectOne(prefix + countKey, params) };
    }

    /**
     * SQL?
     *
     * @param sql SQL?
     * @return boolean
     */
    public boolean executeSql(String sql) {
        try {
            return getSqlSession().getConnection().prepareStatement(sql).execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * SQL?
     *
     * @param sql SQL?
     * @return List<Map>
     */
    public List<Map> querySql(String sql) {
        List<Map> list = Lists.newArrayList();
        try {
            ResultSet rs = getSqlSession().getConnection()
                    .prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)
                    .executeQuery();
            try {
                ResultSetMetaData rsm = rs.getMetaData(); //
                int col = rsm.getColumnCount(); //
                String[] colName = new String[col];
                //???, colName
                for (int i = 0; i < col; i++) {
                    colName[i] = rsm.getColumnName(i + 1);
                }
                rs.beforeFirst();
                while (rs.next()) {
                    Map<String, String> map = Maps.newHashMap();
                    for (String aColName : colName) {
                        map.put(aColName, rs.getString(aColName));
                    }
                    list.add(map);
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}