使用JdbcUtils框架实现Dao 详解~

Dao包含 5个方法

interface Dao<F>(){

}

1.更新数据库 

Connection 为数据库链接对象,sql为数据库执行语句,args为 填充参数

void update(Connection connection ,String sql, Object ... args);

2.获取数据库结果集

List<F> getForList(Connection connection, String sql ,Object ... args);

3.获取数据库的查询的第一个对象

F getForList(Connection connection ,String sql,Object ... args);

Dao代码

package cn.lw.mydao;

import java.sql.Connection;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.util.List;

public interface DAO<T> {
	/**
	 * 批量处理方法
	 * @param connection
	 * @param sql
	 * @param args
	 * @throws SQLException 
	 */
	void batch(Connection connection , String sql,Object[] ...args) throws SQLException;
	/**
	 * 返回数据库库中的的一个记录。例如 一个人的姓名,一个人的成绩。
	 * @param connection
	 * @param sql
	 * @param args
	 * @return
	 * @throws SQLException 
	 * @throws Exception 
	 */
	<F> F getForValue(Connection connection,String sql,Object ...args) throws SQLException, Exception;
	 /**
	  * 返回 T 的一个集合
	 * @param connection
	 * @param sql
	 * @param args
	 * @return
	 * @throws SQLException 
	 */
	List<T> getForList(Connection connection ,String sql, Object...args) throws SQLException;
	
	 /**
	  * 返回一行记录所对应的那个对象
	 * @param connection 
	 * @param sql
	 * @param args
	 * @return
	 * @throws SQLException 
	 */
	T get(Connection connection,String sql,Object ... args) throws SQLException;
	/**
	 * 更新操作,可以包含的sql语句有 增删改
	 * @param connection  数据库链接
	 * @param sql	      要执行的sql语句
	 * @param args	    要填充的占位符
	 * @throws SQLException 
	 */
	void update(Connection connection ,String sql,Object ... args) throws SQLException;
	
}

2.DaoImpl代码;这个类一般用对应数据库表文件的那个类来继承此类

package cn.lw.mydao;

import java.awt.Window.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

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.ScalarHandler;

import cn.lw.jdbc.User;

public class JdbcDaoImpl <T> implements DAO<T> {
	private  QueryRunner queryRunner=null;
	private  Class<T> type=null;
	 public JdbcDaoImpl() {
		// TODO Auto-generated constructor stub
		 queryRunner =new QueryRunner();
		 type=ReflectionUtils.getSuperGenericType(this.getClass());//通过反射的方式获取父亲类指定泛型类的泛型用于JdbcUtils的Query()ResultSetHandler构造器的参数。这样就能返回 出对应的对象了
	}

	@Override
	public void batch(Connection connection, String sql, Object[]... args) throws SQLException {
		// TODO Auto-generated method stub
		queryRunner.batch(connection, sql, args);
		
	}

	@Override
	public <F> F getForValue(Connection connection, String sql, Object... args) throws Exception {
		// TODO Auto-generated method stub
		return (F) queryRunner.query(connection, sql, new ScalarHandler(), args);
	}

	@Override
	public List<T> getForList(Connection connection, String sql, Object... args) throws SQLException {
		// TODO Auto-generated method stub
		return queryRunner.query(connection, sql, new BeanListHandler<>(type),args);
	}

	@Override
	public T get(Connection connection, String sql, Object... args) throws SQLException {
		// TODO Auto-generated method stub
		return queryRunner.query(connection, sql, new BeanHandler<>(type), args
				);
	}

	@Override
	public void update(Connection connection, String sql, Object... args) throws SQLException {
		// TODO Auto-generated method stub
		queryRunner.update(connection, sql, args);
	}

}

3.UserDao代码

package cn.lw.mydao;

import cn.lw.jdbc.User;

public class UserDao extends JdbcDaoImpl<User>{

}

4测试代码

package cn.lw.mydao;

import static org.junit.jupiter.api.Assertions.*;

import java.sql.Connection;
import java.sql.SQLException;

import org.junit.jupiter.api.Test;

import cn.lw.dao.JdbcUtil;
import cn.lw.jdbc.User;

class TestUserDao {
	UserDao userdao=new UserDao();
	@Test
	void testJdbcDaoImpl() {
		fail("Not yet implemented");
	}

	@Test
	void testBatch() {
		fail("Not yet implemented");
	}

	@Test
	void testGetForValue() {
		fail("Not yet implemented");
	}

	@Test
	void testGetForList() {
		fail("Not yet implemented");
	}

	@Test
	void testGet() {
		Connection connection=JdbcUtil.getConnection();
		String sql ="select uname name ,password from user";
		try {
			User user=userdao.get(connection, sql);
			System.out.println();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	void testUpdate() {
		
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值