VO(value object) 值对象
通常用于业务层之间的数据传递,和PO一样也是仅仅包含数据而已。但应是抽象出的业务对象,可以和表对应,也可以不,这根据业务的需要.个人觉得同DTO(数据传输对象),在web上传递。
对于数据库而言,每一个VO对象可以表示出一张表中的一行记录,此类的名称要与表的名称一致。
BO(business object) 业务对象
从业务模型的角度看,见UML元件领域模型中的领域对象。封装业务逻辑的java对象,通过调用DAO方法,结合PO,VO进行业务操作。
PO(persistant object) 持久对象
在o/r映射的时候出现的概念,如果没有o/r映射,没有这个概念存在了。通常对应数据模型(数据库),本身还有部分业务逻辑的处理。可以看成是与数据库中的表相映射的java对象。最简单的PO就是对应数据库中某个表中的一条记录,多个记录可以用PO的集合。PO中应该不包含任何对数据库的操作。
DAO(data access object )数据访问对象
此对象用于访问数据库。通常和PO结合使用,DAO中包含了各种数据库的操作方法。通过它的方法,结合PO对数据库进行相关的操作。
建议命名规则:I表名DAO,如IUserDAO
方法命名规则:更新数据库 doXxx()
查询数据库 findXxx()或getXxx()
POJO(plain ordinary java object) 简单无规则java对象。
DAO设计模式
实现类
实现DAO接口的具体方法,如实现具体CRUD操作。但没有具体的数据库的连接、打开、关闭,具体的操作可以放在代理(Proxy)中,因为这些操作与具体业务无关。
代理(Proxy)
实现具体的操作及数据库的连接开关,并且实现其具体的主题类。
工厂模式(Factory)
有接口就必须有工厂类进行耦合。
注意:在Java开发web页面中,永远不导入java.sql包,只能导入java.util.*和vo包。
下面给出一个具体的例子的代码(来自mldn李新华的教程):
VO:(oracle.vo)
1package org.lxh.oracle.vo;
2
3import java.util.Date;
4
5public class Emp {
6 private int empno ;
7 private String ename ;
8 private String job ;
9 private Date hiredate ;
10 private float sal ;
11 private float comm ;
12 private String photo ;
13 public float getComm() {
14 return comm;
15 }
16 public void setComm(float comm) {
17 this.comm = comm;
18 }
19 public int getEmpno() {
20 return empno;
21 }
22 public void setEmpno(int empno) {
23 this.empno = empno;
24 }
25 public Date getHiredate() {
26 return hiredate;
27 }
28 public void setHiredate(Date hiredate) {
29 this.hiredate = hiredate;
30 }
31 public String getJob() {
32 return job;
33 }
34 public void setJob(String job) {
35 this.job = job;
36 }
37 public String getPhoto() {
38 return photo;
39 }
40 public void setPhoto(String photo) {
41 this.photo = photo;
42 }
43 public float getSal() {
44 return sal;
45 }
46 public void setSal(float sal) {
47 this.sal = sal;
48 }
49 public String getEname() {
50 return ename;
51 }
52 public void setEname(String ename) {
53 this.ename = ename;
54 }
55}
DB的连接及开关
1package org.lxh.oracle.dbc;
2
3import java.sql.* ;
4public class DataBaseConnection{
5 public static final String DBDRIVER =
6
7"oracle.jdbc.driver.OracleDriver" ;
8 public static final String DBURL =
9
10"jdbc:oracle:thin:@localhost:1521:MLDN" ;
11 public static final String DBUSER =
12
13"scott" ;
14 public static final String DBPASS =
15
16"tiger" ;
17 private Connection conn = null ;
18 public DataBaseConnection(){
19 try{
20 Class.forName(DBDRIVER)
21
22;
23 conn =
24
25DriverManager.getConnection
26
27(DBURL,DBUSER,DBPASS) ;
28 }catch(Exception e){
29 e.printStackTrace() ;
30 }
31 }
32 public Connection getConnection(){
33 return this.conn ;
34 }
35 public void close(){
36 if(this.conn!=null){
37 try{
38
39
40this.conn.close() ;
41 }catch(Exception e){}
42 }
43 }
44};
45
DAO类:
1package org.lxh.oracle.dao;
2
3import java.util.List;
4
5import org.lxh.oracle.vo.Emp;
6
7public interface IEmpDAO {
8 public boolean doCreate(Emp emp) throws Exception;
9
10 public boolean doUpdate(Emp emp) throws Exception;
11
12 public boolean doDelete(int empno) throws Exception;
13
14 public Emp findById(int empno) throws Exception;
15
16 public List findAll(int currentPage,int lineSize,String keyWord) throws Exception ;
17
18 public int getAllCount(String keyWord) throws Exception ;
19}
Implements实现类
1package org.lxh.oracle.dao.impl;
2
3import java.sql.Connection;
4import java.sql.PreparedStatement;
5import java.sql.ResultSet;
6import java.util.ArrayList;
7import java.util.List;
8
9import org.lxh.oracle.dao.IEmpDAO;
10import org.lxh.oracle.vo.Emp;
11
12public class IEmpDAOImpl implements IEmpDAO {
13 private Connection conn = null; // 如果要想完成数据库操作则肯定需要数据库连接对象
14 public IEmpDAOImpl(Connection conn){
15 this.conn = conn ; // 由外部实例化时传递连接
16 }
17 public boolean doCreate(Emp emp) throws Exception {
18 boolean flag = false;
19 PreparedStatement pstmt = null;
20 String sql = "INSERT INTO emp(empno,ename,job,hiredate,sal,comm,photo) VALUES (?,?,?,?,?,?,?)";
21 try {
22 pstmt = this.conn.prepareStatement(sql);
23 pstmt.setInt(1, emp.getEmpno());
24 pstmt.setString(2, emp.getEname());
25 pstmt.setString(3, emp.getJob());
26 pstmt.setDate(4, new java.sql.Date(emp.getHiredate().getTime()));
27 pstmt.setFloat(5, emp.getSal());
28 pstmt.setFloat(6, emp.getComm());
29 pstmt.setString(7, emp.getPhoto());
30 int count = pstmt.executeUpdate(); // 执行更新,返回更新的记录数
31 if (count > 0) {
32 flag = true;
33 }
34 } catch (Exception e) {
35 throw e;
36 } finally {
37 try {
38 pstmt.close();
39 } catch (Exception e) {
40 }
41 }
42 return flag;
43 }
44
45 public boolean doDelete(int empno) throws Exception {
46 boolean flag = false;
47 PreparedStatement pstmt = null;
48 String sql = "DELETE FROM emp WHERE empno=?";
49 try {
50 pstmt = this.conn.prepareStatement(sql);
51 pstmt.setInt(1, empno);
52 int count = pstmt.executeUpdate(); // 执行更新,返回更新的记录数
53 if (count > 0) {
54 flag = true;
55 }
56 } catch (Exception e) {
57 throw e;
58 } finally {
59 try {
60 pstmt.close();
61 } catch (Exception e) {
62 }
63 }
64 return flag;
65 }
66
67 public boolean doUpdate(Emp emp) throws Exception {
68 boolean flag = false;
69 PreparedStatement pstmt = null;
70 String sql = "UPDATE emp SET ename=?,job=?,hiredate=?,sal=?,comm=?,photo=? WHERE empno=?";
71 try {
72 pstmt = this.conn.prepareStatement(sql);
73 pstmt.setString(1, emp.getEname());
74 pstmt.setString(2, emp.getJob());
75 pstmt.setDate(3, new java.sql.Date(emp.getHiredate().getTime()));
76 pstmt.setFloat(4, emp.getSal());
77 pstmt.setFloat(5, emp.getComm());
78 pstmt.setString(6, emp.getPhoto());
79 pstmt.setInt(7, emp.getEmpno());
80 int count = pstmt.executeUpdate(); // 执行更新,返回更新的记录数
81 if (count > 0) {
82 flag = true;
83 }
84 } catch (Exception e) {
85 throw e;
86 } finally {
87 try {
88 pstmt.close();
89 } catch (Exception e) {
90 }
91 }
92 return flag;
93 }
94
95 public List findAll(int currentPage, int lineSize, String keyWord)
96 throws Exception {
97 List allEmp = new ArrayList();
98 PreparedStatement pstmt = null;
99 String sql = "SELECT temp.* FROM "
100 + "(SELECT empno,ename,job,hiredate,sal,comm,photo,ROWNUM rn "
101 + " FROM emp "
102 + " WHERE (empno LIKE upper(?) OR ename LIKE upper(?) OR job LIKE upper(?) "
103 + "OR to_char(hiredate,'yyyy') LIKE upper(?) OR to_char(hiredate,'mm') LIKE upper(?)"
104 + " OR to_char(hiredate,'dd') LIKE upper(?) OR sal LIKE upper(?) OR comm LIKE upper(?)) "
105 + " AND ROWNUM<=" + (currentPage * lineSize)
106 + ") temp WHERE temp.rn>" + (currentPage - 1) * lineSize;
107 try {
108 pstmt = this.conn.prepareStatement(sql);
109 pstmt.setString(1, "%" + keyWord + "%");
110 pstmt.setString(2, "%" + keyWord + "%");
111 pstmt.setString(3, "%" + keyWord + "%");
112 pstmt.setString(4, "%" + keyWord + "%");
113 pstmt.setString(5, "%" + keyWord + "%");
114 pstmt.setString(6, "%" + keyWord + "%");
115 pstmt.setString(7, "%" + keyWord + "%");
116 pstmt.setString(8, "%" + keyWord + "%");
117 ResultSet rs = pstmt.executeQuery();
118 Emp emp = null;
119 while (rs.next()) {
120 emp = new Emp();
121 emp.setEmpno(rs.getInt(1));
122 emp.setEname(rs.getString(2));
123 emp.setJob(rs.getString(3));
124 emp.setHiredate(rs.getDate(4));
125 emp.setSal(rs.getFloat(5));
126 emp.setComm(rs.getFloat(6));
127 emp.setPhoto(rs.getString(7));
128 allEmp.add(emp);
129 }
130 rs.close();
131 } catch (Exception e) {
132 throw e;
133 } finally {
134 try {
135 pstmt.close();
136 } catch (Exception e) {
137 }
138 }
139 return allEmp;
140 }
141
142 public Emp findById(int empno) throws Exception {
143 Emp emp = null;
144 PreparedStatement pstmt = null;
145 String sql = "SELECT empno,ename,job,hiredate,sal,comm,photo FROM emp WHERE empno=?";
146 try {
147 pstmt = this.conn.prepareStatement(sql);
148 pstmt.setInt(1, empno);
149 ResultSet rs = pstmt.executeQuery();
150 if (rs.next()) { // 表示已经查找到了
151 emp = new Emp();
152 emp.setEmpno(rs.getInt(1));
153 emp.setEname(rs.getString(2));
154 emp.setJob(rs.getString(3));
155 emp.setHiredate(rs.getDate(4));
156 emp.setSal(rs.getFloat(5));
157 emp.setComm(rs.getFloat(6));
158 emp.setPhoto(rs.getString(7));
159 }
160 rs.close();
161 } catch (Exception e) {
162 throw e;
163 } finally {
164 try {
165 pstmt.close();
166 } catch (Exception e) {
167 }
168 }
169 return emp;
170 }
171
172 public int getAllCount(String keyWord) throws Exception {
173 int count = 0;
174 PreparedStatement pstmt = null;
175 String sql = "SELECT COUNT(empno) FROM emp "
176 + " WHERE empno LIKE upper(?) OR ename LIKE upper(?) OR job LIKE upper(?) "
177 + " OR to_char(hiredate,'yyyy') LIKE upper(?) OR to_char(hiredate,'mm') LIKE upper(?) "
178 + " OR to_char(hiredate,'dd') LIKE upper(?) OR sal LIKE upper(?) OR comm LIKE upper(?)";
179 try {
180 pstmt = this.conn.prepareStatement(sql);
181 pstmt.setString(1,"%"+keyWord+"%") ;
182 pstmt.setString(2,"%"+keyWord+"%") ;
183 pstmt.setString(3,"%"+keyWord+"%") ;
184 pstmt.setString(4,"%"+keyWord+"%") ;
185 pstmt.setString(5,"%"+keyWord+"%") ;
186 pstmt.setString(6,"%"+keyWord+"%") ;
187 pstmt.setString(7,"%"+keyWord+"%") ;
188 pstmt.setString(8,"%"+keyWord+"%") ;
189 ResultSet rs = pstmt.executeQuery();
190 if (rs.next()) { // 表示已经查找到了
191 count = rs.getInt(1) ;
192 }
193 rs.close();
194 } catch (Exception e) {
195 throw e;
196 } finally {
197 try {
198 pstmt.close();
199 } catch (Exception e) {
200 }
201 }
202 return count;
203 }
204
205}
206
Proxy类
1package org.lxh.oracle.dao.proxy;
2
3import java.util.List;
4
5import org.lxh.oracle.dao.IEmpDAO;
6import org.lxh.oracle.dao.impl.IEmpDAOImpl;
7import org.lxh.oracle.dbc.DataBaseConnection;
8import org.lxh.oracle.vo.Emp;
9
10public class IEmpDAOProxy implements IEmpDAO {
11 private DataBaseConnection dbc = null ;
12 private IEmpDAO dao = null ;
13 private int count = 0 ;
14 public IEmpDAOProxy(){
15 this.dbc = new DataBaseConnection() ; // 在代理类中完成数据库连接对象的实例化
16 this.dao = new IEmpDAOImpl(this.dbc.getConnection()) ;
17 }
18 public boolean doCreate(Emp emp) throws Exception {
19 boolean flag = false ;
20 try{
21 if(this.dao.findById(emp.getEmpno())==null){
22 flag = this.dao.doCreate(emp) ; // 调用真实主题实现类
23 }
24 }catch(Exception e){
25 throw e ;
26 }finally{
27 this.dbc.close() ;
28 }
29 return flag;
30 }
31
32 public boolean doDelete(int empno) throws Exception {
33 boolean flag = false ;
34 try{
35 flag = this.dao.doDelete(empno) ; // 调用真实主题实现类
36 }catch(Exception e){
37 throw e ;
38 }finally{
39 this.dbc.close() ;
40 }
41 return flag;
42 }
43
44 public boolean doUpdate(Emp emp) throws Exception {
45 boolean flag = false ;
46 try{
47 flag = this.dao.doUpdate(emp) ; // 调用真实主题实现类
48 }catch(Exception e){
49 throw e ;
50 }finally{
51 this.dbc.close() ;
52 }
53 return flag;
54 }
55
56 public List findAll(int currentPage, int lineSize, String keyWord)
57 throws Exception {
58 List all = null ;
59 try{
60 all = this.dao.findAll(currentPage, lineSize, keyWord) ;
61 this.count = this.dao.getAllCount(keyWord) ; // 查全部记录的时候把全部的记录数也查出来
62 }catch(Exception e){
63 throw e ;
64 }finally{
65 this.dbc.close() ;
66 }
67 return all;
68 }
69
70 public Emp findById(int empno) throws Exception {
71 Emp emp = null ;
72 try{
73 emp = this.dao.findById(empno) ;
74 }catch(Exception e){
75 throw e ;
76 }finally{
77 this.dbc.close() ;
78 }
79 return emp;
80 }
81
82 public int getAllCount(String keyWord) throws Exception {
83 return this.count;
84 }
85
86}
87
工厂Factory类
1package org.lxh.oracle.dao.factory;
2
3import org.lxh.oracle.dao.IEmpDAO;
4import org.lxh.oracle.dao.proxy.IEmpDAOProxy;
5
6public class DAOFactory {
7 public static IEmpDAO getIEmpDAOInstance(){
8 return new IEmpDAOProxy() ;
9 }
10}