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

com.yuyenews.easy.base.ExBaseDao Maven / Gradle / Ivy

The newest version!
package com.yuyenews.easy.base;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yuyenews.easy.util.entity.EntityUtil;

/**
 * 扩展BaseDao
 * 
 * @author yuye
 *
 */
public class ExBaseDao {

	private Logger logger = LoggerFactory.getLogger(ExBaseDao.class);

	/**
	 * 查询数据列表
	 * @param connection lianjie
	 * @param sql sql
	 * @param args canshu
	 * @return list duix
	 */
	public List> selectList(Connection connection, String sql, Object[] args) {
		try {

			ResultSet resultSet = BaseDao.select(connection, sql, args);

			List> list = new ArrayList<>();

			ResultSetMetaData rsmd = resultSet.getMetaData();

			while (resultSet.next()) {

				Map map = new HashMap<>();
				for (int i = 1; i <= rsmd.getColumnCount(); i++) {
					map.put(rsmd.getColumnName(i), resultSet.getObject(rsmd.getColumnName(i)));
				}

				list.add(map);
			}

			return list;
		} catch (Exception e) {
			logger.error("查询数据失败", e);
		}

		return null;
	}

	/**
	 * 查询多条数据
	 * 
	 * @param sql sql
	 * @param args canshu
	 * @return list duix
	 */
	@SuppressWarnings("unchecked")
	public  List selectList(Connection connection, String sql, Object[] args, Class cls) {
		try {

			ResultSet resultSet = BaseDao.select(connection, sql, args);

			List list = new ArrayList<>();

			while (resultSet.next()) {
				Object obj = EntityUtil.getEntity(cls, resultSet);
				list.add((T) obj);
			}

			return list;
		} catch (Exception e) {
			logger.error("查询数据失败", e);
		}

		return null;
	}

	/**
	 * 查询一条数据
	 * 
	 * @param sql
	 * @param args
	 * @return 泛型
	 */
	public  T selectOne(Connection connection, String sql, Object[] args, Class cls) {
		try {

			List list = selectList(connection, sql, args, cls);

			if (list != null && list.size() == 1) {
				return list.get(0);
			} else if (list != null && list.size() > 1) {
				throw new Exception("查询出来的数据超过一条");
			} else {
				return null;
			}

		} catch (Exception e) {
			logger.error("查询数据失败", e);
		}

		return null;
	}
	
	/**
	 * 查询一条数据
	 * 
	 * @param sql
	 * @param args
	 * @return map
	 */
	public Map selectOne(Connection connection, String sql, Object[] args) {
		try {

			List> list = selectList(connection, sql, args);

			if (list != null && list.size() == 1) {
				return list.get(0);
			} else if (list != null && list.size() > 1) {
				throw new Exception("查询出来的数据超过一条");
			} else {
				return null;
			}

		} catch (Exception e) {
			logger.error("查询数据失败", e);
		}

		return null;
	}

	/**
	 * 增删改数据
	 * 
	 * @param connection
	 * @param sql
	 * @return
	 */
	public int update(Connection connection, String sql, Object[] args) {
		try {
			return BaseDao.update(connection, sql, args);
		} catch (SQLException e) {
			logger.error("更新数据失败", e);
		}
		return -1;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy