javadao设计
DAO是Data Access Object数据访问接口。
首先我们先创建一个包命名为dbc,此包下有连接不同数据库的类。
此处我们连接的是MySQL,所以我们在此包下创建一个操作MySQL数据库的类。
- 数据库连接
package dbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author 达达尼昂 本类负责数据库的连接与关闭操作 在本类的构造方法中应进行数据库驱动加载和数据库连接
*/
public class DatabaseConnection {
private static final String MySqlDriver = "com.mysql.cj.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/db1";
private static final String username = "root";
private static final String password = "123456";
private Connection con = null;
/**
* 此构造方法里面为con对象进行实例化 即获得连接
*/
public DatabaseConnection() {
try {
Class.forName(MySqlDriver);
this.con = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @return 返回Connection实例化对象
*/
public Connection getConnection() {
return this.con;
}
/**
* 负责数据库连接的关闭
*/
public void close() {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- 简单java类
接着我们创建一个命名为vo(value object)的包,在此包下创建简单java类,即对应数据库中的每一张表,类中只有字段和n多的get,set方法。
package vo;
import java.io.Serializable;
import java.util.Date;
/**
* @author 达达尼昂 此类为简单java类,即对应数据库中一张表的类,所有的简单java类都在vo包中 此类要求:
* 1.简单java类必须实现Serializable接口 2.简单java类的名称必须与表名相同 3.类中的基本数据类型属性使用包装类
* 4.必须保留一个无参构造方法
*/
public class Emp implements Serializable {
private Integer empno;
private String ename;
private String job;
private Date hireday;
private Double sal;
private Double comm;
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer 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 Date getHireday() {
return hireday;
}
public void setHireday(Date hireday) {
this.hireday = hireday;
}
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;
}
}
我们看到此类实现了Serializable接口,为什么呢?当你想把的内存中的对象状态保存到一个文件中或者数据库中时候,需要序列化。因为vo包下的类中字段如姓名,性别,都存储在堆内存中,一旦程序执行完毕,这些字段的值就不再存在,所以我们就需要对这个类进行序列化,便于传送或保存。
- 序列化:把对象转换为字节序列的过程称为对象的序列化。
- 反序列化:把字节序列恢复为对象的过程称为对象的反序列化。
- 数据层标准(接口)
接着我们创建dao设计的核心,即dao接口(数据层接口),我们创建一个dao包,此包下存在数据层开发的接口。对每个不同表,我们有不同的要求,所以我们创建对应不同表的接口,命名为IXxxDAO的格式。
package dao;
import java.util.List;
import java.util.Set;
import vo.Emp;
/**
* @author 达达尼昂 此类为开发数据层的接口 整个数据层的开发只有两类功能 数据更新:--以doXxx()方式命名,例doCreate 数据查询:
* 查询表中的数据--findXxx() 统计表中的数据--getXxx()
*/
/**
* @author 达达尼昂
* 数据层既然是进行数据操作的,那么就将其保存在dao包下
* 不同的数据表的操作有可能使用不同的数据层开发,那么就针对于数据表进行命名
* eg:empIEmpDAO
*
*/
public interface IEmpDAO {
/**
* 实现数据的增加操作
*
* @param vo 包含了要增加数据的VO对象
* @return 数据保存成功返回true,失败则false
* @throws Exception
*/
public boolean doCreate(Emp vo) throws Exception;
/**
* 实现对表的更新操作,根据id进行
*
* @param vo
* @return
* @throws Exception
*/
public boolean doUpdate(Emp vo) throws Exception;
/**
* 执行数据的批量删除操作 要删除的数据编号保存在set集合中
*
* @return
* @throws Exception
*/
public boolean doRemoveBatch(Set<Integer> ids) throws Exception;
/**
* 根据编号查询指定雇员信息
*
* @param id 要查询的雇员编号
* @return 返回雇员信息
* @throws Exception
*/
public Emp findById(Integer id) throws Exception;
/**
* 查询指定数据表的全部记录,以集合的方式返回
*
* @return 如果表中有数据,则所有对象会封装为VO对象利用List集合返回 如果没有数据,则集合的长度为0,并非为null
* @throws Exception
*/
public List<Emp> findAll() throws Exception;
/**
* 分页进行数据的模糊查询,查询结果以集合的方式返回
*
* @param currentPage 表示当前所在页
* @param linesize 每页显示的数据行数
* @param column 要进行模糊查询的数据列
* @param keyword 模糊查询的关键字
* @return
* @throws Exception
*/
public List<Emp> findAllSpilt(Integer currentPage, Integer linesize, String column, String keyword)
throws Exception;
/**
* 进行模糊查询数据量的统计
*
* @param column 要进行模糊查询的数据列
* @param keyword 模糊查询的关键字
* @return
* @throws Exception
*/
public Integer getAllCount(String column, String keyword) throws Exception;
}
- 数据层实现类
所有的数据层实现类要求保存在dao.impl子包下。
对数据的增删改查存在于此类中。
package dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import dao.IEmpDAO;
import vo.Emp;
public class EmpDAOImpl implements IEmpDAO {
private Connection con;
private PreparedStatement ps;
/**
* 如果要向使用数据层进行原子性的功能操作,必须要提供有Connection接口对象 由于开发中,是业务层要调用数据层,所以数据库的打开与关闭交由业务层管理
*
* @param con
*/
public EmpDAOImpl(Connection con) {
this.con = con;
}
@Override
public boolean doCreate(Emp vo) throws Exception {
String sql = "insert into emp(empno,ename,job,hireday,sal,comm)values(?,?,?,?,?,?)";
this.ps = this.con.prepareStatement(sql);
this.ps.setInt(1, vo.getEmpno());
this.ps.setString(2, vo.getEname());
this.ps.setString(3, vo.getJob());
this.ps.setDate(4, new java.sql.Date(vo.getHireday().getTime()));
this.ps.setDouble(5, vo.getSal());
this.ps.setDouble(6, vo.getComm());
return this.ps.executeUpdate() > 0;
}
@Override
public boolean doUpdate(Emp vo) throws Exception {
String sql = "update emp set ename=?,job=?,hireday=?,sal=?,com=?where empno=?";
this.ps.setString(1, vo.getEname());
this.ps.setString(2, vo.getJob());
this.ps.setDate(3, new java.sql.Date(vo.getHireday().getTime()));
this.ps.setDouble(4, vo.getSal());
this.ps.setDouble(5, vo.getComm());
this.ps.setInt(6, vo.getEmpno());
return false;
}
@Override
public boolean doRemoveBatch(Set<Integer> ids) throws Exception {
// 没有要删除的数据
if (ids == null || ids.size() == 0) {
return false;
}
StringBuffer sql = new StringBuffer();
sql.append("delete from emp where empno in(");
Iterator<Integer> iter = ids.iterator();
while (iter.hasNext()) {
sql.append(iter.next()).append(",");
}
sql.delete(sql.length() - 1, sql.length()).append(")");
// prepaerStatement只接受String类型的参数,使用toString进行转化
this.ps = this.con.prepareStatement(sql.toString());
return this.ps.executeUpdate() == ids.size();
}
@Override
public Emp findById(Integer id) throws Exception {
Emp vo = null;
String sql = "select empno,ename,job,hireday,sal,comm from emp where empno=?";
this.ps = con.prepareStatement(sql);
this.ps.setInt(1, id);
ResultSet rs = this.ps.executeQuery();
if (rs.next()) {
vo = new Emp();
vo.setEmpno(rs.getInt(1));
vo.setEname(rs.getString(2));
vo.setJob(rs.getString(3));
vo.setHireday(rs.getDate(4));
vo.setSal(rs.getDouble(5));
vo.setComm(rs.getDouble(6));
}
return vo;
}
@Override
public List<Emp> findAll() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Emp> findAllSpilt(Integer currentPage, Integer linesize, String column, String keyword)
throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public Integer getAllCount(String column, String keyword) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
- 数据层工厂类
创建一个Factoty包,此包下创建一个数据层工厂类,即daoFactory。
package factory;
import java.sql.Connection;
import dao.IEmpDAO;
import dao.impl.EmpDAOImpl;
public class DAOFactory {
/**需要传入connection
* @param con
* @return 返回一个EmpDAOImpl实例
*/
public static IEmpDAO getIEmpDAOInstance(Connection con) {
return new EmpDAOImpl(con);
}
}
- 业务层标准(接口)
package service;
import java.util.List;
import java.util.Set;
import vo.Emp;
public interface IEmpService {
/**
* 实现雇员数据的增加操作,要调用IEmpDAO下的findById方法 如果要增加的数据编号不存在,则调用Create方法
*
* @param vo
* @return
* @throws Exception
*/
public boolean insert(Emp vo) throws Exception;
/**
* 调用IEmpDAO下的doUpdate方法
*
* @param vo
* @return
* @throws Exception
*/
public boolean update(Emp vo) throws Exception;
public boolean delete(Set<Integer> ids) throws Exception;
public Emp get(Integer ids) throws Exception;
public List<Emp> list(Emp vo) throws Exception;
}
- 业务层实现类
package serviceImpl;
import java.util.List;
import java.util.Set;
import dbc.DatabaseConnection;
import factory.DAOFactory;
import service.IEmpService;
import vo.Emp;
/**
* @author 达达尼昂
*
*/
public class EmpServiceImpl implements IEmpService {
// 这个类的对象内部就提供有一个数据库连接类的实例化对象
private DatabaseConnection dbc = new DatabaseConnection();
@Override
public boolean insert(Emp vo) throws Exception {
try {
// 要添加的雇员编号不存在,则find返回null,null表示可以进行新雇员数据的保存
if (DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).findById(vo.getEmpno())== null) {
return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).doCreate(vo);
}
return false;
} catch (Exception e) {
e.printStackTrace();
} finally {
this.dbc.close();
}
return false;
}
@Override
public boolean update(Emp vo) throws Exception {
try {
return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).doUpdate(vo);
} catch (Exception e) {
e.printStackTrace();
} finally {
this.dbc.close();
}
return false;
}
@Override
public boolean delete(Set<Integer> ids) throws Exception {
try {
return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).doRemoveBatch(ids);
} catch (Exception e) {
e.printStackTrace();
} finally {
this.dbc.close();
}
return false;
}
@Override
public Emp get(Integer ids) throws Exception {
try {
return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).findById(ids);
} catch (Exception e) {
e.printStackTrace();
} finally {
this.dbc.close();
}
return null;
}
@Override
public List<Emp> list(Emp vo) throws Exception {
try {
return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).findAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
this.dbc.close();
}
return null;
}
}
- 业务层工厂类
package factory;
import service.IEmpService;
import serviceImpl.EmpServiceImpl;
public class ServiceFactory {
public static IEmpService getIEmpServiceInstance() {
return new EmpServiceImpl();
}
}
一张思维导图: