一、BasicDAO的介绍 二、BasicDAO的示意图 三、完成BasicDAO的简单实例
1、创建BasicDAO类
//开发BasicDAO,是其他DAO的父类
public class BasicDAO<T> {//泛型指定具体的类型
private QueryRunner qr = new QueryRunner();
//开发通用的DML方法,针对任意的表
public int update(String sql, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
int update = qr.update(connection, sql, parameters);
return update;
} catch (SQLException e) {
//将编译异常转换为运行异常,抛出
throw new RuntimeException(e);
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//返回多个对象(即查询的结果是多行),针对任意表
/**
* @param sql sql语句可以有?号
* @param clazz 传入一个类的Class对象 比如Actor.class
* @param parameters 传入? 的具体的值,可以是多个
* @return 根据Actor.class 返回对应的ArrayList集合
*/
public List<T> queryMulti(String sql, Class<T> clazz, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
} catch (SQLException e) {
//将编译异常转换为运行异常,抛出
throw new RuntimeException(e);
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//查询单行结果的通用方法
public T querySingle(String sql, Class<T> clazz, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);
} catch (SQLException e) {
//将编译异常转换为运行异常,抛出
throw new RuntimeException(e);
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//查询单行单列的方法,即返回单值的方法
public Object queryScalar(String sql, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qr.query(connection, sql, new ScalarHandler(), parameters);
} catch (SQLException e) {
//将编译异常转换为运行异常,抛出
throw new RuntimeException(e);
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
}
2、创建ActorDAO类继承BasicDAO类
public class ActorDAO extends BasicDAO<Actor>{
//1.就有BasicDao的方法
//2.根据业务需求,可以编写特有的方法
}
3、创建domain,Actor类
//Actor对象 和 actor表的记录对应
public class Actor {//JavaBean ,POJO,Domain对象
private Integer id;
private String name;
private String sex;
private Date borndate;
private String phone;
public Actor() {//一定要给一个无参构造器[反射需要]
}
public Actor(Integer id, String name, String sex, Date borndate, String phone) {
this.id = id;
this.name = name;
this.sex = sex;
this.borndate = borndate;
this.phone = phone;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBorndate() {
return borndate;
}
public void setBorndate(Date borndate) {
this.borndate = borndate;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Actor{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", borndate=" + borndate +
", phone='" + phone + '\'' +
'}';
}
}
4、创建测试类TestDAO
public class TestDAO {
//测试ActorDAO 对Actor表的crud操作
@Test
public void testActorDAO() {
ActorDAO actorDAO = new ActorDAO();
//1.查询多行记录
List<Actor> actors = actorDAO.queryMulti("SELECT * FROM actor WHERE id >= ?", Actor.class, 2);
System.out.println("===查询结果===");
for (Actor actor : actors) {
System.out.println(actor);
}
//查询单行记录
Actor actor = actorDAO.querySingle("SELECT * FROM actor WHERE id = ?", Actor.class, 2);
System.out.println("===查询单行结果===");
System.out.println(actor);
//查询单行单例
Object o = actorDAO.queryScalar("SELECT name FROM actor WHERE id = ?", 2);
System.out.println("===查询单行单列===");
System.out.println(o);
//DML操作 (insert,delete,update)
// int affectedRows = actorDAO.update("UPDATE actor SET NAME=? WHERE id = ?", "林青霞", 2);
// int affectedRows = actorDAO.update("INSERT INTO actor VALUES(NULL,?,?,?,?)", "许三多", "男","1999-01-01","123456");
int affectedRows = actorDAO.update("DELETE FROM actor WHERE id = ?", "5");
System.out.println(affectedRows > 0 ? "执行成功" : "执行没有影响表");
}
}
输出结果如下
===查询结果===
Actor{id=2, name='林青霞', sex='男', borndate=1980-01-03 08:00:00.0, phone='123456789'}
Actor{id=3, name='张三丰', sex='男', borndate=1999-01-10 08:00:00.0, phone='1234567890'}
Actor{id=5, name='许三多', sex='男', borndate=1999-01-01 08:00:00.0, phone='123456'}
===查询单行结果===
Actor{id=2, name='林青霞', sex='男', borndate=1980-01-03 08:00:00.0, phone='123456789'}
===查询单行单列===
林青霞
执行成功
JDBCUtilsByDruid工具类的封装,看这篇文章传送门
Java 数据库连接池C3P0,德鲁伊(Druid)的详解 里面有对JDBCUtilsByDruid工具类的封装。
|