古诗MySQL数据库中使用到的DAO模式
2016-02-19 11:19
627 查看
一.什么是DAO模式
DAO(Data Access Object Pattern)用于将低层的数据操作API与上层的业务逻辑层分离,其主要涉及以下几个部分:
1.Data Access Object Interface
定义了在model object上的标准操作接口。
2.Data Access Object concrete class
实现了1中的接口,负责从database或者xml等中操作数据。
3.Model Object or Value Object
简单的POJO对象。
二.DAO模式例子
Model or Value Object :Student
Data Access Object Interface: StudentDAO
Concrete class Implement Data Access Object Interface:StudentDaoImpl
StudentDAO用来展示如何使用DAO模式。
Step1
Student.java
Step2
Data Access object interface
StudentDao.java
Step3
concrete class implemting above interface
StudentDaoImpl.java
step4
use StudentDao to demonstrate DAO pettern
DAO(Data Access Object Pattern)用于将低层的数据操作API与上层的业务逻辑层分离,其主要涉及以下几个部分:
1.Data Access Object Interface
定义了在model object上的标准操作接口。
2.Data Access Object concrete class
实现了1中的接口,负责从database或者xml等中操作数据。
3.Model Object or Value Object
简单的POJO对象。
二.DAO模式例子
Model or Value Object :Student
Data Access Object Interface: StudentDAO
Concrete class Implement Data Access Object Interface:StudentDaoImpl
StudentDAO用来展示如何使用DAO模式。
Step1
Student.java
public class Student { private String name; private int rollNo; Student(String name, int rollNo){ this.name = name; this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } }
Step2
Data Access object interface
StudentDao.java
import java.util.List; public interface StudentDao { public List<Student> getAllStudents(); public Student getStudent(int rollNo); public void updateStudent(Student student); public void deleteStudent(Student student); }
Step3
concrete class implemting above interface
StudentDaoImpl.java
import java.util.ArrayList; import java.util.List; public class StudentDaoImpl implements StudentDao { //list is working as a database List<Student> students; public StudentDaoImpl(){ students = new ArrayList<Student>(); Student student1 = new Student("Robert",0); Student student2 = new Student("John",1); students.add(student1); students.add(student2); } @Override public void deleteStudent(Student student) { students.remove(student.getRollNo()); System.out.println("Student: Roll No " + student.getRollNo() + ", deleted from database"); } //retrive list of students from the database @Override public List<Student> getAllStudents() { return students; } @Override public Student getStudent(int rollNo) { return students.get(rollNo); } @Override public void updateStudent(Student student) { students.get(student.getRollNo()).setName(student.getName()); System.out.println("Student: Roll No " + student.getRollNo() + ", updated in the database"); } }
step4
use StudentDao to demonstrate DAO pettern
public class DaoPatternDemo { public static void main(String[] args) { StudentDao studentDao = new StudentDaoImpl(); //print all students for (Student student : studentDao.getAllStudents()) { System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]"); } //update student Student student =studentDao.getAllStudents().get(0); student.setName("Michael"); studentDao.updateStudent(student); //get the student studentDao.getStudent(0); System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]"); } }
相关文章推荐
- mysql之——存储过程 + 游标 + 事务
- Mysql各种索引区别
- mySQL 开启事件存储过程
- index condition pushdown(icp)---mysql5.6新功能
- mysql中文乱码问题
- mysql 查看 删除 日志操作总结(包括单独和主从mysql)
- MySQL服务器权限表
- MySQL数据处理函数
- MySQL各个版本的区别
- MySQL查看数据库信息
- mysql事物隔离级别
- 转 Cobar使用文档(可用作MySQL大型集群解决方案)
- Mysql中字符串正确的连接方法
- 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……
- 解决MySQL安装版出现the the service mysql57 failed问题的方法。
- 关于MySQL修改密码的问题
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- MySQL5.6基于GTID的主从复制
- 3.0 mysql配置讲解