Dao模式学习总结:
1、dao:Data Access Object(数据存取对象)
什么是dao模式:DAO模式是标准的J2EE设计模式之一.开发人员使用这个模式把底层的数据访问操作和上层的业务逻辑分开,此模式的主要作用是封装对数据库的各种操作;为了降低耦合性。
2、一个典型的dao模式应该包括:
(1)vo:(value object)值对象,只包含属性,对应于数据库一个表的属 性。
(2)dao接口:封装对某个vo的所有操作
(3)dao实现类:实现dao接口,重写具体方法;
(4)dao工厂:用来创建dao对象(可以没有)。
3、案例:
Account:账户表的vo
AccountDao:账户的访问数据接口,定义可对账户表的所有操作
AccountDaoImpl:AccountDao的实现类;
Convertor:转换接口;将一个ResultSet转换成一个vo对象
AccountConvertor:Convertor的实现类
JdbcUtil :加载mysql驱动,获得连接,CURD操作的封装
Text:测试代码;
代码:
Account.java:
public class Account {
Integer id;
String name;
Integer remain;
public Account(Integer id, String name,Integer remain) {
super();
this.id = id;
this.name = name;
this.remain = remain;
}
public Account() {
super();
}
public Account(String name,Integer remain) {
super();
this.name = name;
this.remain = remain;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getRemain() {
return remain;
}
public void setRemain(Integer remain) {
this.remain = remain;
}
@Override
public String toString() {
return "Account [id=" + id + ", name=" + name + ", remain=" + remain + "]";
}
}
AccountDao:
import java.util.List;
import jdbc.vo.Account;
public interface AccountDao {
void insert(Account account);
void delete(Integer i);
boolean update(Account account);
Account getAccountByid(Integer id);
List<Account> getAll();
}
AccountDaoImpl .java:
import jdbc.util.JdbcUtil;
import jdbc.vo.Account;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class AccountDaoImpl implements AccountDao{
String insert_sql="insert into account values(null,?,?)";
String delete_sql="delete from account where id=?";
String update_sql="update account set name=?,remain=? where id=?";
String selectOne="select * from account where id=?";
String select="select * from account";
@Override
public void insert(Account account) {
JdbcUtil.executeSql(insert_sql,account.getName(),account.getRemain());
}
@Override
public void delete(Integer i) {
JdbcUtil.executeSql(delete_sql,i);
}
@Override
public boolean update(Account account) {
return JdbcUtil.executeSql(update_sql, account.getName(),account.getRemain(),account.getId())>0?true:false;
}
@Override
public Account getAccountByid(Integer id) {
Account a=null;
try(Connection conn=JdbcUtil.getConnectiom();
PreparedStatement ps=conn.prepareStatement(selectOne)){
ps.setInt(1, id);
ResultSet rs=ps.executeQuery();
if(rs.next()) {
a=new Account();
a.setId(rs.getInt(1));
a.setName(rs.getString(2));
a.setRemain(rs.getInt(3));
}
}catch(Exception e) {
e.printStackTrace();
}
return a;
}
@Override
public List<Account> getAll() {
return JdbcUtil.query(select,new AccountConvertor());
}}
Convertor.java:
import java.sql.ResultSet;
public interface Convertor<T> {
public T convert(ResultSet rs) throws Exception;
}
AccountConvertor.java:
import java.sql.ResultSet;
import jdbc.util.Convertor;
import jdbc.vo.Account;
public class AccountConvertor implements Convertor<Account> {
@Override
public Account convert(ResultSet rs) throws Exception {
Account a=new Account();
a.setId(rs.getInt(1));
a.setName(rs.getString(2));
a.setRemain(rs.getInt(3));
return a;
}
}
JdbcUtil.java:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import jdbc.util.Convertor;
public class JdbcUtil {
static String user="root";
static String passwd="tiger";
static String url="jdbc:mysql://localhost:3306/jdbctext";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnectiom() {
Connection conn=null;
try {
conn=DriverManager.getConnection(url,user,passwd);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static int executeSql(String sql,Object ...args) {
int n=0;
try(Connection conn=getConnectiom();
PreparedStatement ps=conn.prepareStatement(sql)){
for(int i=0;i<args.length;i++) {
ps.setObject(i+1,args[i]);
}
n=ps.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}
return n;
}
/**
* 执行查询语句,将查询结果转换成T类型
* @return
*/
public static <T> List<T> query(String sql,Convertor<T> convertor,Object...args){
List<T>list=new ArrayList<T>();
try(Connection conn=getConnectiom();
PreparedStatement ps=conn.prepareStatement(sql)){
for(int i=0;i<args.length;i++) {
ps.setObject(i+1,args[i]);
}
ResultSet rs=ps.executeQuery();
while(rs.next()) {
list.add(convertor.convert(rs));
}
}catch(Exception e) {
e.printStackTrace();
}
return list;
}
}