本次博客带领大家学习BasicDao的使用。
BasicDao的基本说明
- DAO:data access object数据访问对象。
- 这样的通用类,称为BasicDao,是专门和数据库交互的,即完成对数据库(表)的crud操作。
- 在BasicDao 的基础上,实现一张表对应一个Dao,更好的完成功能,比如Customer表-Customer.java类(javabean)-Customer.java。
BasicDAO 应用实例
- dao_.utils //工具类
public class JDBCUtilsByDruid {
private static DataSource ds;
//在静态代码块完成 ds初始化
static {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src\\druid.properties"));
ds = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
//编写getConnection 方法
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
//关闭连接,在数据库连接池技术中,close不是真的断掉连接,而是把使用的Connection对象放回连接池
public static void close(ResultSet resultSet, Statement statement,Connection connection){
try {
if (resultSet != null){
resultSet.close();
}
if (statement !=null){
statement.close();
}
if (connection !=null){
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
- dao_.domain //javabean
public class Actor {
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 "\nActor{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", borndate=" + borndate +
", phone='" + phone + '\'' +
'}';
}
}
- dao_.dao //存放XxxDAO 和 BasicDAO
public class BasicDAO<T> { //泛型指定具体类型
private QueryRunner qr=new QueryRunner();
//开发通用的dml方法,针对任意的表
public int update(String sql,Object... paramenters){
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
int update = qr.update(connection, sql, paramenters);
return update;
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
JDBCUtilsByDruid.close(null,null,connection);
}
}
//返回多个对象(即查询的结果是多行),针对任意表
public List<T> queryMulti(String sql,Class<T> clazz,Object... paramenters){
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qr.query(connection, sql, new BeanListHandler<T>(clazz),paramenters);
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
JDBCUtilsByDruid.close(null,null,connection);
}
}
//查询单行结果的通用方法
public T querySingle(String sql,Class<T> clazz,Object... paramenters){
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qr.query(connection, sql, new BeanHandler<T>(clazz),paramenters);
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
JDBCUtilsByDruid.close(null,null,connection);
}
}
//查询单行单列的方法,即返回单值的方法
public Object queryScalar(String sql,Object... paramenters){
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qr.query(connection, sql, new ScalarHandler(),paramenters);
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
JDBCUtilsByDruid.close(null,null,connection);
}
}
}
public class ActorDAO extends BasicDAO<Actor> {
//1.就有 BasicDAO 的方法
//2.根据业务需求,可以编写特有的方法。
}
- dao_.test //写测试类
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, 1);
System.out.println("====查询结果===");
for (Actor actor : actors){
System.out.println(actor);
}
//2. 查询单行记录
Actor actor = actorDAO.querySingle("select * from actor where id =?", Actor.class, 6);
System.out.println("====查询单行结果====");
System.out.println(actor);
//3.查询单行单列
Object o = actorDAO.queryScalar("select name from actor where id = ?", 6);
System.out.println("=====查询单行单列值====");
System.out.println(o);
//4.dml操作 insert,update,delete
int update = actorDAO.update("insert into actor values(null,?,?,?,?)", "张无忌", "男", "2000-11-11", "999");
System.out.println(update >0 ? "执行成功" : "执行没有影响结果");
}
}