DAO设计模式实际上是两个模式的组合,即Data Accessor模式和Active Domain Object模式。其中Data Accessor模式实现了数据访问和业务逻辑的分离,即将数据访问的实现机制加以封装,与数据的使用代码相分离,从外部来看,提供黑盒式的数据存取接口。而Active Domain Obejct模式实现了业务数据的对象化封装,提供了对所面向领域的对象的封装。
DAO模式通过对业务层提供数据抽象层接口,实现了以下几个目标:数据存储逻辑的分离;数据访问底层实现的分离;资源管理和调度的分离;数据抽象。
DAO模式的实现:
(1)创建一个抽象工厂类,它包含两个重要的部分:第一部分是一些抽象方法,这些抽象方法是所有实现该抽象工厂的具体工厂类所必须的
(2)然后,分别创建各个类型数据源的工厂类,在这个工厂类中里面也有两个重要组成部分
(3)定义具体DAO类的接口,并在接口中定义所有的业务方法,和数据操作方法。
(4)定义具体的DAO类,在这个类中才是实际的业务方法,和数据操作的实现。
(5)定义数据传输对象,它是用来在客户端和DAO之间传递数据的,它其实就是javabean
(6)完成以上5步后我们就可以在数据客户端使用以上由DAO设计模式定义好的各个类了。
class User {
private String name;
private int age;
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
}
interface UserDAO {
public boolean doCreate(User user)throws Exception;
}
class UserDAOImpl implements UserDAO {
private String str=null;
public UserDAOImpl(String str){
this.str=str;
}
public boolean doCreate(User user)throws Exception{
boolean flag=false;
try{
System.out.println("真实主题类的数据库增加操作");
}catch(Exception e){
e.printStackTrace();
}finally{
}
return flag;
}
}
class UserDAOProxy implements UserDAO {
private UserDAOImpl dao=null;
private String str=null; //此处其实应该传入一个数据库的连接到真实主题类的,不过这里只是个演示,所有以字符串的形式传了进去
public UserDAOProxy(){
this.str=new String();
this.dao=new UserDAOImpl(this.str);
}
public boolean doCreate(User user)throws Exception{
boolean flag=true;
System.out.println("代理类的操作,打开数据库,同时取得真实主题类的实例去调用真实的数据层操作");
try{
this.dao.doCreate(user);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("代理类的操作,调用完真实主题类的操作,同时关闭数据库");
}
return flag;
}
}
class UserDAOFactory {
public static UserDAO getUserDAONewInstance(){
System.out.println("通过工厂类取得代理类的实例去调用代理类的操作。");
return new UserDAOProxy();
}
}
public class FactoryDemo {
public static void main(String args[])throws Exception{
User user=new User();
user.setName("jhjjs");
user.setAge(20);
UserDAOFactory.getUserDAONewInstance().doCreate(user);
}
}
lz看下吧,刚做的
DAO (Data Access Objects) 建议您还是先看下
package
model;
import
javax.persistence.*;
import
java.io.Serializable;
import
java.util.Date;
import
java.util.Calendar;
import
java.util.GregorianCalendar;
/**
* Created by IntelliJ IDEA.
* User: Michael
* Date: Sep 13, 2006
* Time: 5:14:17 PM
* To change this template use File | Settings | File Templates.
*/
@SuppressWarnings({
"JavaDoc"
}
)
@Entity
@Table(name = "students"
)
public
class
Student implements
Serializable
{
private
Long id;
private
String name;
private
Date birthday;
private
Student()
{
this
(null, ""
, new
Date());
}
public
Student(String name, Date birthday)
{
this
(null, name, birthday);
}
public
Student(Long id, String name, Date birthday)
{
this.id = id;
this.name = name;
this.birthday = new
Date(birthday.getTime());
}
@Id
@Column(name = "student_id"
)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "students_student_id_seq"
)
@SequenceGenerator(name = "students_student_id_seq"
, sequenceName = "students_student_id_seq"
)
public
Long getId()
{
return
id;
}
private
void
setId(Long id)
{
this.id = id;
}
@Column(name = "full_name"
, unique = false, nullable = false, length = 32)
public
String getName()
{
return
name;
}
private
void
setName(String name)
{
this.name = name;
}
@Column(name = "birthday"
, unique = false, nullable = false
)
public
Date getBirthday()
{
return
birthday;
}
private
void
setBirthday(Date birthday)
{
this.birthday = birthday;
}
public
int
getAge()
{
int
age = 0;
Calendar calendar = GregorianCalendar.getInstance();
Date today = new
Date();
calendar.setTime(today);
int
currentYear = calendar.get(Calendar.YEAR);
calendar.setTime(birthday);
int
birthYear = calendar.get(Calendar.YEAR);
age = currentYear - birthYear;
return
age;
}
public
boolean
equals(Object o)
{
if
(this
== o)
{
return
true
;
}
if
(o == null
|| getClass() != o.getClass())
{
return
false
;
}
Student student = (Student) o;
if
(id != null
? !id.equals(student.id) : student.id != null
)
{
return
false
;
}
return
true
;
}
public
int
hashCode()
{
return
(id != null
? id.hashCode() : 0);
}
public
String toString()
{
return
"Student{"
+
"id="
+ id +
", name='"
+ name + '\''
+
", birthday="
+ birthday +
'}'
;
}
}
Here's the DAO interface:
package
persistence;
public
interface
StudentDao
{
public
Student find(Long id);
public
List
public
List
public
void
saveOrUpdate(Student s);
public
void
delete(Long id);
public
void
delete(Student s);
}
private static final String url="jdbc:oracle:thin:@localhost:1521:XE";
private static final String driver="oracle.jdbc.driver.OracleDriver";
private static final String username="epet";
private static final String password="epet";
public Connection getConnection(){
Connection conn=null;
try {
Class.forName(driver);
conn=DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
}
return conn;
}
public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection conn){
try{
if(rs!=null){
rs.close();
rs=null;
}
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
if(conn!=null&&!conn.isClosed()){
conn.close();
conn=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
public int executeUpdate(String sql,Object... params){
Connection conn=getConnection();
PreparedStatement pstmt=null;
int result=0;
try {
pstmt=conn.prepareStatement(sql);
if(params!=null&¶ms.length>0){
for(int i=0;i
}
}
result=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
closeAll(null, pstmt, conn);
}
return result;
}