MySQL_JDBC_DAO复习

 

view层

service层

dao层

bean实体类层

sql层

 

dao层属于处理sql语句的java类层次,通过处理sql语句实现ORM思想,数据库表字段到java实体类中的映射(此处小白自述,往不错误之处请大家见谅,还请大家能批评指正)

 

 

一、数据库层面

创建account数据表

                                                             

 

二、java层面

Account实体类

package bean;

public class Account {

	private int id;
	private String username;
	private double balance;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public Account(int id, String username, double balance) {
		super();
		this.id = id;
		this.username = username;
		this.balance = balance;
	}

	public Account() {
		super();
	}

	@Override
	public String toString() {
		return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]";
	}

}

  BasicDao

package dao;

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 utils.JDBCUtils;

/**
 * 封装一个通用的BasicDao
 * 
 * @author Administrator
 * 功能:
 * 1、增删改(所有的表都通用)
 * 2、获取单条记录(所有的表都通用)
 * 3、获取多条记录(所有的表都通用)
 * 4、获取单个值(所有的表都通用)
 */
public class BasicDao<T> {

	QueryRunner qr;
	{
		qr = new QueryRunner();
	}
	
	//增删改
	public int update(String sql,Object ... objects){
		Connection connection = null;
		try {
			connection = JDBCUtils.getConnection();
			return qr.update(connection, sql, objects);
		} catch (Exception e) {
			throw new RuntimeException();
		} finally{
			JDBCUtils.close(null, null, connection);
		}
	}
	
	//查询返回单条记录
	public T SearchOne(String sql,Class clazz,Object ... objects){
		Connection connection = null;
		try {
			connection = JDBCUtils.getConnection();
			return qr.query(connection, sql, new BeanHandler<T>(clazz), objects);
		} catch (Exception e) {
			throw new RuntimeException();
		} finally{
			JDBCUtils.close(null, null, connection);
		}
	}
	
	//查询返回多条记录
	public List<T> SearchMore(String sql,Class clazz,Object ... objects){
		Connection connection = null;
		try {
			connection = JDBCUtils.getConnection();
			return qr.query(connection, sql, new BeanListHandler<T>(clazz), objects);
		} catch (Exception e) {
			throw new RuntimeException();
		} finally{
			JDBCUtils.close(null, null, connection);
		}
	}
	
	//查询返回单值
	public Object Scalar(String sql,Class clazz,Object ... objects){
		Connection connection = null;
		try {
			connection = JDBCUtils.getConnection();
			return qr.query(connection, sql, new ScalarHandler(), objects);
		} catch (Exception e) {
			throw new RuntimeException();
		} finally{
			JDBCUtils.close(null, null, connection);
		}
	}
}

 AccountDao

package dao;

import bean.Account;

public class AccountDao extends BasicDao<Account>{

}

 

 TestAccountDao

package demo.connection4;

import java.util.List;

import org.junit.Test;

import bean.Account;
import dao.AccountDao;

public class TestAccountDao {
	
	@Test
	public void test1(){
		AccountDao dao = new AccountDao();
		List<Account> accounts = dao.SearchMore("select * from account", Account.class);
		for (Account account : accounts) {
			System.out.println(account);
		}
	}
}

 

 TestAccountDao运行结果

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值