[Java] DAO设计模式
Posted cxc1357
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Java] DAO设计模式相关的知识,希望对你有一定的参考价值。
背景
- B/S(Browser/Server):浏览器/服务器架构,用于广域网
- C/S(Client/Server):客户机/服务器架构,用于局域网
- 不同层之间没有任何的耦合问题,彼此都是依据协议或者接口标准结合在一起
业务分析
- 前端
- 控制层
- 调用业务层
- 业务层(Business Object BO)
- 调用数据层实现业务操作
- 业务层需要明确地知道数据层的操作方法,但不需要知道它的具体实现
- 使用代理模式(动态代理)进行业务控制,Sprint、Struct框架中会提供
- 定义操作协议(接口)
- 定义业务实现类,控制数据库的打开和关闭,取得数据层接口对象,调用数据层操作
- 定义业务工厂类,用于供控制层实例化,隐藏具体实现
- 数据层(Data Access Object DAO,持久层)
- 把具体的业务“翻译”成数据库的原子操作(增删改查)
- 定义接口,对业务层隐藏子类,一个数据表对应一个接口
- 定义简单Java类(POJO),对数据表中的数据进行包装,以便在不同层之间传递数据
- 定义实现类,一个类实现一个接口,类中一个方法实现一个具体的SQL操作
- 定义工厂类,用于其它层实例化接口对象,隐藏具体实现
- 数据库
实例
- 功能:连接mysql,为雇员表增加一行数据
- 每层只能和相邻层交互
- 每层结构都是:接口先行--子类实现--工厂解耦
- 项目结构:
代码
- 数据库连接类 -- DatabaseConnection.java
1 package dbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 7 public class DatabaseConnection { 8 private static final String DBDRIVER = "com.mysql.cj.jdbc.Driver"; 9 private static final String DBURL = "jdbc:mysql://127.0.0.1:3306/company?serverTimezone=GMT%2B8&characterEncoding=UTF-8&useSSL=false"; 10 private static final String DBUSER = "root"; 11 private static final String PASSWORD = "Chen1227+"; 12 private Connection conn = null; 13 14 // 连接数据库 15 public DatabaseConnection() { 16 try { 17 Class.forName(DBDRIVER); 18 } catch (ClassNotFoundException e1) { 19 e1.printStackTrace(); 20 } 21 try { 22 this.conn = DriverManager.getConnection(DBURL,DBUSER,PASSWORD); 23 } catch (SQLException e) { 24 e.printStackTrace(); 25 } 26 } 27 28 // 取得数据库的连接对象 29 public Connection getConnection() { 30 return this.conn; 31 } 32 33 // 关闭数据库 34 public void close() { 35 if(this.conn != null) { 36 try { 37 this.conn.close(); 38 //System.out.print("close success!"); 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } 42 } 43 } 44 }
- POJO类 -- Emp.java
1 package vo; 2 3 import java.io.Serializable; 4 import java.util.Date; 5 6 7 @SuppressWarnings("serial") 8 public class Emp implements Serializable{ 9 private Integer empno; 10 private String ename; 11 private String job; 12 private Date hiredate; 13 private Double sal; 14 private Double comm; 15 16 public Integer getEmpno() { 17 return empno; 18 } 19 public void setEmpno(Integer empno) { 20 this.empno = empno; 21 } 22 public String getEname() { 23 return ename; 24 } 25 public void setEname(String name) { 26 this.ename = name; 27 } 28 public String getJob() { 29 return job; 30 } 31 public void setJob(String job) { 32 this.job = job; 33 } 34 public Date getHiredate() { 35 return hiredate; 36 } 37 public void setHiredate(Date hiredate) { 38 this.hiredate = hiredate; 39 } 40 public Double getSal() { 41 return sal; 42 } 43 public void setSal(Double sal) { 44 this.sal = sal; 45 } 46 public Double getComm() { 47 return comm; 48 } 49 public void setComm(Double comm) { 50 this.comm = comm; 51 } 52 }
- 数据层
- 数据层接口 -- IEmpDAO.java
1 package dao; 2 3 import java.util.List; 4 import java.util.Set; 5 6 import vo.Emp; 7 8 public interface IEmpDAO { 9 public boolean doCreate(Emp vo) throws Exception; 10 public boolean doUpdata(Emp vo) throws Exception; 11 public boolean doRemoveBatch(Emp vo) throws Exception; 12 public boolean doRemoveBatch(Set<Integer> ids) throws Exception; 13 public Emp findById(Integer id) throws Exception; 14 public List<Emp> findAll() throws Exception; 15 public List<Emp> findAll(Integer currentPage, Integer LineSize, String column, String keyWord) throws Exception; 16 public Integer getAllCount(String column, String keyWord) throws Exception; 17 }
-
- 数据层实现类 -- EmpDAOImpl.java
1 package dao.impl; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.util.List; 6 import java.util.Set; 7 8 import dao.IEmpDAO; 9 import vo.Emp; 10 11 public class EmpDAOImpl implements IEmpDAO{ 12 private Connection conn; 13 private PreparedStatement pstmt; 14 15 public EmpDAOImpl(Connection conn) { 16 this.conn = conn; 17 } 18 19 @Override 20 public boolean doCreate(Emp vo) throws Exception { 21 String sql = "INSERT INTO emp(empno,ename,job,hiredate,sal,comm) VALUES (?,?,?,?,?,?)"; 22 this.pstmt = this.conn.prepareStatement(sql); 23 this.pstmt.setInt(1, vo.getEmpno()); 24 this.pstmt.setString(2, vo.getEname()); 25 this.pstmt.setString(3, vo.getJob()); 26 this.pstmt.setDate(4, new java.sql.Date(vo.getHiredate().getTime())); 27 this.pstmt.setDouble(5, vo.getSal()); 28 this.pstmt.setDouble(6, vo.getComm()); 29 // System.out.println("insert success!"); 30 return this.pstmt.executeUpdate() > 0; 31 } 32 33 @Override 34 public boolean doUpdata(Emp vo) throws Exception { 35 return false; 36 } 37 38 @Override 39 public boolean doRemoveBatch(Emp vo) throws Exception { 40 return false; 41 } 42 43 @Override 44 public boolean doRemoveBatch(Set<Integer> ids) 45 throws Exception { 46 return false; 47 } 48 49 @Override 50 public Emp findById(Integer id) throws Exception { 51 return null; 52 } 53 54 @Override 55 public List<Emp> findAll() throws Exception { 56 return null; 57 } 58 59 @Override 60 public List<Emp> findAll(Integer currentPage, 61 Integer LineSize, String column, String keyWord) 62 throws Exception { 63 return null; 64 } 65 66 @Override 67 public Integer getAllCount(String column, 68 String keyWord) throws Exception { 69 return null; 70 } 71 72 }
-
- 数据层工厂类 -- DAOFactory.java
1 package factory; 2 3 import java.sql.Connection; 4 import dao.IEmpDAO; 5 import dao.impl.EmpDAOImpl; 6 7 public class DAOFactory { 8 public static IEmpDAO getIEmpDAOInstance(Connection conn) { 9 return new EmpDAOImpl(conn); 10 } 11 }
- 业务层
- 业务层接口 -- IEmpService.java
1 package service; 2 import vo.Emp; 3 4 public interface IEmpService { 5 public boolean insert(Emp vo) throws Exception; 6 }
-
- 业务层实现类 -- EmpServiceImpl.java
1 package service.impl; 2 3 import dbc.DatabaseConnection; 4 import factory.DAOFactory; 5 import service.IEmpService; 6 import vo.Emp; 7 8 public class EmpServiceImpl implements IEmpService{ 9 private DatabaseConnection dbc = new DatabaseConnection(); 10 11 @Override 12 public boolean insert(Emp vo) throws Exception { 13 try { 14 if(DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).findById(vo.getEmpno()) == null ) { 15 return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).doCreate(vo); 16 } 17 System.out.println("insert false!"); 18 return false; 19 }catch(Exception e) { 20 throw e; 21 }finally { 22 this.dbc.close(); 23 } 24 } 25 }
-
- 业务层工厂类 -- ServiceFactory.java
1 package factory; 2 3 import service.IEmpService; 4 import service.impl.EmpServiceImpl; 5 6 public class ServiceFactory { 7 public static IEmpService getIEmpServiceInstance() { 8 return new EmpServiceImpl(); 9 } 10 }
- 测试类
- main()测试
1 package test; 2 3 import java.util.Date; 4 import factory.ServiceFactory; 5 import vo.Emp; 6 7 public class TestEmpInsert { 8 public static void main(String[] args) throws Exception{ 9 Emp vo = new Emp(); 10 vo.setEmpno(8888); 11 vo.setEname("Lucy"); 12 vo.setJob("Teacher"); 13 vo.setHiredate(new Date()); 14 vo.setSal(8900.0); 15 vo.setComm(5600.0); 16 System.out.println(ServiceFactory.getIEmpServiceInstance().insert(vo)); 17 } 18 }
-
- Junit测试
1 package test.junit; 2 3 import java.util.Date; 4 import org.junit.Test; 5 import factory.ServiceFactory; 6 import junit.framework.TestCase; 7 import vo.Emp; 8 9 public class IEmpServiceTest { 10 11 @Test 12 public void testInsert() throws Exception { 13 Emp vo = new Emp(); 14 vo.setEmpno(8888); 15 vo.setEname("Lucy"); 16 vo.setJob("Teacher"); 17 vo.setHiredate(new Date()); 18 vo.setSal(8900.0); 19 vo.setComm(5600.0); 20 TestCase.assertTrue(ServiceFactory.getIEmpServiceInstance().insert(vo)); 21 } 22 }
参考
B/S与C/S的区别
https://zhidao.baidu.com/question/3711387.html
java中PreparedStatement和Statement详细讲解
https://blog.csdn.net/czh500/article/details/88202971
class.forName()做了什么
以上是关于[Java] DAO设计模式的主要内容,如果未能解决你的问题,请参考以下文章