Dao 是个什么东西?

最近忙于,采用jsp/servlet设计个人网站,但设计注册与登陆系统,以及文件、用户模板设计的时候,突然发现如果只是有传统的JDBC编程,会造成大量的重复,而且逻辑相当混乱,百度了一下,才知道还有Dao神器(请原谅,楼主小白一个),它将数据库的操作(增加、删除、修改、查找等)抽象为了一个类,而且在建立多对多、一对多或者一对一关系的时候,相当的便利;下面,结合我的个人网站项目对其进行介绍和展示;

//下面为登陆与注册系统的简单实现;
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package MagicWorld;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(
        name = "index"
)
public class index extends HttpServlet {
    private static String DBMETH = "com.mysql.jdbc.Driver";
    private static String DBURL = "jdbc:mysql://localhost:3306/MagicWorld";
    private static String USERNAME = "root";
    private static String PASSWORD = "root";

    public index() {
    }

    private static Connection LinkDB() {
        Connection coo = null;

        try {
            Class.forName(DBMETH);
        } catch (ClassNotFoundException var3) {
            var3.printStackTrace();
        }

        try {
            coo = DriverManager.getConnection(DBURL, USERNAME, PASSWORD);
        } catch (SQLException var2) {
            var2.printStackTrace();
        }
        return coo;
    }

    protected void Sign(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        String message=null;
        PrintWriter out = response.getWriter();
        Connection coo = LinkDB();
        System.out.println("驱动加载成功");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String sign=request.getParameter("sign in");
        System.out.println(username+ " "+password+" "+sign);
        String sql = "SELECT USERNAME,PASSWORD FROM usersmessage Where USERNAME=?;";
        String sqlindex = "INSERT INTO usersmessage(username,password)values(?,?);";
        PreparedStatement temp = coo.prepareStatement(sql);
        temp.setString(1, username);
        System.out.println("连接成功...");
        ResultSet result = temp.executeQuery();
        System.out.println("查询成功...");
        if (("sign in").equals(sign)) {
            if (result.next()) {
                if (result.getString(2).equals(password)) {
                    //out.println("登录成功");
                    message="登录成功";

                } else {
                    message="密码错误,请重新输入";
                    // out.println("密码错误,请重新输入");
                }

                result.close();
            } else {
                message="该用户不存在";
                //out.println("该用户不存在");
            }

            temp.close();
            coo.close();
        } else {
            System.out.println("开始注册");
            if (result.next()) {
                //out.println("用户名已存在");
                message="用户名已存在";
                temp.close();
                result.close();
            }
            else {
                System.out.println("开始注册...");
                temp.close();
                result.close();
                PreparedStatement emp = coo.prepareStatement(sqlindex);
                System.out.println("连接成功");
                emp.setString(1,username);
                emp.setString(2,password);
                int num = emp.executeUpdate();
                if (num > 0) {
                    //out.println("注册成功,请返回登录");
                    message="注册成功,请返回登录";
                    request.getRequestDispatcher("jsp/sign.jsp").forward(request,response);
                } else {
                    //out.println("数据库异常,请重新注册");
                    message="数据库异常,请重新注册";
                }

                emp.close();
            }

            coo.close();
        }

        System.out.println(message);
        //request.getRequestDispatcher("jsp/sign.jsp").forward(request,response);
        request.setAttribute("message",message);
        request.getRequestDispatcher("jsp/sign.jsp").forward(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            this.Sign(request, response);
        } catch (SQLException e) {
            System.out.println("数据库异常");
        } finally {
            System.out.println("完毕,已关闭数据库");
        }
    }
    @Override
    public void destroy() {
        super.destroy();
    }
}
---
// 是不是觉得逻辑混乱,so do I !

Dao, what the fu ** ?

首先分析一下,上面给出的源码:
1、登录:输入用户名和密码后,登录此系统,首先要对其进行验证,看看是否在数据库中;
2、注册:首先进行查找用户名操作,看看是否已存在此用户名,没有的话,对数据库进行插入操作;

可以看出以上主要是数据库的插入与删除等操作;
但是,如果我要存入一篇文章呢,是不是又要用到插入了呢?
如果我要进行实现每日推荐系统的更新呢,是不又要用到查找了呢?
如果,我要再实现一个blog分类呢?
因此,我们可以提取这些共性的或者是通用的东西,然后封装为一个个接口;我们只操作这些接口,或者实现这些接口;

Dao 规范
Dao是一个组件,它有自己的分包规范:

   com.song.pss.domain;  //装pss模块的domain类,模型对象.
   com.song.pss.dao;     //装pss模块的dao接口.
   com.song.pss.dao.impl;//装pss模块的dao接口的实现类.
   com.song.pss.test;    //存储DAO的测试类

\

你可能感兴趣的:(Dao 是个什么东西?)