DAO设计模式总结

1、  之前所以的jdbc代码都写在jsp页面中,维护困难。Jsp页面只关注数据的显示,不关心数据是从那里来的,因此jsp中不能使用任何的sql

DAOdata access object)是夹在业务层数据层中间的数据库访问接口,DAO模式是j2ee数据模式之一,j2ee的组件层次:客户端à表示层à业务层à数据层à数据库

 

2、  一个典型的DAO实现有以下组件:

a)         一个DAO接口

                         i.              用于封装对数据的所有操作

b)        一个DAO接口实现类

                         i.              实现对数据的所以操作

c)        一个传输对象VO(值对象,只包含settergetter方法的类,即:person类)

d)        一个工厂类

                         i.              PersonDao dao = new PersonDAOImple();接口直接通过子类实例化,就必须知道其具体的子类,造成修改不方便,所以使用工厂设计,使前台不必关注与具体的子类是谁

3、  DAO整体设计采用一下模式:

a)         调用处(jsp页面)à DAO工厂 à 具体子类实现 à 完成数据操作

 

4、例:

 

DAOFactory.java (工厂类)

package cn.mldn.lxh.factory ;

 

import cn.mldn.lxh.dao.* ;

import cn.mldn.lxh.dao.impl.* ;

 

public class DAOFactory

{

       public static PersonDAO getPersonDAOInstance()

       {

              return new PersonDAOImpl() ;

       }

};

 

DataBaseConnection.java (数据库连接)

package cn.mldn.lxh.dbc ;

import java.sql.* ;

 

// 主要功能就是连接数据库、关闭数据库

public class DataBaseConnection

{

       private final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;

       private final String DBURL = "jdbc:oracle:thin:@localhost:1521:MLDN" ;

       private final String DBUSER = "scott" ;

       private final String DBPASSWORD = "tiger" ;

       private Connection conn = null ;

 

       public DataBaseConnection()

       {

              try

              {

                     Class.forName(DBDRIVER) ;

                     this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;     

              }

              catch (Exception e)

              {

              }

       }

 

       // 取得数据库连接

       public Connection getConnection()

       {

              return this.conn ;

       }

 

       // 关闭数据库连接

       public void close()

       {

              try

              {

                     this.conn.close() ;

              }

              catch (Exception e)

              {

              }           

       }

};

 

Person.java (传输对象VO

package cn.mldn.lxh.vo ;

 

// 值对象,包含属性,settergetter方法

public class Person

{

       private String id ;

       private String name ;

       private String password ;

       private int age ;

       private String email ;

 

       // 生成gettersetter方法

       public void setId(String id)

       {

              this.id = id ;

       }

       public void setName(String name)

       {

              this.name = name ;

       }

       public void setPassword(String password)

       {

              this.password = password ;

       }

       public void setAge(int age)

       {

              this.age = age ;

       }

       public void setEmail(String email)

       {

              this.email = email ;

       }

       public String getId()

       {

              return this.id ;

       }

       public String getName()

       {

              return this.name ;

       }

       public String getPassword()

       {

              return this.password ;

       }

       public int getAge()

       {

              return this.age ;

       }

       public String getEmail()

       {

              return this.email ;

       }

};

 

PersonDao.java DAO接口)

package cn.mldn.lxh.dao ;

 

import java.util.* ;

import cn.mldn.lxh.vo.* ;

 

// 规定出了操作person表在此项目里的全部方法

public interface PersonDAO

{

       // 增加操作

       public void insert(Person person) throws Exception ;

       // 修改操作

       public void update(Person person) throws Exception ;

       // 删除操作

       public void delete(String id) throws Exception ;

       // ID查询操作

       public Person queryById(String id) throws Exception ;

       // 查询全部

       public List queryAll() throws Exception ;

       // 模糊查询

       public List queryByLike(String cond) throws Exception ;

}

 

PersonDaoImpl.java DAO接口实现)

package cn.mldn.lxh.dao.impl ;

import java.sql.* ;

import java.util.* ;

import cn.mldn.lxh.vo.* ;

import cn.mldn.lxh.dbc.* ;

import cn.mldn.lxh.dao.* ;

 

// 此类需要完成具体的数据库操作,需要JDB代码

public class PersonDAOImpl implements PersonDAO

{

       // 增加操作

       public void insert(Person person) throws Exception

       {

              String sql = "INSERT INTO person (id,name,password,age,email) VALUES (?,?,?,?,?)" ;

              PreparedStatement pstmt = null ;

              DataBaseConnection dbc = null ;

 

              // 下面是针对数据库的具体操作

              try

              {

                     // 连接数据库

                     dbc = new DataBaseConnection() ;

                     pstmt = dbc.getConnection().prepareStatement(sql) ;

                     pstmt.setString(1,person.getId()) ;

                     pstmt.setString(2,person.getName()) ;

                     pstmt.setString(3,person.getPassword()) ;

                     pstmt.setInt(4,person.getAge()) ;

                     pstmt.setString(5,person.getEmail()) ;

                     // 进行数据库更新操作

                     pstmt.executeUpdate() ;

                     pstmt.close() ;

              }

              catch (Exception e)

              {

                     throw new Exception("操作出现异常") ;

              }

              finally

              {

                     // 关闭数据库连接

                     dbc.close() ;

              }

       }

       // 修改操作

       public void update(Person person) throws Exception

       {

              String sql = "UPDATE person SET name=?,password=?,age=?,email=? WHERE id=?" ;

              PreparedStatement pstmt = null ;

              DataBaseConnection dbc = null ;

 

              // 下面是针对数据库的具体操作

              try

              {

                     // 连接数据库

                     dbc = new DataBaseConnection() ;

                     pstmt = dbc.getConnection().prepareStatement(sql) ;               

                     pstmt.setString(1,person.getName()) ;

                     pstmt.setString(2,person.getPassword()) ;

                     pstmt.setInt(3,person.getAge()) ;

                     pstmt.setString(4,person.getEmail()) ;

                     pstmt.setString(5,person.getId()) ;

                     // 进行数据库更新操作

                     pstmt.executeUpdate() ;

                     pstmt.close() ;

              }

              catch (Exception e)

              {

                     throw new Exception("操作出现异常") ;

              }

              finally

              {

                     // 关闭数据库连接

                     dbc.close() ;

              }

       }

       // 删除操作

       public void delete(String id) throws Exception

       {

              String sql = "DELETE FROM person WHERE id=?" ;

              PreparedStatement pstmt = null ;

              DataBaseConnection dbc = null ;

 

              // 下面是针对数据库的具体操作

              try

              {

                     // 连接数据库

                     dbc = new DataBaseConnection() ;

                     pstmt = dbc.getConnection().prepareStatement(sql) ;               

                     pstmt.setString(1,id) ;

                     // 进行数据库更新操作

                     pstmt.executeUpdate() ;

                     pstmt.close() ;

              }

              catch (Exception e)

              {

                     throw new Exception("操作出现异常") ;

              }

              finally

              {

                     // 关闭数据库连接

                     dbc.close() ;

              }

       }

       // ID查询操作

       public Person queryById(String id) throws Exception

       {

              Person person = null ;

              String sql = "SELECT id,name,password,age,email FROM person WHERE id=?" ;

              PreparedStatement pstmt = null ;

              DataBaseConnection dbc = null ;

 

              // 下面是针对数据库的具体操作

              try

              {

                     // 连接数据库

                     dbc = new DataBaseConnection() ;

                     pstmt = dbc.getConnection().prepareStatement(sql) ;               

                     pstmt.setString(1,id) ;

                     // 进行数据库查询操作

                     ResultSet rs = pstmt.executeQuery() ;

                     if(rs.next())

                     {

                            // 查询出内容,之后将查询出的内容赋值给person对象

                            person = new Person() ;

                            person.setId(rs.getString(1)) ;

                            person.setName(rs.getString(2)) ;

                            person.setPassword(rs.getString(3)) ;

                            person.setAge(rs.getInt(4)) ;

                            person.setEmail(rs.getString(5)) ;

                     }

                     rs.close() ;

                     pstmt.close() ;

              }

              catch (Exception e)

              {

                     throw new Exception("操作出现异常") ;

              }

              finally

              {

                     // 关闭数据库连接

                     dbc.close() ;

              }

              return person ;

       }

       // 查询全部

       public List queryAll() throws Exception

       {

              List all = new ArrayList() ;

              String sql = "SELECT id,name,password,age,email FROM person" ;

              PreparedStatement pstmt = null ;

              DataBaseConnection dbc = null ;

 

              // 下面是针对数据库的具体操作

              try

              {

                     // 连接数据库

                     dbc = new DataBaseConnection() ;

                     pstmt = dbc.getConnection().prepareStatement(sql) ;               

                     // 进行数据库查询操作

                     ResultSet rs = pstmt.executeQuery() ;

                     while(rs.next())

                     {

                            // 查询出内容,之后将查询出的内容赋值给person对象

                            Person person = new Person() ;

                            person.setId(rs.getString(1)) ;

                            person.setName(rs.getString(2)) ;

                            person.setPassword(rs.getString(3)) ;

                            person.setAge(rs.getInt(4)) ;

                            person.setEmail(rs.getString(5)) ;

 

                            // 将查询出来的数据加入到List对象之中

                            all.add(person) ;

                     }

                     rs.close() ;

                     pstmt.close() ;

              }

              catch (Exception e)

              {

                     throw new Exception("操作出现异常") ;

              }

              finally

              {

                     // 关闭数据库连接

                     dbc.close() ;

              }

              return all ;

       }

       // 模糊查询

       public List queryByLike(String cond) throws Exception

       {

              List all = new ArrayList() ;

              String sql = "SELECT id,name,password,age,email FROM person WHERE name LIKE ? or email LIKE ?" ;

              PreparedStatement pstmt = null ;

              DataBaseConnection dbc = null ;

 

              // 下面是针对数据库的具体操作

              try

              {

                     // 连接数据库

                     dbc = new DataBaseConnection() ;

                     pstmt = dbc.getConnection().prepareStatement(sql) ; 

                     // 设置模糊查询条件

                     pstmt.setString(1,"%"+cond+"%") ;

                     pstmt.setString(2,"%"+cond+"%") ;

                     // 进行数据库查询操作

                     ResultSet rs = pstmt.executeQuery() ;

                     while(rs.next())

                     {

                            // 查询出内容,之后将查询出的内容赋值给person对象

                            Person person = new Person() ;

                            person.setId(rs.getString(1)) ;

                            person.setName(rs.getString(2)) ;

                            person.setPassword(rs.getString(3)) ;

                            person.setAge(rs.getInt(4)) ;

                            person.setEmail(rs.getString(5)) ;

 

                            // 将查询出来的数据加入到List对象之中

                            all.add(person) ;

                     }

                     rs.close() ;

                     pstmt.close() ;

              }

              catch (Exception e)

              {

                     throw new Exception("操作出现异常") ;

              }

              finally

              {

                     // 关闭数据库连接

                     dbc.close() ;

              }

              return all ;

       }

};

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值