理解分層
前臺(tái):顯示層和控制層
后臺(tái)業(yè)務(wù)層:業(yè)務(wù)層和數(shù)據(jù)層,int 飞蹂,double等業(yè)務(wù)層用基本類型几苍,數(shù)據(jù)層用包裝類
項(xiàng)目的核心是后臺(tái)業(yè)務(wù)層,什么是業(yè)務(wù)層陈哑,什么是數(shù)據(jù)層妻坝?
業(yè)務(wù)層是整個(gè)程序提供的功能,而一個(gè)業(yè)務(wù)層的操作要想完成需要多個(gè)數(shù)據(jù)層的操作惊窖,數(shù)據(jù)層完成的只是一個(gè)個(gè)原子性的數(shù)據(jù)庫操作,而我們?cè)趯?shí)際開發(fā)中刽宪, 每一個(gè)操作的業(yè)務(wù)往往需要牽扯到多個(gè)原子性的操作,也就是說所有的原子性的操作業(yè)務(wù)最終在業(yè)務(wù)層只能完成界酒,如果業(yè)務(wù)非常復(fù)雜圣拄,往往需要一個(gè)總業(yè)務(wù)層,而后牽扯到若干個(gè)子業(yè)務(wù)層毁欣,每個(gè)子業(yè)務(wù)層再去操作數(shù)據(jù)層
數(shù)據(jù)層:又被稱為數(shù)據(jù)訪問層 (Data Access Object)DAO庇谆,是專門進(jìn)行數(shù)據(jù)庫的原子性操作,也就是在數(shù)據(jù)層中最需要控制的PreparedStatement接口的使用凭疮。
業(yè)務(wù)層:又被稱為業(yè)務(wù)中心饭耳,業(yè)務(wù)對(duì)象(Business Object,BO),或者叫服務(wù)層(Service),核心的操作是調(diào)用DAO層的操作以完成整體項(xiàng)目的業(yè)務(wù)設(shè)計(jì)
DAO案例
任務(wù):使用emp表(empno,ename,job,hiredate,sal,comm)實(shí)現(xiàn)以下操作功能
- 【業(yè)務(wù)層】實(shí)現(xiàn)雇員的添加哭尝,但是要保證被添加的雇員編號(hào)不重復(fù)哥攘;
|-[數(shù)據(jù)層]判斷要增加的雇員編號(hào)是否存在
|-[數(shù)據(jù)層]如果雇員編號(hào)不存在則進(jìn)行數(shù)據(jù)的保存操作
【業(yè)務(wù)層】實(shí)現(xiàn)雇員的修改操作
|-[數(shù)據(jù)層]執(zhí)行數(shù)據(jù)修改操作【業(yè)務(wù)層】實(shí)現(xiàn)多個(gè)雇員的數(shù)據(jù)刪除操作
|-[數(shù)據(jù)層]執(zhí)行多個(gè)雇員的數(shù)據(jù)刪除操作【業(yè)務(wù)層】可以根據(jù)雇員編號(hào)找到一個(gè)雇員的信息
|-[數(shù)據(jù)層]根據(jù)雇員編號(hào)查詢指定的雇員數(shù)據(jù)【業(yè)務(wù)層】查詢所有雇員的信息
|-[數(shù)據(jù)層]查詢?nèi)康墓蛦T數(shù)據(jù)【業(yè)務(wù)層】實(shí)現(xiàn)數(shù)據(jù)的分頁顯示(模糊查詢)同時(shí)又可以返回所有的雇員信息
|-[數(shù)據(jù)層]雇員數(shù)據(jù)的分頁查詢
|-[數(shù)據(jù)層]使用COUNT()函數(shù),統(tǒng)計(jì)所有雇員的數(shù)量
結(jié)論:用戶所有的需求都是業(yè)務(wù)層,開發(fā)人員必須要根據(jù)業(yè)務(wù)層進(jìn)行數(shù)據(jù)層的數(shù)據(jù)
項(xiàng)目準(zhǔn)備
為了方便項(xiàng)目的管理所有的包的父包設(shè)置為com.pxc逝淹,子包要根據(jù)不同模塊劃分.定義數(shù)據(jù)庫的連接類(DatabaseConnection)專門用于數(shù)據(jù)可以的連接操作耕姊,保存在bdc包中
<pre>package com.pxc.dbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*用于數(shù)據(jù)庫的連接和關(guān)閉操作,在實(shí)例化此類的時(shí)候就意味著數(shù)據(jù)庫的開發(fā)
*所以在此類的構(gòu)造方法中就要進(jìn)行數(shù)據(jù)庫驅(qū)動(dòng)加載和數(shù)據(jù)庫的連接取得
*@author PXC
*
*/
public class DatabaseConnection {
private static final String DBDRIVER="com.mysql.jdbc.Driver";
private static final String DBURL="jdbc:mysql://localhost:3306/mybatis";
private static final String USER="root";
private static final String PWD="935377012";
private Connection conn=null;
/**
* 在構(gòu)造方法里為conn實(shí)例化
*/
public DatabaseConnection(){
try {
Class.forName(DBDRIVER);
try {
this.conn=DriverManager.getConnection(DBURL,USER,PWD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void close(){
if(this.conn!=null){
try {
this.conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Connection getConnection(){
return conn;
}
}</pre>
開發(fā)Value Object
不同層次之間必然要進(jìn)行數(shù)據(jù)的傳遞栅葡,但是既然要操作的是指定的數(shù)據(jù)表茉兰,所以數(shù)據(jù)的結(jié)構(gòu)要與表的結(jié)構(gòu)一一對(duì)應(yīng),那么自然就想到是簡(jiǎn)單java類(po,to,pojo,vo)
簡(jiǎn)單java類有如下的要求
考慮到以后有可能出現(xiàn)的分布式應(yīng)用問題欣簇,所以簡(jiǎn)單java類必須要實(shí)現(xiàn)java.io.Serializable接口规脸;
簡(jiǎn)單java類的名稱必須與表名稱保持一致。表名稱如果是這種stydent_info類名稱為StudentInfo
3.類中的字段必須使用private封裝熊咽,封裝好的屬性必須提供get和set方法
類中的屬性不允許使用基本數(shù)據(jù)類型莫鸭,都必須使用基本數(shù)據(jù)類型的包裝類。原因是基本數(shù)據(jù)類型的默認(rèn)值是0横殴,而包裝類的默認(rèn)數(shù)據(jù)類型是null
類中必須保留無參構(gòu)造方法
將所有的簡(jiǎn)單java類保存在vo包中
開發(fā)數(shù)據(jù)層操作標(biāo)準(zhǔn)
<pre>
package com.pxc.dao;
import java.security.PublicKey;
import java.util.List;
import java.util.Set;
import com.pxc.vo.Emp;
/***
- 定義emp表的數(shù)據(jù)層的操作標(biāo)準(zhǔn)
- @author PXC
/
public interface IEmpDAO {
/**
* 實(shí)現(xiàn)數(shù)據(jù)的增加操作
* @param emp
* @return 數(shù)據(jù)保存成功返回true
* @throws Exception sql執(zhí)行異常
/
public boolean doCreate(Emp emp)throws Exception;//簡(jiǎn)化開發(fā)被因,嚴(yán)格的話一定要挨個(gè)拋
/**
* 實(shí)現(xiàn)數(shù)據(jù)的修改操作,本次修改是根據(jù)id進(jìn)行全部字段的修改
* @param emp 包含了壓迫修改的信息衫仑,一定要提供id
* @return 修改成功返回true
* @throws Exception sql執(zhí)行異常
/
public boolean doUpdate(Emp emp)throws Exception;
/*
* 執(zhí)行數(shù)據(jù)的批量數(shù)據(jù)的操作梨与,所有要?jiǎng)h除的數(shù)據(jù)以Set集合形式保存
* @param ids 包含了所有要?jiǎng)h除的數(shù)據(jù)ID,不包含重復(fù)內(nèi)容
* @return 刪除成功返回true(刪除的數(shù)據(jù)個(gè)數(shù)和要?jiǎng)h除的數(shù)據(jù)個(gè)數(shù)相同)
* @throws Exception
/
public boolean doRemoveBatch(Set ids) throws Exception;
/**
* 根據(jù)編號(hào)查詢指定的雇員信息
* @param id 要查詢的雇員編號(hào)
* @return 如果雇員信息存在文狱,則將數(shù)據(jù)以pojo返回粥鞋,如果不存在則返回null
* @throws Exception
/
public Emp findById(Integer id)throws Exception;
/*
* 查詢指定表的全部記錄,并且以集合的形式返回
* @return 如果表中有數(shù)據(jù)瞄崇,則所有的數(shù)據(jù)都會(huì)被封裝為pojo對(duì)象而后利用List集合返回呻粹,
* 如果沒有數(shù)據(jù)則集合的長度為0,不是返回null
* @throws Exception SQL執(zhí)行異常
/
public List findAll() throws Exception;
/**
* 分頁進(jìn)行數(shù)據(jù)的模糊查詢苏研,查詢的結(jié)果以集合的形式返回
* @param currentPage 當(dāng)前所在的頁
* @param lineSize 每頁顯示的行數(shù)
* @param column 要進(jìn)行模糊查詢的數(shù)據(jù)列
* @param keyWord 模糊查詢的關(guān)鍵字
* @return 如果表中有數(shù)據(jù)尚猿,則所有的數(shù)據(jù)都會(huì)被封裝為pojo對(duì)象而后利用List集合返回,
* 如果沒有數(shù)據(jù)則集合的長度為0楣富,不是返回null
* @throws Exception SQL執(zhí)行異常
/
public List findAllSplit(Integer currentPage,Integer lineSize,String column,String keyWord)throws Exception;
/**
* 進(jìn)行模糊查詢數(shù)據(jù)量的統(tǒng)計(jì)
* @param column 要進(jìn)行模糊查詢的數(shù)據(jù)列
* @param keyWord 模糊查詢的關(guān)鍵字
* @return 返回表中的數(shù)據(jù)量,沒有數(shù)據(jù)返回0
* @throws Exception SQL執(zhí)行異常
*/
public Integer getAllCount(String column, String keyWord)throws Exception;
}
</pre>
數(shù)據(jù)層的實(shí)現(xiàn)類
數(shù)據(jù)層要被業(yè)務(wù)層調(diào)用伴榔,數(shù)據(jù)層要進(jìn)行數(shù)據(jù)庫的操作纹蝴,由于在開發(fā)中一個(gè)業(yè)務(wù)層操作需要執(zhí)行多個(gè)數(shù)據(jù)層的操作,所以數(shù)據(jù)庫的打開和關(guān)閉操作應(yīng)該由業(yè)務(wù)層操作踪少。
所有的數(shù)據(jù)層數(shù)顯類要保存在dao.impl子包下
范例:EmpDAOImpl子類
<pre>
package com.pxc.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.pxc.dao.IEmpDAO;
import com.pxc.vo.Emp;
public class EmpDAOImpl implements IEmpDAO {
private Connection conn;//需要利用connection對(duì)象
private PreparedStatement pstmt;
/**
* 如果要使用數(shù)據(jù)層進(jìn)行原子性功能的操作實(shí)現(xiàn)塘安,必須提供Connection接口對(duì)象
* 另外業(yè)務(wù)層要調(diào)用數(shù)據(jù)層,所以數(shù)據(jù)庫的打開和關(guān)閉交給業(yè)務(wù)層處理
* @param conn 數(shù)據(jù)庫的連接對(duì)象
*/
public EmpDAOImpl(Connection conn){
this.conn=conn;
}
@Override
public boolean doCreate(Emp emp) throws Exception {
String sql="insert into emp(empno,ename,job,hiredate,sal,comn) values(?,?,?,?,?,?)";
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setInt(1, emp.getEmpno());
this.pstmt.setString(2, emp.getEname());
this.pstmt.setString(3, emp.getJob());
this.pstmt.setDate(4, new java.sql.Date(emp.getHiredate().getTime()));
this.pstmt.setDouble(5, emp.getSal());
this.pstmt.setDouble(6, emp.getComm());
return this.pstmt.executeUpdate()>0;
}
@Override
public boolean doUpdate(Emp emp) throws Exception {
String sql="update emp set ename=?,job=?,hiredate=?,sal=?,comn=? where empno=?";
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1, emp.getEname());
this.pstmt.setString(2, emp.getJob());
this.pstmt.setDate(3, new java.sql.Date(emp.getHiredate().getTime()));
this.pstmt.setDouble(4, emp.getSal());
this.pstmt.setDouble(5, emp.getComm());
this.pstmt.setInt(6, emp.getEmpno());
return this.pstmt.executeUpdate()>0;
}
@Override
public boolean doRemoveBatch(Set ids) throws Exception {
if(ids==null||ids.size()==0){//沒有要?jiǎng)h除的數(shù)據(jù)
return false;
}
StringBuffer sql=new StringBuffer();
sql.append("delete from emp where empno in(");
Iterator iter=ids.iterator();
while(iter.hasNext()){
sql.append(iter.next()+",");
}
sql.delete(sql.length()-1, sql.length()).append(")");
this.pstmt=this.conn.prepareStatement(sql.toString());
return this.pstmt.executeUpdate()==ids.size();
}
@Override
public Emp findById(Integer id) throws Exception {
Emp emp=null;
String sql="select * from emp where empno=?";
this.pstmt=this.conn.prepareStatement(sql);
pstmt.setInt(1,id);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
emp.setEmpno(rs.getInt(1));
emp.setEname(rs.getString(2));
emp.setJob(rs.getString(3));
emp.setHiredate(rs.getDate(4));
emp.setSal(rs.getDouble(5));
emp.setComm(rs.getDouble(6));
}
return emp;
}
@Override
public List findAll() throws Exception {
List all =new ArrayList();
String sql="select * from emp";
this.pstmt=this.conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
Emp emp=new Emp();
emp.setEmpno(rs.getInt(1));
emp.setEname(rs.getString(2));
emp.setJob(rs.getString(3));
emp.setHiredate(rs.getDate(4));
emp.setSal(rs.getDouble(5));
emp.setComm(rs.getDouble(6));
all.add(emp);
}
return all;
}
@Override
public List findAllSplit(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 {
String sql="select count(*) from emp where "+column+"like ?";
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1,"%"+keyWord+"%");
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
return rs.getInt(1);
}
return 0;
}
}</pre>
定義數(shù)據(jù)層工廠類
<pre>package com.pxc.factory;
import java.sql.Connection;
import com.pxc.dao.IDeptDAO;
import com.pxc.dao.IEmpDAO;
import com.pxc.dao.impl.DeptDAOImpl;
import com.pxc.dao.impl.EmpDAOImpl;
public class DAOFactory {
public static IEmpDAO getIEmpDAOInstance(Connection conn){
return new EmpDAOImpl(conn);
}
}</pre>
定義業(yè)務(wù)層執(zhí)行標(biāo)準(zhǔn)
<pre>package com.pxc.service;
import java.util.Set;
import java.util.List;
import java.util.Map;
import com.pxc.vo.Emp;
/***
* 定義emp表的業(yè)務(wù)層的執(zhí)行標(biāo)準(zhǔn)援奢,此類一定要負(fù)責(zé)數(shù)據(jù)庫的打開和關(guān)閉操作
*此類可以通過DAOFactory類取得IEmpDAO的接口對(duì)象
* @author PXC
*
*/
public interface IEmpService {
/***
* 實(shí)現(xiàn)雇員數(shù)據(jù)的增加操作兼犯,本次操作要調(diào)用IEmpDAO接口的如下方法
* 需要調(diào)用IEmpDAO.findById()方法,判斷是否已經(jīng)存在
* 如果現(xiàn)在要增加的數(shù)據(jù)編號(hào)不存在則調(diào)用IEmpDAO.doCreate()方法,返回操作的結(jié)果
* @param emp 包含了要增加數(shù)據(jù)的pojo對(duì)象
* @return 如果增加數(shù)據(jù)的ID重復(fù)或者保存失敗返回false,否則返回true
* @throws Exception SQL切黔、執(zhí)行異常
*/
public boolean insert(Emp emp)throws Exception;
/***
* 實(shí)現(xiàn)雇員的修改操作砸脊,本次調(diào)用的是IEmpDAO.doupdate()方法,本次修改屬于全部內(nèi)容修改
* @param emp 包含了要增加數(shù)據(jù)的pojo對(duì)象
* @return 修改成功返回true否則趕回false
* @throws Exception SQL執(zhí)行異常
*/
public boolean update(Emp emp) throws Exception;
/**
* 執(zhí)行雇員數(shù)據(jù)的刪除纬霞,刪除多個(gè)雇員信息凌埂,調(diào)用IEmpDAo.doRemoveBatch()方法
* @param ids 包含了要?jiǎng)h除數(shù)據(jù)的集合,沒有重復(fù)數(shù)據(jù)
* @return 刪除成功返回true否則趕回false
* @throws Exception SQL執(zhí)行異常
*/
public boolean delete(Set<Integer> ids) throws Exception;
/**
* 根據(jù)雇員編號(hào)查找雇員完整信息诗芜,調(diào)用IEmpDAO.findById()方法
* @param ids 要查找的雇員編號(hào)
* @return 如果找到雇員信息則以pojo返回瞳抓,否則返回null
* @throws Exception SQL執(zhí)行異常
*/
public Emp get(Integer ids)throws Exception;
/**
* 查詢?nèi)抗蛦T信息,調(diào)用的是IEmpDAO中的findAll()方法
* @return 查詢結(jié)果以List集合形式返回伏恐。如果沒有結(jié)果則返回0孩哑。
* @throws Exception SQL執(zhí)行異常
*/
public List<Emp> list()throws Exception;
/**
* 實(shí)現(xiàn)數(shù)據(jù)的模糊查詢和數(shù)據(jù)的統(tǒng)計(jì),調(diào)用
* IEmpDAO.findAllSplit()方法,返回List集合數(shù)據(jù)
* IEmpDAO.getAllCount()方法,返回的是integer類型
* @param currentPage 當(dāng)前所在頁
* @param lineSize 每頁顯示的記錄數(shù)
* @param column 模糊查詢的數(shù)據(jù)列
* @param keyWord 模糊查詢的關(guān)鍵字
* @return 本方法要返回多種數(shù)據(jù)類型翠桦,所以使用Map結(jié)婚横蜒,由于類型不統(tǒng)一,所有value的類型設(shè)置為Object秤掌,返回內(nèi)容如下
* key=allEmps愁铺,value=IEmpDAO.findAllSplit()返回結(jié)果,LIst集合
* key=empCOunt,value=IEmpDAO.getAllCount()返回結(jié)果闻鉴,Integer
* @throws Exception
*/
public Map<String,Object> List(int currentPage,int lineSize,String column,String keyWord) throws Exception;
}</pre>
業(yè)務(wù)層的實(shí)現(xiàn)類
業(yè)務(wù)層實(shí)現(xiàn)類的核心功能
負(fù)責(zé)控制數(shù)據(jù)庫的打開和關(guān)閉茵乱。業(yè)務(wù)層對(duì)象實(shí)例化之后必須準(zhǔn)備好數(shù)據(jù)庫的連接
-
根據(jù)DAOFactory調(diào)用getIEmpDAO Instance()方法而后取得IEmpDAO接口對(duì)象,業(yè)務(wù)層的實(shí)現(xiàn)類保存在dao.impl子包中
<pre>
package com.pxc.service.impl;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.pxc.dbc.DatabaseConnection;
import com.pxc.factory.DAOFactory;
import com.pxc.service.IEmpService;
import com.pxc.vo.Emp;
public class EmpServiceImpl implements IEmpService {
// 在這個(gè)類的內(nèi)部就提供一個(gè)數(shù)據(jù)庫實(shí)例化對(duì)象
private DatabaseConnection dbc = new DatabaseConnection();
@Override
public boolean insert(Emp emp) throws Exception {
// TODO Auto-generated method stub
try {
//要增加的雇員編號(hào)不存在孟岛,findById()返回的結(jié)果接受null瓶竭,null表示可以進(jìn)行新雇員信息的保存
if(DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).findById(emp.getEmpno())==null){
return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).doCreate(emp);
}
return false;
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}}
@Override
public boolean update(Emp emp) throws Exception {
// TODO Auto-generated method stub
try {
return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).doUpdate(emp);
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
}@Override
public boolean delete(Set ids) throws Exception {
try {
return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).doRemoveBatch(ids);
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
}
@Override
public Emp get(Integer id) throws Exception {
try {
return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).findById(id);
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
}
@Override
public java.util.List list() throws Exception {
try {
return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).findAll();
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
}
@Override
public Map<String, Object> List(int currentPage, int lineSize, String column, String keyWord) throws Exception {
try {
Map<String, Object> map=new HashMap<String, Object>();
map.put("allEmps", DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).findAllSplit(currentPage, lineSize, column, keyWord));
map.put("empCount", DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).getAllCount(column, keyWord));
return map;
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
}
}</pre>
不同層的訪問就是依靠的工廠類和接口進(jìn)行操作编兄,寫了接口索绪,取得對(duì)象就用工廠類
定義業(yè)務(wù)層的工廠類
業(yè)務(wù)層要被控制層調(diào)用,所以要為業(yè)務(wù)層定義工廠類殖侵,該類也保存在factory子包下次询,業(yè)務(wù)層應(yīng)該分為兩種
- 前臺(tái)業(yè)務(wù)邏輯:可以將其保存在service.front包中荧恍,工廠類:serviceFrontFactory
- 后臺(tái)業(yè)務(wù)邏輯:可以將其保存在service.back包中,工廠類:serviceBackFactory
范例定義serviceFactory(本次不區(qū)分前后臺(tái))
<pre>package com.pxc.factory;
import com.pxc.service.IEmpService;
import com.pxc.service.impl.EmpServiceImpl;
public class ServiceFactory {
public static IEmpService getIeEmpServiceInstance(){
return new EmpServiceImpl();
}
}</pre>
在實(shí)際編寫中屯吊,子類永遠(yuǎn)是不可見的送巡,控制層完全看不見數(shù)據(jù)庫的任何操作。
代碼測(cè)試
最終的業(yè)務(wù)層是需要用戶去調(diào)用的盒卸,所以測(cè)試分為兩種骗爆。
調(diào)用測(cè)試
按照傳統(tǒng)方式產(chǎn)生對(duì)象,而后調(diào)用里面的方式操作蔽介,保存在test子包類
測(cè)試案例:
<pre>package com.pxc.test;
import java.util.Date;
import com.pxc.factory.ServiceFactory;
import com.pxc.vo.Emp;
public class TestEmpInsert {
public static void main(String[] args) {
Emp emp=new Emp();
emp.setEmpno(456);
emp.setEname("pxc");
emp.setJob("IT");
emp.setHiredate(new Date());
emp.setSal(0.5);
emp.setComm(10.0);
try {
System.out.println(ServiceFactory.getIeEmpServiceInstance().insert(emp));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
}</pre>-
利用junit進(jìn)行測(cè)試
對(duì)于這種業(yè)務(wù)的測(cè)試使用junit是最好的選擇摘投,首先要選擇類或者接口進(jìn)行測(cè)試煮寡,本次使用IEmpService
<pre>package com.pxc.test.junit;
import static org.junit.Assert.*;
import java.util.Date;
import java.util.Random;
import org.junit.Test;
import com.pxc.factory.ServiceFactory;
import com.pxc.vo.Emp;
import junit.framework.TestCase;
public class IEmpServiceTest {
private static int empno ;
static{
empno=new Random().nextInt(10000);//動(dòng)態(tài)生成empno 的數(shù)據(jù)
}
@Test
public void testInsert() {
Emp emp = new Emp();
emp.setEmpno(empno);
emp.setEname("pxc2"+empno);
emp.setJob("IT"+empno);
emp.setHiredate(new Date());
emp.setSal(0.5);
emp.setComm(10.0);
try {
TestCase.assertTrue(ServiceFactory.getIeEmpServiceInstance().insert(emp));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
@Test
public void testUpdate() {
Emp emp=new Emp();
emp.setEmpno(2);
emp.setEname("pxc5");
emp.setJob("IT");
emp.setHiredate(new Date());
emp.setSal(0.5);
emp.setComm(10.0);
try {
TestCase.assertTrue(ServiceFactory.getIeEmpServiceInstance().update(emp));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
@Test
public void testList() {
try {
TestCase.assertTrue(ServiceFactory.getIeEmpServiceInstance().list().size()>0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
</pre>
以上就是我在學(xué)習(xí)DAO時(shí)做的筆記,本次只涉及到單表查詢犀呼,等熟練運(yùn)用單表查詢后幸撕,再去考慮多表查詢。