您的位置:首页 > 其它

课时8:DAO设计模式

2015-11-05 15:36 393 查看
1.DAO简介
DAO的全称是:Data Access Object,数据访问对象。
使用DAO设计模式,来封装数据库持久的的所有操作(CRUD),使低级的数据逻辑和高级的业务逻辑分离,达到解耦合的目的。
2.一个典型的DAO实现有如下的组件:
一个DAO接口
数据传输对象(有时称为值对象)
一个实现了DAO接口的具体类
一个DAO工厂类
3.以维护一个客户信息为例,具体组件如下所示:
CustomerDao 解耦
Customer 值对象(VO)
CustomerDaoImpl(接口的具体实现类)
CustomerFactory(工厂类,实例化用)
4.实例
Customer Bean类

1.建立一个类class Customer,里面存放所有数据库内成员的信息
2.建立一个接口interface CustomerDao,里面存放对数据库操作的所有方法
3.建立一个类class CustomerDaoImpl implements CustomerDao,并且实现CustomerDao里面的所有方法
4.建立一个类class Test6,对数据库进行操作



package com.geek99.demo;
//这张表是数据库里的内容一一对应
public class Customer {

private int id;
private String name;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

public String toString(){
return id+":"+name+":"+email;
}

}



package com.geek99.demo;

import java.util.List;

public interface CustomerDao {
public void add(Customer c);
public void update(Customer c);
public void delete(int id);
public Customer getCustomerById(int id);
public List<Customer> query();
//CRUD create retrieval update delete
}



package com.geek99.demo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

//Customer javabean
//CustomerDao
//CustomerDaoImpl

public class CustomerDaoImpl implements CustomerDao{

public void add(Customer c) {
String sql="insert into CustomerTbl(name,email)values(?,?)";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,c.getName());
pstmt.setString(2, c.getEmail());

pstmt.executeUpdate();

} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}

}

public void update(Customer c) {
String sql="update CustomerTbl set name=?,email=? where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);

pstmt.setString(1,c.getName());
pstmt.setString(2, c.getEmail());
pstmt.setInt(3, c.getId());

pstmt.executeUpdate();

} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}

}

public void delete(int id) {
String sql="delete from CustomerTbl where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();

} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}

public Customer getCustomerById(int id) {
String sql="select id,name,email from CustomerTbl where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
String name=rs.getString(1);
String email=rs.getString(2);
Customer c=new Customer();
c.setId(id);
c.setName(name);
c.setEmail(email);
return c;
}

} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
return null;
}

public List<Customer> query() {
String sql="select id,name,email from CustomerTbl";
Connection conn=DBUtil.open();
try {
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
List<Customer> list=new ArrayList<Customer>();
while(rs.next()){
int id=rs.getInt(1);
String name=rs.getString(2);
String email=rs.getString(3);
Customer c=new Customer();
c.setId(id);
c.setName(name);
c.setEmail(email);
list.add(c);
}
return list;

} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
return null;
}

}



package com.geek99.demo;

import java.util.List;

public class Test6 {
public static void main(String[] args) {
CustomerDao dao=new CustomerDaoImpl();
// Customer c=new Customer();
// c.setName("geek");
// c.setEmail("geek@126.com");
// dao.add(c);
// c.setId(11);
// c.setName("newgeek");
// c.setEmail("newgeek@111.com");
// dao.update(c);
// dao.delete(10);
// List<Customer> list=dao.query();
// System.out.println(list);
Customer c=dao.getCustomerById(11);
System.out.println(c);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: