-
什么是Dao(data access object)模式*
Dao模式就是对数据库中属性及其对属性操作的分装,
-
一个典型的Dao操作应该包括: 1.VO:对属性的分装,将一个数据库表格中的所有的属性分装到一个类中。值对象 2.Dao实现类: 3.Dao接口:对属性操作的封装,例如:CURD(增删改查)(这个可以不写采用的优势是可以解耦) 4.Dao工厂:用来创建Dao对象。
-
采用Dao模式的优点: 方便代码的统一格式化。
-
4.代码举例:(增删改)
package com.oracle.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.oracle.util.JdbcUtil;
import com.oracle.vo.Account;
public class AccountDaoImpl implements AccountDao {
String sql_insert="insert into account values(null,?,?)";
String sql_delete="delete from account where accountid=?";
String sql_update="update account set name=?,remain=? where accountid=?";
@Override
public void insert(Account account) {
// TODO Auto-generated method stub
try(Connection conn=JdbcUtil.getConnection();
PreparedStatement st=conn.prepareStatement(sql_insert);){
st.setString(1,(account).getName());
st.setInt(2, account.getRemain());
st.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("插入一个账号:"+account);
}
@Override
public void delete(Integer accountId) {
// TODO Auto-generated method stub
try(Connection conn=JdbcUtil.getConnection();
PreparedStatement ps=conn.prepareStatement(sql_delete)){
ps.setInt(1, accountId);
ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("删除一个账号:"+accountId);
}
@Override
public boolean update(Account account) {
// TODO Auto-generated method stub
int b=0;
try(Connection conn=JdbcUtil.getConnection();
PreparedStatement ps=conn.prepareStatement(sql_update)){
ps.setString(1, account.getName());
ps.setInt(2, account.getRemain());
ps.setInt(3, account.getAccountid());
ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("更新成功:"+account);
return b>0?true:false;
}
- 对增删改的优化
代码:
- JdbcUtil:
public static Connection getConnection() throws SQLException {
Connection conn=null;
try {
conn=(Connection) DriverManager.getConnection(url,userName,passWord);
}catch(SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 执行一个sql;
* @param sql;
* @param objects sql语义需要绑定的参数值;
* @return
*/
public static int excuteSql(String sql,Object...objects) {
int count=0;
try( Connection conn = getConnection();
PreparedStatement ps=conn.prepareStatement(sql);) {
for(int i=0;i<objects.length;i++) {
ps.setObject(i+1, objects[i]);
// ps.executeUpdate();
}
count=ps.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(count);
return count;
}
- 增删改的代码:
String sql_insert="insert into account values(null,?,?)";
String sql_delete="delete from account where accountid=?";
String sql_update="update account set name=?,remain=? where accountid=?";
@Override
public void insert(Account account) {
// TODO Auto-generated method stub
JdbcUtil.excuteSql(sql_insert, account.getName(),account.getRemain());
}
@Override
public void delete(Integer accountId) {
// TODO Auto-generated method stub
JdbcUtil.excuteSql(sql_delete,accountId);
}
@Override
public boolean update(Account account) {
// TODO Auto-generated method stub
return JdbcUtil.excuteSql(sql_update, account.getName(),account.getRemain(),account.getAccountid())>0?true:false;
}
欢迎评论和修改>@<