All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mars.mj.helper.JdbcTemplete Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
package com.mars.mj.helper;

import com.mars.core.constant.MarsSpace;
import com.mars.core.util.ThreadUtil;
import com.mars.mj.manager.ConnectionManager;
import com.mars.mj.util.DataCheckUtil;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * jdbc模板
 */
public class JdbcTemplete {

    private static MarsSpace marsSpace = MarsSpace.getEasySpace();

    private String dataSourceName;

    private JdbcTemplete() {
    }

    /**
     * 获取JdbcTemplete对象
     *
     * @return
     */
    public static JdbcTemplete get() {
        JdbcTemplete jdbcTemplete = new JdbcTemplete();
        jdbcTemplete.dataSourceName = jdbcTemplete.getDataSourceName(null);
        return jdbcTemplete;
    }

    /**
     * 获取JdbcTemplete对象
     *
     * @param dataSourceName
     * @return
     */
    public static JdbcTemplete get(String dataSourceName) {
        JdbcTemplete jdbcTemplete = new JdbcTemplete();
        jdbcTemplete.dataSourceName = jdbcTemplete.getDataSourceName(dataSourceName);
        return jdbcTemplete;
    }

    /**
     * 查询多条数据
     *
     * @param sql
     * @param param
     * @return
     * @throws Exception
     */
    public List> selectList(String sql, Object param) throws Exception {
        DataCheckUtil.isNull(param,"传参不可以为null");

        ConnectionManager connectionManager = getConnection();
        try {
            List> result = select(sql, param, connectionManager.getConnection());
            return result;
        } catch (Exception e) {
            throw e;
        } finally {
            connectionManager.close();
        }
    }

    /**
     * 查询多条数据
     *
     * @param sql
     * @return
     * @throws Exception
     */
    public List> selectList(String sql) throws Exception {
        ConnectionManager connectionManager = getConnection();
        try {
            List> result = select(sql, null, connectionManager.getConnection());
            return result;
        } catch (Exception e) {
            throw e;
        } finally {
            connectionManager.close();
        }
    }

    /**
     * 查询一条数据
     *
     * @param sql
     * @param param
     * @return
     * @throws Exception
     */
    public Map selectOne(String sql, Object param) throws Exception {
        DataCheckUtil.isNull(param,"传参不可以为null");

        ConnectionManager connectionManager = getConnection();
        try {
            List> mapList = select(sql, param, connectionManager.getConnection());
            if (mapList != null && mapList.size() == 1) {
                return mapList.get(0);
            } else if (mapList.size() > 1) {
                throw new Exception("查出来的数据不止一条");
            }
        } catch (Exception e) {
            throw e;
        } finally {
            connectionManager.close();
        }
        return null;
    }

    /**
     * 查询一条数据
     *
     * @param sql
     * @return
     * @throws Exception
     */
    public Map selectOne(String sql) throws Exception {
        ConnectionManager connectionManager = getConnection();
        try {
            List> mapList = select(sql, null, connectionManager.getConnection());
            if (mapList != null && mapList.size() == 1) {
                return mapList.get(0);
            } else if (mapList.size() > 1) {
                throw new Exception("查出来的数据不止一条");
            }
        } catch (Exception e) {
            throw e;
        } finally {
            connectionManager.close();
        }
        return null;
    }

    /**
     * 增删改
     *
     * @param sql
     * @param param
     * @return
     * @throws Exception
     */
    public int update(String sql, Object param) throws Exception {
        DataCheckUtil.isNull(param,"传参不可以为null");

        ConnectionManager connectionManager = getConnection();
        try {
            if (param instanceof Object[]) {
                Object[] params = (Object[]) param;
                return DBHelper.update(sql, connectionManager.getConnection(), params);
            } else {
                return DBHelper.update(builderSql(sql, param), connectionManager.getConnection(), null);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            connectionManager.close();
        }
    }

    /**
     * 增删改
     *
     * @param sql
     * @return
     * @throws Exception
     */
    public int update(String sql) throws Exception {
        ConnectionManager connectionManager = getConnection();
        try {
            int result = DBHelper.update(sql, connectionManager.getConnection());
            return result;
        } catch (Exception e) {
            throw e;
        } finally {
            connectionManager.close();
        }
    }

    /**
     * 获取数据库连接
     *
     * @return
     * @throws Exception
     */
    private ConnectionManager getConnection() throws Exception {
        ConnectionManager connectionManager = new ConnectionManager();

        /* 获取当前线程中的Connection */
        Object obj = marsSpace.getAttr(ThreadUtil.getThreadIdToTraction());

        /* 数据库连接 */
        Connection connection = null;

        /* 获取数据源名称 */
        String dataSourceName2 = getDataSourceName(dataSourceName);

        if (obj != null) {
            Map connections = (Map) obj;
            connection = connections.get(dataSourceName2);
            connectionManager.setHasTrantion(false);
        } else {
            connection = DBHelper.getConnection(dataSourceName2);
            connectionManager.setHasTrantion(true);
        }
        connectionManager.setConnection(connection);
        return connectionManager;
    }

    /**
     * 查询
     *
     * @param args
     * @param connection
     * @return
     * @throws Exception
     */
    private List> select(String sql, Object args, Connection connection) throws Exception {
        List> result = null;
        if (args != null) {
            if (args instanceof Object[]) {
                Object[] params = (Object[]) args;
                result = DBHelper.selectList(sql, connection, params);
            } else {
                result = DBHelper.selectList(builderSql(sql, args), connection, null);
            }
        } else {
            result = DBHelper.selectList(sql, connection);
        }
        return result;
    }

    /**
     * 构建sql语句
     *
     * @param sql
     * @param args
     * @return
     * @throws Exception
     */
    private String builderSql(String sql, Object args) throws Exception {

        Class cls = args.getClass();

        /* 替换sql中的占位符 */
        Field[] fields = cls.getDeclaredFields();
        for (Field f : fields) {
            f.setAccessible(true);
            String pre = "{" + f.getName() + "}";
            if (sql.indexOf(pre) > -1) {
                String ftname = f.getType().getName();
                if(ftname.equals(String.class.getName()) || ftname.equals(Character.class.getName())){
                    sql = sql.replace(pre, "'"+f.get(args).toString()+"'");
                } else {
                    sql = sql.replace(pre, f.get(args).toString());
                }
            }
        }

        return sql;
    }

    /**
     * 获取数据源名称
     *
     * @return str
     */
    private String getDataSourceName(String dataSourceName) {
        if (dataSourceName == null) {
            dataSourceName = marsSpace.getAttr("defaultDataSource").toString();
        }
        return dataSourceName;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy