首页 > 数据库技术 > 详细

JDBC的业务逻辑的应用

时间:2020-09-15 12:42:59      阅读:65      评论:0      收藏:0      [点我收藏+]

文件的定义规范:

技术分享图片

 

 

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的业务逻辑的应用

原文:https://www.cnblogs.com/Polar-sunshine/p/13672245.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!