Java

设计模式之DAO模式

2019-05-27  本文已影响0人  盗梦者_56f2

简介

DAO (DataAccessobjects 数据存取对象)是指位于业务逻辑和持久化数据之间实现对持久化数据的访问。通俗来讲,就是将数据库操作都封装起来。
这种模式可以大概分为三个层:1.DAO层 2.服务层 3.表现层

1)表现层 :相当于客户端用来查看,提交信息的角色

2)服务层 :是表现层和DAO层的纽带,其实也没干什么事就是通知消息的角色

3)DAO :真正要做事的角色(对数据库的某些操作)

执行顺序: 表现层-->服务层-->DAO层-->返回服务层-->返回表现层

实现代码

#Student.java
package com.myjdbc.bean;
 
public class Student {
    
    private Integer stuId;
    private String stuName ;
    private Integer stuAge;
    private String stuTel ;
    private String stuAddress ;
    private Integer groupId;
    
    public Integer getStuId() {
        return stuId;
    }
    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public Integer getStuAge() {
        return stuAge;
    }
    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }
    public String getStuTel() {
        return stuTel;
    }
    public void setStuTel(String stuTel) {
        this.stuTel = stuTel;
    }
    public String getStuAddress() {
        return stuAddress;
    }
    public void setStuAddress(String stuAddress) {
        this.stuAddress = stuAddress;
    }
    public Integer getGroupId() {
        return groupId;
    }
    public void setGroupId(Integer groupId) {
        this.groupId = groupId;
    }
}
#JDBCUtils.java
package com.myjdbc.utils;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
 
public class JDBCUtils {
    /**
     * 获取连接
     * 
     */
    public static Connection getConnection()
    {
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://localhost:1433; DataBaseName = studentManager";
        String user = "root" ;
        String password = "root";
        Connection conn = null ;
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    /**
     * 关闭连接
     */
    public static void free(ResultSet rs, Statement sta , Connection con)
    {
        try {
            if(null != rs)
            {
                rs.close();
            }
            
            if(null != sta)
            {
                sta.close();
            }
            
            if(null != con)
            {
                con.close();
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
#StudentDAO.java
package com.myjdbc.dao;
 
import java.util.Set;
 
import com.myjdbc.bean.Student ;
 
public interface StudentDAO {
        //查询所有
    public Set<Student> findAll();
}
#ConcreteStudentDao.java
package com.myjdbc.dao;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
 
import com.myjdbc.bean.Student;
import com.myjdbc.dao.StudentDAO;
import com.myjdbc.utils.JDBCUtils;
 
public class ConcreteStudentDao implements StudentDAO{
    //查询所有
    public Set<Student> findAll()
    {
        Connection conn = null ;
        PreparedStatement ps = null ;
        Student stu = null ;
        ResultSet rs = null;
        Set<Student> set = null ;
        try
        {
            conn = JDBCUtils.getConnection();
            String sql = "select stuName,stuAge,stuTel,stuAddress,groupId from student";
            ps = conn.prepareStatement(sql);
            
            set = new HashSet<Student>() ;
            rs = ps.executeQuery() ;
            
            while(rs.next())
            {
                stu = new Student();
                stu.setStuName(rs.getString(1));
                stu.setStuAge(rs.getInt(2));
                stu.setStuTel(rs.getString(3));
                stu.setStuAddress(rs.getString(4));
                stu.setGroupId(rs.getInt(5));
                set.add(stu);
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            JDBCUtils.free(rs, ps, con);
        }
        return set;
    }
}
#StudentService.java
package com.myjdbc.service;
 
import java.util.Set;
 
import com.myjdbc.bean.Student;
import com.myjdbc.dao.StudentDAO;
import com.myjdbc.dao.ConcreteStudentDao;
 
public class StudentService {
    
    StudentDAO sd = new ConcreteStudentDao();
    public Set<Student> findAll()
    {
        return this.sd.findAll();
    }
 
}
#Client.java
package com.myjdbc.test;
 
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
 
import com.myjdbc.bean.Student;
import com.myjdbc.service.StudentService;
 
public class Client {
   public static void main(String[] args)
   {
       Set<Student> set = new HashSet<Student>();
       StudentService ss = new StudentService();
       set = ss.findAll() ;
       Iterator<Student> iterator = set.iterator();
       while(iterator.hasNext())
       {
          Student student =  (Student)iterator.next() ;
          System.out.println(student.getStuName() +" " +student.getStuAge()+" "+student.getStuTel()+" "+student.getStuAddress()+" "+student.getGroupId());
       }
   }
}

上一篇下一篇

猜你喜欢

热点阅读