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运行结果