DAO设计模式实例!
现在是我第二次写技术博客了,上次写了MVC,今天我将对DAO做出一个全面说明,也希望各位高手发现了我的说明错误,能够及时提出,不要再潜水了哦!
用的开发工具不用说,大部分程序员都熟悉的 MyEclipse 8.6
数据库:mysql
mysql数据库的设计:
项目名称: test
我所建立的包如下:
注意命名的规范哦!
1.dao接口类
2.factory工厂类
3.impl真实类
4.proxy代理类
5.数据库连接类
6.vo类
7.JUnit测试类
对了别忘了引入驱动包!
接下来解析一下代码:
1.创建vo类!
package org.yzsoft.test.vo;
public class Tstudent {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudentno() {
return studentno;
}
public void setStudentno(String studentno) {
this.studentno = studentno;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getNativeplace() {
return nativeplace;
}
public void setNativeplace(String nativeplace) {
this.nativeplace = nativeplace;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
private String studentno;
private String classname;
private String sex;
private String age;
private String nativeplace;
private String note;
}
2.接下来是数据库连接类
package org.yzsoft.test.util ;
import java.sql.Connection ;
import java.sql.DriverManager ;
public class DatabaseConnection {
private static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
private static final String DBURL = "jdbc:mysql://localhost:3306/db_testdao" ;
private static final String DBUSER = "root" ;
private static final String DBPASSWORD = "admin" ;
private Connection conn ;
public DatabaseConnection() throws Exception {
Class.forName(DBDRIVER) ;
this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
}
public Connection getConnection(){
return this.conn ;
}
public void close() throws Exception {
if(this.conn != null){
try{
this.conn.close() ;
}catch(Exception e){
throw e ;
}
}
}
}
3.然后写DAO接口!
package org.yzsoft.test.dao;
import java.util.List;
import org.yzsoft.test.vo.Tstudent;
public interface ITstudentDAO {
public boolean doCreate(Tstudent tstudent) throws Exception ;
public List<Tstudent> findAll(String keyWord) throws Exception ;
public Tstudent findById(int id) throws Exception ;
public boolean doUpdate(Tstudent tstudent) throws Exception ;
public boolean doDelete(int id) throws Exception ;
}
4.然后是坑爹的真实类!———这是系统的核心!要注意了!
package org.yzsoft.test.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.yzsoft.test.dao.ITstudentDAO;
import org.yzsoft.test.vo.Tstudent;
public class TstudentDAOImpl implements ITstudentDAO {
private Connection conn = null ;
private PreparedStatement pstmt = null ;
public TstudentDAOImpl(Connection conn){
this.conn = conn ;
}
public boolean doCreate(Tstudent tstudent) throws Exception {
boolean flag = false ;
String sql = "INSERT INTO t_student(studentno,classname,sex,age,nativeplace,note) VALUES (?,?,?,?,?,?)" ;
this.pstmt = this.conn.prepareStatement(sql) ;
this.pstmt.setString(1,tstudent.getStudentno()) ;
this.pstmt.setString(2,tstudent.getClassname()) ;
this.pstmt.setString(3,tstudent.getSex()) ;
this.pstmt.setString(4,tstudent.getAge()) ;
this.pstmt.setString(5,tstudent.getNativeplace()) ;
this.pstmt.setString(6,tstudent.getNote()) ;
if(this.pstmt.executeUpdate() > 0){
flag = true ;
}
this.pstmt.close() ;
return flag ;
}
public boolean doDelete(int id) throws Exception {
boolean flag = false ;
String sql = "delete from t_student where id=?" ;
this.pstmt = this.conn.prepareStatement(sql) ;
this.pstmt.setInt(1,id) ;
if(this.pstmt.executeUpdate() > 0){
flag = true ;
}
this.pstmt.close() ;
return flag ;
}
public boolean doUpdate(Tstudent tstudent) throws Exception {
boolean flag = false ;
String sql = "update t_student set studentno=?,classname=?,sex=?,age=?,nativeplace=?,note=? where id=?" ;
this.pstmt = this.conn.prepareStatement(sql) ;
this.pstmt.setInt(7,tstudent.getId()) ;
this.pstmt.setString(1,tstudent.getStudentno()) ;
this.pstmt.setString(2,tstudent.getClassname()) ;
this.pstmt.setString(3,tstudent.getSex()) ;
this.pstmt.setString(4,tstudent.getAge()) ;
this.pstmt.setString(5,tstudent.getNativeplace()) ;
this.pstmt.setString(6,tstudent.getNote()) ;
if(this.pstmt.executeUpdate() > 0){
flag = true ;
}
this.pstmt.close() ;
return flag ;
}
public List<Tstudent> findAll(String keyWord) throws Exception {
List<Tstudent> all = new ArrayList<Tstudent>() ;
String sql = "SELECT id,studentno,classname,sex,age,nativeplace,note FROM t_student WHERE id LIKE ? OR studentno LIKE ? OR classname LIKE ?OR sex LIKE ?OR age LIKE ?OR nativeplace LIKE ?OR note LIKE ?" ;
this.pstmt = this.conn.prepareStatement(sql) ;
this.pstmt.setString(1,"%"+keyWord+"%") ;
this.pstmt.setString(2,"%"+keyWord+"%") ;
this.pstmt.setString(3,"%"+keyWord+"%") ;
this.pstmt.setString(4,"%"+keyWord+"%") ;
this.pstmt.setString(5,"%"+keyWord+"%") ;
this.pstmt.setString(6,"%"+keyWord+"%") ;
this.pstmt.setString(7,"%"+keyWord+"%") ;
ResultSet rs = this.pstmt.executeQuery() ;
Tstudent tstudent = null ;
while(rs.next()){
tstudent = new Tstudent() ;
tstudent = new Tstudent() ;
tstudent.setId(rs.getInt(1)) ;
tstudent.setStudentno(rs.getString(2)) ;
tstudent.setClassname(rs.getString(3)) ;
tstudent.setSex(rs.getString(4)) ;
tstudent.setAge(rs.getString(5)) ;
tstudent.setNativeplace(rs.getString(6)) ;
tstudent.setNote(rs.getString(7)) ;
all.add(tstudent) ;
}
this.pstmt.close() ;
return all ;
}
public Tstudent findById(int id) throws Exception {
Tstudent tstudent = null ;
String sql = "SELECT id,studentno,classname,sex,age,nativeplace,note FROM t_student WHERE id=?" ;
this.pstmt = this.conn.prepareStatement(sql) ;
this.pstmt.setInt(1,id) ;
ResultSet rs = this.pstmt.executeQuery() ;
if(rs.next()){
tstudent = new Tstudent() ;
tstudent.setId(rs.getInt(1)) ;
tstudent.setStudentno(rs.getString(2)) ;
tstudent.setClassname(rs.getString(3)) ;
tstudent.setSex(rs.getString(4)) ;
tstudent.setAge(rs.getString(5)) ;
tstudent.setNativeplace(rs.getString(6)) ;
tstudent.setNote(rs.getString(7)) ;
}
this.pstmt.close() ;
return tstudent ;
}
}
5.下面是代理!问题不太多!
package org.yzsoft.test.proxy;
import java.util.List;
import org.yzsoft.test.dao.ITstudentDAO;
import org.yzsoft.test.impl.TstudentDAOImpl;
import org.yzsoft.test.util.DatabaseConnection;
import org.yzsoft.test.vo.Tstudent;
public class TstudentDAOProxy implements ITstudentDAO {
private DatabaseConnection dbc = null ;
private ITstudentDAO dao = null ;
public TstudentDAOProxy() throws Exception {
this.dbc = new DatabaseConnection() ;
this.dao = new TstudentDAOImpl(this.dbc.getConnection()) ;
}
public boolean doCreate(Tstudent tstudent) throws Exception {
boolean flag = false ;
try{
if(this.dao.findById(tstudent.getId()) == null){
flag = this.dao.doCreate(tstudent) ;
}
}catch(Exception e){
throw e ;
}finally{
this.dbc.close() ;
}
return flag ;
}
public boolean doDelete(int id) throws Exception {
boolean flag = false ;
try{
flag = this.dao.doDelete(id) ;
}catch(Exception e){
throw e ;
}finally{
this.dbc.close() ;
}
return flag ;
}
public boolean doUpdate(Tstudent tstudent) throws Exception {
boolean flag = false;
try {
flag = this.dao.doUpdate(tstudent);
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
return flag;
}
public List<Tstudent> findAll(String keyWord) throws Exception {
List<Tstudent> all = null ;
try{
all = this.dao.findAll(keyWord) ;
}catch(Exception e){
throw e ;
}finally{
this.dbc.close() ;
}
return all ;
}
public Tstudent findById(int id) throws Exception {
Tstudent tstudent = null ;
try{
tstudent = this.dao.findById(id) ;
}catch(Exception e){
throw e ;
}finally{
this.dbc.close() ;
}
return tstudent ;
}
}
6.最后工厂
package org.yzsoft.test.factory ;
import org.yzsoft.test.dao.ITstudentDAO;
import org.yzsoft.test.proxy.TstudentDAOProxy;
public class DAOFactory {
public static ITstudentDAO getITStudentDAOa() throws Exception{
return new TstudentDAOProxy() ;
}
}
下面将进入界面开发!
首先我们肯定做首页啦!
首先是首页
index
然后是增加页!
奉上代码!
index
<%@ page language="java" import="java.util.*,org.yzsoft.test.vo.*,org.yzsoft.test.factory.*" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
.CSS1 {
text-align: center;
font-size: 20px;
}
</style>
</head>
<body>
<%request.setCharacterEncoding("UTF-8"); %>
<%
try{
String keyWord=request.getParameter("kw");
if(keyWord==null){
keyWord="";
}
List<Tstudent>all=DAOFactory.getITStudentDAOa().findAll(keyWord);
Iterator<Tstudent> iter=all.iterator(); %>
<p class="CSS1">学生信息</p>
<table width="802" height="95" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>ID</td>
<td>学生学号</td>
<td>学生班级</td>
<td>学生性别</td>
<td>年龄</td>
<td>不知道</td>
<td>备注</td>
<td>管理操作 || <a href="new-stud.jsp">添加</a></td>
</tr>
<%
while(iter.hasNext()){
Tstudent tstudent=iter.next();
%>
<tr>
<td><%=tstudent.getId()%></td>
<td><%=tstudent.getStudentno()%></td>
<td><%=tstudent.getClassname()%></td>
<td><%=tstudent.getSex()%></td>
<td><%=tstudent.getAge()%></td>
<td><%=tstudent.getNativeplace()%></td>
<td><%=tstudent.getNote()%></td>
<td><a href="up-student.jsp?id=<%=tstudent.getId()%>">修改</a> || <a href="delete-ok.jsp?id=<%=tstudent.getId() %>">删除</a></td>
</tr>
<%} %>
</table>
<%}catch(Exception e)
{e.printStackTrace();} %>
</body>
</html>
2.增加信息页面
(先从第一个页面传递值至添加执行页)
<%@ page language="java" import="java.util.*,org.yzsoft.test.vo.*,org.yzsoft.test.factory.*" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'new-studok.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%request.setCharacterEncoding("UTF-8"); %>
<%
Tstudent tstudent=new Tstudent();
String studentno=request.getParameter("studentno");
String classname=request.getParameter("classname");
String sex=request.getParameter("sex");
String age=request.getParameter("age");
String nativeplace=request.getParameter("nativeplace");
String note=request.getParameter("note");
tstudent.setStudentno(request.getParameter("studentno")) ;
tstudent.setClassname(request.getParameter("classname")) ;
tstudent.setSex(request.getParameter("sex")) ;
tstudent.setAge(request.getParameter("age")) ;
tstudent.setNativeplace(request.getParameter("nativeplace")) ;
tstudent.setNote(request.getParameter("note")) ;
try{
if(DAOFactory.getITStudentDAOa().doCreate(tstudent)){
%>
<h3>信息添加成功!</h3>
<%
} else {
%>
<h3>信息添加失败!</h3>
<%
}
%>
<%
}catch(Exception e){
e.printStackTrace() ;
}
%>
<jsp:forward page="index.jsp"></jsp:forward>
</body>
</html>
END
欢迎大家提出建议!