JDBC的业务逻辑的应用

文件的定义规范:

JDBC的业务逻辑的应用

 

 

Dao.java文件内容:

package com.sk.jdbc.dao;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.sk.jdbc.util.ConnectionUtil;

//Dao类中存放通用的数据访问方法
public class Dao<T> {

	private Class<T> clazz;
	private QueryRunner queryRunner = new QueryRunner();

	public Connection conn = null;

	@SuppressWarnings("unchecked")
	public Dao() {
		Type type = this.getClass().getGenericSuperclass(); //
		if (type instanceof ParameterizedType) {
			ParameterizedType parameterizedType = (ParameterizedType) type;
			Type[] types = parameterizedType.getActualTypeArguments();
			if (types != null && types.length > 0) {
				if (types[0] instanceof Class) {
					clazz = (Class<T>) types[0];
				}
			}
		}
	}

	public void openConnection() throws SQLException {
		if (this.conn == null  || this.conn.isClosed()) {
			this.conn = ConnectionUtil.getConnection();
		}
	}

	public void closeConnection() throws SQLException {
		if (this.conn != null &&  !this.conn.isClosed() ) {
			ConnectionUtil.release(this.conn);
		}
	}

	public void beginTransaction() throws SQLException {
			openConnection();
			this.conn.setAutoCommit(false);
	}

	public void commit() throws SQLException {
		if (this.conn != null) {
			this.conn.commit();
		}
	}

	public void rollback() throws SQLException {
		if (this.conn != null) {
			this.conn.rollback();
		}
	}

	// 通用的update()方法:
	public void update(String sql, Object... args) throws SQLException {
		openConnection();
		queryRunner.update(conn, sql, args);
	}

	// 通用的查询方法----查单个记录
	public T queryOne(String sql, Object... args) throws SQLException {

		T entity = null;
		openConnection();
		entity = queryRunner.query(conn, sql, new BeanHandler<>(clazz), args);
		return entity;

	}

	// 通用的查询方法----查多个记录
	public List<T> queryList(String sql, Object... args) throws SQLException {

		List<T> list = null;
		openConnection();
		list = queryRunner.query(conn, sql, new BeanListHandler<>(clazz), args);
		return list;

	}

	// 查单值
	public Object queryValue(String sql, Object... args) throws SQLException {

		Object result = null;
		openConnection();
		result = queryRunner.query(conn, sql, new ScalarHandler<>(), args);
		return result;
	}

	public Map<String, Object> queryMap(String sql, Object... args)
			throws SQLException {
		Map<String, Object> phoneMap = null;
		openConnection();
		phoneMap = queryRunner.query(conn, sql, new MapHandler(), args);
		return phoneMap;

	}

	public List<Map<String, Object>> queryMapList(String sql, Object... args)
			throws SQLException {
		List<Map<String, Object>> phoneMapList = null;
		openConnection();
		phoneMapList = queryRunner.query(conn, sql, new MapListHandler(), args);
		return phoneMapList;

	}

}

  

 

JDBC的业务逻辑的应用

上一篇:GoAccess 监控工具


下一篇:Sqlite+Ef6错误