DAO设计模式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DAO设计模式相关的知识,希望对你有一定的参考价值。
DAO 数据访问对象 属于JAVAEE的数据层的操作 用来完成数据库的访问,实现基本的CURD数据库操作
DAO包括五个部分:
1. 数据库连接类 实现数据库连接封装的操作
2. javabean类 对应数据库中的表 每个bean对应一张表
3. DAO接口 定义了数据库的CURD等操作 用于给具体子类实现数据库操作
4. DAO实现类 实现DAO接口
5 . DAO工厂类 用来获取DAO实现类的实例 完成数据库操作 一般获取实例的方法为静态方法
代码演示:
1. 数据库连接类
package com.lbwmx.MySQL;
import Java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
//创建数据库操作类DatabaseConnection,主要负责数据库连接及关闭。
public class DbConnection {
private final String driver="com.mysql.jdbc.Driver";
private final String url="jdbc:mysql://localhost:3306/test";
private final String dbuser="littlebear";
private final String dbpassword="littlebear";
private Connection conn=null;
public DbConnection() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
this.conn=DriverManager.getConnection(url,dbuser,dbpassword);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
return this.conn;
}
public void closeConnection() {
try {
this.conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2. javabean 类
package com.lbwmx.bean;
import com.lbwmx.dao.DaoFactory;
public class User {
private int uid;
private String name;
private String password;
public User() {
super();
}
public User(int uid,String password){
this.uid=uid;
this.password=password;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean LoginValidate() throws Exception{
User user=(User)DaoFactory.getDaoInstance().findByUid(this.uid);
if (user==null) {
return false;
}
if (!user.getPassword().equals(this.password)) {
return false; //error password
}
return true;
}
}
3. DAO接口类
package com.lbwmx.dao;
import java.util.List;
import com.lbwmx.bean.User;
public interface UserDao {
public boolean doInsert(User user) throws Exception;
public User findByUid(int uid) throws Exception;
public List findAll() throws Exception;
}
4 . DAO实现类
package com.lbwmx.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.lbwmx.bean.User;
public class UserDaoImpl implements UserDao{
private Connection conn=null;
private PreparedStatement psmt=null;
public UserDaoImpl(Connection conn) {
this.conn=conn;
}
public boolean doInsert(User user) throws Exception {
String insert="insert into user(uid,name,password) values (?,?,?)";
this.psmt=this.conn.prepareStatement(insert);
this.psmt.setInt(1, user.getUid());
this.psmt.setString(2, user.getName());
this.psmt.setString(3, user.getPassword());
int n = this.psmt.executeUpdate();
this.psmt.close();
if (n>0) {
return true;
}
return false;
}
public List findAll() throws Exception {
String select="select * from user";
List list=new ArrayList();
this.psmt=this.conn.prepareStatement(select);
ResultSet rst=this.psmt.executeQuery();
while (rst.next()) {
User user=new User();
user.setUid(rst.getInt("uid"));
user.setName(rst.getString("name"));
user.setPassword(rst.getString("password"));
list.add(user);
}
this.psmt.close(); //当关闭 psmt的时候,rst结果集也会随之关闭!
//rst.close();
return list;
}
public User findByUid(int uid) throws Exception {
String sql="select * from user where uid=?";
this.psmt=this.conn.prepareStatement(sql);
ResultSet rst=this.psmt.executeQuery();
User user=null;
if (rst.next()) {
user=new User();
user.setUid(rst.getInt("uid"));
user.setName(rst.getString("name"));
user.setPassword(rst.getString("password"));
}
this.psmt.close();
return user;
}
}
DAO代理类
package com.lbwmx.dao;
import java.util.List;
import com.lbwmx.bean.User;
import com.lbwmx.mysql.DbConnection;
public class DaoProxy implements UserDao{
private DbConnection dbConnection=null;
private UserDao dao=null;
public DaoProxy() {
this.dbConnection=new DbConnection();
this.dao=new UserDaoImpl(this.dbConnection.getConnection());
}
public boolean doInsert(User user) throws Exception {
boolean flag=false;
try {
if (null==this.dao.findByUid(user.getUid())) {
flag=this.dao.doInsert(user);
}
} catch (Exception e) {
throw e;
}finally{
this.dbConnection.closeConnection();
}
return flag;
}
public List findAll() throws Exception {
List list=null;
try {
list=this.dao.findAll();
} catch (Exception e) {
throw e;
}finally{
this.dbConnection.closeConnection();
}
return list;
}
public User findByUid(int uid) throws Exception {
User user=null;
try {
user=this.dao.findByUid(uid);
} catch (Exception e) {
throw e;
}
return user;
}
}
5 .DAOFactory类 获取实现类实例
package com.lbwmx.dao;
public class DaoFactory {
//定义工厂类DaoFactory。工厂类通过DaoProxy实例化IDao对象,供调用者操作数据库。
public static UserDao getDaoInstance(){
return new DaoProxy();
}
}
测试代码:
User user = new User(1000,"password" );
boolean flag = DaoFactory.getDaoInstance().doInsert(user);
if(flag){
System.out.println("success");
}else{
System.out.println("error");
}
以上是关于DAO设计模式的主要内容,如果未能解决你的问题,请参考以下文章