SqlMapFactory.java 3.19 KB
package com.sitech.util;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

public class SqlMapFactory {
	private static Map<String, SqlMapClient> sqlMapClientMap = new HashMap<String, SqlMapClient>();

	private static String defaultSqlMapConfig = "sqlmap.xml";

	public static synchronized SqlMapClient getDefaultSqlMapClient() {
		return getSqlMapClient(defaultSqlMapConfig);
	}

    public static synchronized SqlMapClient getOracleSqlMapClient(String mapConfig) {
        return getSqlMapOracleClient(mapConfig);
    }

	public static synchronized SqlMapClient getSqlMapClient(String config) {
		if (sqlMapClientMap.get(config) == null) {
			String resource = config;
			Reader reader;
			SqlMapClient sqlMap = null;
			try {

				ResourceBundle LoginInfo = ResourceBundle.getBundle("LoginInfo");
				String sClassforName = LoginInfo.getString("ClassforName");
				String sUrl = LoginInfo.getString("url");
				String sUserName = LoginInfo.getString("sqluser");
				String sPassword = LoginInfo.getString("sqlpassword");
				sPassword = DES3.decrypt(sPassword);
				Properties properties = new Properties();
				properties.put("SERVERDriver", sClassforName);
				properties.put("SERVERConnectionURL", sUrl);
				properties.put("SERVERUsername", sUserName);
				properties.put("SERVERPassword", sPassword);

				reader = Resources.getResourceAsReader(resource);

				sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader, properties);
			} catch (Exception e) {
				throw new RuntimeException(e);
			} finally {
			}
			sqlMapClientMap.put(config, sqlMap);
			return sqlMap;
		} else {
			return (SqlMapClient) sqlMapClientMap.get(config);
		}
	}

    public static synchronized SqlMapClient getSqlMapOracleClient(String config) {
        if (sqlMapClientMap.get(config) == null) {
            String resource = config;
            Reader reader;
            SqlMapClient sqlMap = null;
            try {

                ResourceBundle dbInfo = ResourceBundle.getBundle("db");
                String sClassforName = dbInfo.getString("driver");
                String sUrl = dbInfo.getString("url");
                String sUserName = dbInfo.getString("sqluser");
                String sPassword = dbInfo.getString("sqlpassword");
                sPassword = DES3.decrypt(sPassword);
                Properties properties = new Properties();
                properties.put("SERVERDriver", sClassforName);
                properties.put("SERVERConnectionURL", sUrl);
                properties.put("SERVERUsername", sUserName);
                properties.put("SERVERPassword", sPassword);

                reader = Resources.getResourceAsReader(resource);

                sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader, properties);
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
            }
            sqlMapClientMap.put(config, sqlMap);
            return sqlMap;
        } else {
            return (SqlMapClient) sqlMapClientMap.get(config);
        }
    }
}