mybatis-连接池

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE configuration 
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration>
    <properties resource="config/db.properties" />
    <settings> 
        <!-- changes from the defaults for testing --> 
        <setting name="cacheEnabled" value="false" /> 
        <setting name="useGeneratedKeys" value="true" /> 
        <setting name="defaultExecutorType" value="REUSE" /> 
    </settings>
    <environments default="development"> 
       <environment id="development"> 
           <transactionManager type="jdbc"/> 
           <dataSource type="POOLED"> 
              <property name="driver" value="${driver}"/> 
              <property name="url" value="${url}"/> 
              <property name="username" value="${username}"/> 
              <property name="password" value="${password}"/>
              <!-- POOLED  -->
              <property name="poolMaximumActiveConnections" value="100"/>
              <property name="poolMaximumIdleConnections" value="0"/>
              <property name="poolTimeToWait" value="20000"/>
           </dataSource> 
       </environment> 
    </environments>
    <mappers>
        <package name="com.chinacreator.appserver.base.sqlmapper"/>
        <package name="com.chinacreator.appserver.produce.sqlmapper"/>
        <package name="com.chinacreator.appserver.business.sqlmapper"/>
    </mappers>
</configuration> 
View Code
package com.chinacreator.appserver.base.util;

import java.io.IOException;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.log4j.Logger;

import com.chinacreator.appserver.base.dictionary.ErrorConstant;
import com.chinacreator.appserver.base.dictionary.SysConstant;
import com.chinacreator.appserver.base.exception.ExceptionAbstract;
import com.chinacreator.appserver.base.exception.SystemException;
import com.chinacreator.appserver.base.servlet.CacheManager;
/*
 *sql工具类
 *Title:SqlUtil.java
 *Copyright:Copyright(c)2013
 *Company:chinacreator
 *@author 刘春元
 *@version 1.0
 *2013-5-8
 */
public class SqlUtil {
    private static final Logger logger = Logger.getLogger(SqlUtil.class.getName());
    private static SqlUtil uniqueInstance = null;
    private static SqlSessionFactory sqlSessionFactory; 
    private static String resource = "sqlconfig.xml"; 
    //TransactionFactory transactionFactory = new JdbcTransactionFactory();
    private SqlUtil() {
        logger.debug("init SqlManager .....");
    }
    public static SqlUtil getInstance(){
        if (uniqueInstance == null) {
            uniqueInstance = new SqlUtil();
        }
        return uniqueInstance;
    }
    public void init() throws ExceptionAbstract{
        try {
            Reader reader = Resources.getResourceAsReader(resource); 
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            throw new SystemException(ErrorConstant.SYSTEM_Z02,ErrorConstant.SYSTEM_Z02_DESC);
        } 
    }

    public SqlSessionFactory getSqlSessionFactory() { 
           return sqlSessionFactory; 
    }
    /*
     * 查询一条
     *@param sqlid
     *@param inmap
     *@return
     *作者:刘春元
     *2013-5-13
     */
    public Map<String, Object> selectOne(String sqlid,Map<String, Object> inmap) throws ExceptionAbstract{
        Map<String, Object> outmap = new HashMap<String, Object>();
        SqlSession sqlsession = sqlSessionFactory.openSession();
        try {
            outmap = sqlsession.selectOne(sqlid, inmap);
            if(null !=outmap){
                StringUtil.mapconvert(outmap);
            }
        } catch (Exception e) {
            throw new SystemException(ErrorConstant.SYSTEM_A01,ErrorConstant.SYSTEM_A01_DESC);
        }finally{
            sqlsession.close();
        }
        return outmap;
    }
    public Object selectOneString(String sqlid,Map<String, String> inmap) throws ExceptionAbstract{
        Object str = "";
        SqlSession sqlsession = sqlSessionFactory.openSession();
        try{
            str = sqlsession.selectOne(sqlid, inmap);
            if(str instanceof Clob){
                str = StringUtil.ClobToString((Clob) str);
            }if(str instanceof Blob){//附件
                str = StringUtil.BlobToStream((Blob) str,inmap);
            }
        }catch (Exception e) {
            throw new SystemException(ErrorConstant.SYSTEM_A02,ErrorConstant.SYSTEM_A02_DESC);
        }finally{
            sqlsession.close();
        }
        return str;
    }
    /*
     * 查询所有
     *@param sqlid
     *@return
     *作者:刘春元
     *2013-5-13
     */
    public List<Map<String,Object>> selectList(String sqlid, Map<String, Object> inmap) throws ExceptionAbstract{
         List<Map<String,Object>> outListMap = new ArrayList<Map<String,Object>>();
         SqlSession sqlsession = sqlSessionFactory.openSession();
         try {
            outListMap = sqlsession.selectList(sqlid,inmap);
            if(outListMap.size() > 0){
                StringUtil.listconvert(outListMap);
            }
        } catch (Exception e) {
            try {
                throw new SystemException(ErrorConstant.SYSTEM_A03,new String(CacheManager.getInstance().getCacheByKey("sql", sqlid).getBytes("ISO-8859-1"),"UTF-8")+"^"+ErrorConstant.SYSTEM_A03_DESC);
            } catch (UnsupportedEncodingException e1) {
                throw new SystemException(ErrorConstant.SYSTEM_Z01,ErrorConstant.SYSTEM_Z01_DESC);
            }
        }finally{
            sqlsession.close();
        }
         return outListMap;
    }
    /*
     * 插入单条数据(此方式没有事务处理)
     *@param sqlid
     *@param inMap
     *@return
     *@throws Exception
     *作者:刘春元
     *2013-5-17
     */
    public int insertOne(String sqlid,Map<String, String> inMap) throws ExceptionAbstract{
         int flag = 0;
         Transaction newTransaction = null;
         SqlSession sqlsession = null;
         try {
            TransactionFactory transactionFactory = new JdbcTransactionFactory();  
              sqlsession = sqlSessionFactory.openSession();
             newTransaction = transactionFactory.newTransaction(sqlsession.getConnection());  
             flag = sqlsession.insert(sqlid,inMap);
         }catch (Exception e) {
                try {
                    newTransaction.rollback();
                    throw new SystemException(ErrorConstant.SYSTEM_A04,ErrorConstant.SYSTEM_A04_DESC);
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }finally {
                   try {
                    newTransaction.close();
                    sqlsession.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
              }
         return flag;
}
    public int updateOne(String sqlid,Map<String, String> inMap) throws ExceptionAbstract{
         int flag = 0;
         Transaction newTransaction = null;
         SqlSession sqlsession = null;
         try {
            TransactionFactory transactionFactory = new JdbcTransactionFactory();  
              sqlsession = sqlSessionFactory.openSession();
             newTransaction = transactionFactory.newTransaction(sqlsession.getConnection());  
             flag = sqlsession.update(sqlid,inMap);
         }catch (Exception e) {
                try {
                    newTransaction.rollback();
                    throw new SystemException(ErrorConstant.SYSTEM_A05,ErrorConstant.SYSTEM_A05_DESC);
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }finally {
                   try {
                    newTransaction.close();
                    sqlsession.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
              }
         return flag;
}
    public int deleteOne(String sqlid,Map<String, String> inMap) throws ExceptionAbstract{
         int flag = -1;
         Transaction newTransaction = null;
         SqlSession sqlsession = null;
         try {
            TransactionFactory transactionFactory = new JdbcTransactionFactory();  
              sqlsession = sqlSessionFactory.openSession();
             newTransaction = transactionFactory.newTransaction(sqlsession.getConnection());  
             flag = sqlsession.delete(sqlid,inMap);
         }catch (Exception e) {
                try {
                    newTransaction.rollback();
                    throw new SystemException(ErrorConstant.SYSTEM_A06,ErrorConstant.SYSTEM_A06_DESC);
                } catch (SQLException e1) {
                    throw new SystemException(ErrorConstant.SYSTEM_A07,ErrorConstant.SYSTEM_A07_DESC);
                }
            }finally {
                   try {
                    newTransaction.close();
                    sqlsession.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
              }
         return flag;
}
    /*
     * db全事务处理用于新增,修改,删除遵循CURD原则性
     *@param objectMap(key:sqlId,sqlType,addMap)
     *@return
     *作者:刘春元
     *2013-5-19
     */
    @SuppressWarnings("unchecked")
    public int transactionAll(List<Map<String, Object>> objectListMap)throws ExceptionAbstract{
         int flag = 0;
         Transaction newTransaction = null;
         SqlSession sqlsession = null;
         try {
            TransactionFactory transactionFactory = new JdbcTransactionFactory();  
              sqlsession = sqlSessionFactory.openSession();
             newTransaction = transactionFactory.newTransaction(sqlsession.getConnection());  
                for (Map<String, Object> listMap : objectListMap) {
                    String idObject= (String) listMap.get("sqlId");
                    String typeObject= (String) listMap.get("sqlType");
                    Map<String, Object> mapObject= (Map<String, Object>) listMap.get("dataMap");
                    if(null == idObject || null == typeObject || null == mapObject){
                        new SystemException(ErrorConstant.SYSTEM_A09, ErrorConstant.SYSTEM_A09_DESC);
                    }else{
                        if(SysConstant.C.equals(typeObject)){
                            flag = sqlsession.insert(idObject, mapObject);
                        }else if(SysConstant.U.equals(typeObject)){
                            flag = sqlsession.update(idObject, mapObject);
                        }else if(SysConstant.D.equals(typeObject)){
                             flag = sqlsession.delete(idObject, mapObject);
                        }
                    }
                }
         }catch (Exception e) {
                try {
                    flag = 0;
                    newTransaction.rollback();
                    //throw new SystemException(ErrorConstant.SYSTEM_A07,ErrorConstant.SYSTEM_A07_DESC);
                    return flag;
                } catch (SQLException e1) {
                    throw new SystemException(ErrorConstant.SYSTEM_A07,ErrorConstant.SYSTEM_A07_DESC);
                }
            }finally {
                   try {
                    newTransaction.close();
                     sqlsession.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
              }
         return flag;
    }
    public String selectSequences(String sequences) throws ExceptionAbstract{
        String str = "";
        SqlSession sqlsession = sqlSessionFactory.openSession();
        try{
            //str = (String) this.selectOneString(sequences,null);
            str = sqlsession.selectOne("Sql_sequences", sequences);
        }catch (Exception e) {
            throw new SystemException(ErrorConstant.SYSTEM_A08,ErrorConstant.SYSTEM_A08_DESC);
        }finally{
            sqlsession.close();
        }
        return str;
    }
}
初始化

 

posted @ 2013-11-28 16:14  琦琦狐  阅读(729)  评论(0编辑  收藏  举报