DAO设计模式

来源:互联网 发布:js的event对象 编辑:程序博客网 时间:2024/06/03 23:50

什么是设计模式呢?一种反复被使用,项目组成员都必须知晓,经过分类编写代码的设计经验的总和。1995年将设计模式提高了新的高度,并且规范化,这个事情是由4个的一个组织完成的,GOF(四人帮)。


DAO:数据访问对象(Data Access Object),以对象形式来操作数据库,之前我们第一阶段使用DAO设计模式就是一个标准的DAO,到了现在,我们主要是jsp+JDBC进行操作,在jsp中存在大量java代码,像之前的程序,我们应该用面向对象的方式进行封装,形成一个小小的组件。

DAO设计模式的流程:

DAO组成:
·POJO:每一个POJO对象就是数据库中一笔数据
·DAO:操作的接口,按照功能实现的需求贵了一组对数据库的操作方法,方法命名的规范:
·数据库的更新操作:doXxx(),新增doIns(),修改doUpd(),删除doDel()
·数据库的查询操作:findByUserName(String userName),findByID,findAll
·代理类:代理完成数据库连接的取得和关闭,并且调用真实主题类(实现类)
·实现类:应该完成和业务具体相关的数据库操作,不去专门处理数据库连接的取得和关闭
·工厂类:有接口就必须要有工厂,工厂的作用就是为了解耦合
注意:
在以后的开发中,jsp中是绝对不再允许导入java.sql包的
DAO完成之后,实际上就是按照一个组件的方式运行的
在使用DAO进行程序开发的时候,可以很好的将显示和具体的java代码分开,从java代码端进行数据库操作,而java代码我们完成的就是一个业务模型,很好的进行解耦合的操作。


POJO类:

package userPOJO;public class UserPOJO {    private int userId;    private String userName;    private String password;    private String headicon;    public UserPOJO(int userId, String userName, String password, String headicon) {        super();        this.userId = userId;        this.userName = userName;        this.password = password;        this.headicon = headicon;    }    public UserPOJO() {        super();        // TODO Auto-generated constructor stub    }    public int getUserId() {        return userId;    }    public void setUserId(int userId) {        this.userId = userId;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getHeadicon() {        return headicon;    }    public void setHeadicon(String headicon) {        this.headicon = headicon;    }}



DAO接口:

package userDAO;import userPOJO.UserPOJO;public interface UserDAO {    public  boolean AddUser(UserPOJO pojo) ;    public  boolean UpdUserInfo(UserPOJO pojo);    public  UserPOJO getUserpojoByUserName(String userName);}



数据库连接:

package pub;import java.sql.Connection;import java.sql.DriverManager;public class GetDataBaseConnection {    public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";    public static final String URL = "jdbc:oracle:thin:@10.211.55.3:1521:orcl";    public static final String USERNAME = "lizhi";    public static final String PASSWORD = "lizhi";    public static Connection getConnection() throws Exception{        Connection conn = null;        Class.forName(DRIVER);        conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);        return conn;    }}



DAO实现类:

package userImpl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import pub.GetDataBaseConnection;import userDAO.UserDAO;import userPOJO.UserPOJO;public class UserImpl implements UserDAO{    Connection conn ;    public UserImpl(Connection conn){        this.conn = conn;    }    public  boolean AddUser(UserPOJO pojo){        boolean flag = false;        Connection conn = null;        PreparedStatement pstate = null;        try {            conn = GetDataBaseConnection.getConnection();            conn.setAutoCommit(false);            String sql = "insert into usert (user_id,username,password,headicon) values(?,?,?,?)";            pstate = conn.prepareStatement(sql);            pstate.setInt(1, pojo.getUserId());            pstate.setString(2, pojo.getUserName());            pstate.setString(3, pojo.getPassword());            pstate.setString(4, pojo.getHeadicon());            pstate.execute();            conn.commit();            flag = true;        } catch (Exception e) {            try {                conn.rollback();            } catch (Exception e1) {                e1.printStackTrace();            }            e.printStackTrace();        } finally {            try {                pstate.close();                conn.close();            } catch (Exception e) {                e.printStackTrace();            }        }        return flag;    }public  boolean UpdUserInfo(UserPOJO pojo){    boolean flag = false;      Connection conn = null;      PreparedStatement pstate = null;      try{      conn = GetDataBaseConnection.getConnection();      conn.setAutoCommit(false);      String sql = "update usert set username = ?,password = ?,headicon = ? where user_id="+pojo.getUserId();      pstate = conn.prepareStatement(sql);      pstate.setString(1,pojo.getUserName());      pstate.setString(2,pojo.getPassword());      pstate.setString(3,pojo.getHeadicon());      pstate.execute();      conn.commit();      flag = true;      }catch(Exception e){          try{          conn.rollback();          }catch(Exception e1){              e1.printStackTrace();          }          e.printStackTrace();      }finally{          try{              pstate.close();              conn.close();          }catch(Exception e){              e.printStackTrace();          }      }      return flag;}   public  UserPOJO getUserpojoByUserName(String userName){    UserPOJO pojo = null;     Connection conn = null;      PreparedStatement pstate = null;      ResultSet res = null;      try{      conn = GetDataBaseConnection.getConnection();      String sql = "select user_id,password,headicon from usert where username="+"'"+userName+"'";      pstate = conn.prepareStatement(sql);      res = pstate.executeQuery();      while(res.next()){          int userId = res.getInt(1);          String password = res.getString(2);          String headicon = res.getString(3);          pojo = new UserPOJO(userId,userName,password,headicon);      }      }catch(Exception e){          try{          conn.rollback();          }catch(Exception e1){              e1.printStackTrace();          }          e.printStackTrace();      }finally{          try{              res.close();              pstate.close();              conn.close();          }catch(Exception e){              e.printStackTrace();          }      }    return pojo;}}



DAO代理类:

package userProxy;import java.sql.Connection;import pub.GetDataBaseConnection;import userDAO.UserDAO;import userImpl.UserImpl;import userPOJO.UserPOJO;public class UserProxy implements UserDAO{    Connection conn;    UserImpl impl ;    public UserProxy(){        try {            this.conn = GetDataBaseConnection.getConnection();        } catch (Exception e) {            e.printStackTrace();        }        this.impl = new UserImpl(this.conn);    }    @Override    public boolean AddUser(UserPOJO pojo) {        // TODO Auto-generated method stub        boolean flag = this.impl.AddUser(pojo);        this.close();        return flag;    }    @Override    public boolean UpdUserInfo(UserPOJO pojo) {        // TODO Auto-generated method stub        boolean flag = this.impl.UpdUserInfo(pojo);        this.close();        return flag;    }    @Override    public UserPOJO getUserpojoByUserName(String userName) {        // TODO Auto-generated method stub        UserPOJO pojo = this.impl.getUserpojoByUserName(userName);        this.close();        return pojo;    }    public void close(){        try {            this.conn .close();        } catch (Exception e) {            e.printStackTrace();            // TODO: handle exception        }    }}



DAO工厂类:

package userFactory;import userDAO.UserDAO;import userProxy.UserProxy;public class UserFactory {    public static UserDAO getUserInfoDAOInstences(){        return new UserProxy();    }}

测试代码:

boolean bool=UserFactory.getUserInfoDAOInstences().AddUser(pojo);//使用了添加用户功能

通过DAO设计模式,可以在JSP中屏蔽了数据库连接的操作,达到JSP只负责显示的效果。

0 0
原创粉丝点击