Fork me on GitHub

设计模式———DAO设计模式

设计模式——DAO设计模式

什么是DAO模式:DAO(数据访问对象)是一种应用程序编程接口(API),存在于微软的Visual Basic中,它允许程序员请求对微软的Access数据库的访问。DAO是微软的第一个面向对象的数据库接口。DAO对象封闭了Access的Jet函数。通过Jet函数,它还可以访问其他的结构化查询语言(SQL)数据库
DAO模式是标准的J2EE设计模式之一.开发人员使用这个模式把底层的数据访问操作和上层的商务逻辑分开.一个典型的DAO实现有下列几个组件:

  1. 一个DAO工厂类;
  2. 一个DAO接口;
  3. 一个实现DAO接口的具体类;
  4. 数据传递对象(有些时候叫做值对象)

下面举例:(实现emp表的增删该查)
1.DAO接口:

1
2
3
4
5
6
7
8
9
10
11
public interface EmployeeDao {
//1查询
public List<Employee> findAll();
//根据编号来查询员工
Employee findByNo(int empno);
//2更新
void update(Employee e);
//3删除
void delete(int empno);
//4添加
void add(Employee e);

2.DAO实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
public class EmployeeDaoImpl implements EmployeeDao{  
@Override
public List<Employee> findAll() {
ArrayList <Employee> employees = new ArrayList<>();

//获取连接
Connection conn = null;
PreparedStatement pstat = null;
ResultSet rs = null;
try {
conn = DbUtils.getConnection();
pstat = conn.prepareStatement("select * from emp;");
rs = pstat.executeQuery();
while(rs.next())
{
int empno = rs.getInt("empno");
String ename = rs.getString("ename");
String job = rs.getString("job");
int mgr = rs.getInt("mgr");
Date hiredate = rs.getDate("hiredate");
double sal = rs.getDouble("sal");
double comm = rs.getDouble("comm");
int deptno = rs.getInt("deptno");

Employee employee = new Employee(empno,ename,job,mgr,hiredate,sal,comm,deptno);
employees.add(employee);
}
try {
return employees;
} catch (Exception e) {
throw new RuntimeException("查询emp失败");
}finally {
DbUtils.closeAll(rs, pstat, conn);
}
} catch (Exception e) {

}
return null;
}
//更新
@Override
public void update(Employee e) {

Object[] params = {e.getEname(),e.getJob(),e.getMgr(),e.getHiredate(),e.getSal(),e.getComm(),e.getDeptno(),e.getEmpno()};
DbUtils.executeUpdate("update emp set ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=? where empno=?", params);

} //删除
@Override
public void delete(int empno) {
DbUtils.executeUpdate("delete from emp where empno=?", empno);

}
//添加
@Override
public void add(Employee e) {
Object[] params = {e.getEmpno(),e.getEname(),e.getJob(),e.getMgr(),e.getHiredate(),e.getSal(),e.getComm(),e.getDeptno()};
DbUtils.executeUpdate("insert into emp values(?,?,?,?,?,?,?,?)", params);


}
@Override
public Employee findByNo(int empno) {
//获取连接
Connection conn = null;
PreparedStatement pstat = null;
ResultSet rs = null;
try {
conn = DbUtils.getConnection();
pstat = conn.prepareStatement("select * from emp where empno=?;");
pstat.setInt(1,empno);
rs = pstat.executeQuery();
Employee employee = null;
if(rs.next())
{
String ename = rs.getString("ename");
String job = rs.getString("job");
int mgr = rs.getInt("mgr");
Date hiredate = rs.getDate("hiredate");
double sal = rs.getDouble("sal");
double comm = rs.getDouble("comm");
int deptno = rs.getInt("deptno");

employee = new Employee(empno,ename,job,mgr,hiredate,sal,comm,deptno);

}
try {
return employee;
} catch (Exception e) {
throw new RuntimeException("查询emp失败");
}finally {
DbUtils.closeAll(rs, pstat, conn);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;

}

}

3.用户类

java.text.SimpleDateFormat; import java.util.Date; public class Employee {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private double sal;
private double comm;
private int deptno;
public Employee(int empno, String ename, String job, int mgr, Date hiredate, double sal, double comm, int deptno)
{
super();
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
public Employee()
{
super();

}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "Employee [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate="
+ hiredate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
}
}

4.测试类

java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Scanner; import net.zzqd.dao.EmployeeDao;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import net.zzqd.dao.impl.EmployeeDaoImpl;
import net.zzqd.domain.Employee;public class Test {
public static void main(String[] args) {
System.out.println("--------------欢迎进入xxx系统-------------");
System.out.println("请选择操作:1 查询 2 添加 3 更新 4删除");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
EmployeeDao employeeDao = new EmployeeDaoImpl();
switch (choice) {
case 1:
List<Employee> employees = employeeDao.findAll();
for(Employee e:employees)
{
System.out.println(e.toString());
}
break;
case 2:
System.out.println("请输入员工编号");
int empno = input.nextInt();
System.out.println("请输入员工姓名");
String ename = input.next();
System.out.println("请输入员工工作");
String job = input.next();
System.out.println("请输入员工经理编号");
int mgr = input.nextInt();
System.out.println("请输入员工入职时间");
String hiredate = input.next();
System.out.println("请输入员工工资");
double sal = input.nextDouble();
System.out.println("请输入员工奖金");
double comm = input.nextInt();
System.out.println("请输入员工部门");
int deptno = input.nextInt();
//1封装成一个对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Employee e = new Employee(empno,ename,job,mgr,sdf.parse(hiredate),sal,comm,deptno);
employeeDao.add(e);
System.out.println("添加成功");
} catch (Exception e) {
e.printStackTrace();
}
break;
case 3:
System.out.println("请输入要查找的员工编号");
int eno = input.nextInt();
Employee employee = employeeDao.findByNo(eno);
if(employee!=null)
{
System.out.println("请输入新的名字");
String newname = input.next();
employee.setEname(newname);
System.out.println(employee.toString());
//修改数据库
employeeDao.update(employee);
System.out.println("修改成功");
System.out.println(employee.toString());

System.out.println("-------------------------");
System.out.println("请输入您更新的信息");
System.out.println("请输入新的工作");
String newjob = input.next();
employee.setJob(newjob);
System.out.println("请输入员工经理编号");
int newmgr = input.nextInt();
employee.setMgr(newmgr);
System.out.println("请输入员工入职时间");
try {

Date date = new Date();
String newhiredate = input.next();
date = new SimpleDateFormat("yyyy-MM-dd").parse(newhiredate) ;
employee.setHiredate(date);

} catch (ParseException e1)
{
e1.printStackTrace();
}

System.out.println("请输入员工工资");
double newsal = input.nextDouble();
employee.setSal(newsal);
System.out.println("请输入员工奖金");
double newcomm = input.nextInt();
employee.setComm(newcomm);
System.out.println("请输入员工部门");
int newdeptno = input.nextInt();
employee.setDeptno(newdeptno);
employeeDao.update(employee);
System.out.println("更新成功");
}
else
{
System.out.println("查无此人");
}
case 4:
System.out.println("请输入要删除的编号");
int no = input.nextInt();
employeeDao.delete(no);
System.out.println("删除成功");
break;
default:
break;
}
}
}

DbUtils工具类略