DAO设计的一个小例子

首先,我数据库中的样本为tobacco烟草表,大概有烟草ID,名称,产地,各种价格之类的字段,我要做的无非就是把表中的数据显示出来而已,并简单的实现以下分页。
1.第一步当然是JavaBean了。

public class Tobacco {
private String tobaid;
private String tobaname;
private String factid;
private float tbprice;
private float pfprice;
private float lsprice;
private String style;
public String getTobaid() {
return tobaid;
}
public void setTobaid(String tobaid) {
this.tobaid = tobaid;
}
public String getTobaname() {
return tobaname;
}
public void setTobaname(String tobaname) {
this.tobaname = tobaname;
}
public String getFactid() {
return factid;
}
public void setFactid(String factid) {
this.factid = factid;
}
public float getTbprice() {
return tbprice;
}
public void setTbprice(float tbprice) {
this.tbprice = tbprice;
}
public float getPfprice() {
return pfprice;
}
public void setPfprice(float pfprice) {
this.pfprice = pfprice;
}
public float getLsprice() {
return lsprice;
}
public void setLsprice(float lsprice) {
this.lsprice = lsprice;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}

}
其实在我看来JavaBean很好写,也许是我刚开始学习,所以接触的东西比较浅显吧。
2.第二部,当然需要连接数据库,这里我用的是DAO模式,虽然程序很小,但是正好接触到了DAO,所以就试着写了一下。刚开始的时候觉得DAO实在是麻烦,又是接口又是实现,再加上代理类,工厂类什么的觉得多此一举。不过,当自己在写DAO的时候才发觉没有那么简单,实现数据操作与业务逻辑之间的分离真不是盖的,写完DAO之后用起来真的是很方便。


DAOImpl类:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.liu.tobacco.dao.TobaccoDAO;
import com.liu.tobacco.vo.Tobacco;

public class TobaccoDAOImpl implements TobaccoDAO {
private Connection conn=null;
public TobaccoDAOImpl(Connection conn){
this.conn=conn;
}
@Override
public List<Tobacco> findAll(int currentPage,int pageSize) throws Exception {
// TODO Auto-generated method stub
List<Tobacco> allTobacco=new ArrayList<Tobacco>();
PreparedStatement pstmt=null;
String sql="select temp.* from(select tobaid,tobaname,tbprice,pfprice,lsprice,style,factid,rownum rn from tobacco " +
"where rownum<="+(currentPage*pageSize)+") temp where rn>"+(currentPage-1)*pageSize;
try{
pstmt=this.conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
Tobacco tobacco=null;
while(rs.next()){
tobacco=new Tobacco();
tobacco.setTobaid(rs.getString(1));
tobacco.setTobaname(rs.getString(2));
tobacco.setTbprice(rs.getFloat(3));
tobacco.setPfprice(rs.getFloat(4));
tobacco.setLsprice(rs.getFloat(5));
tobacco.setStyle(rs.getString(6));
tobacco.setFactid(rs.getString(7));
allTobacco.add(tobacco);
}
rs.close();
}catch(Exception e){
throw e;
}finally{
try{
pstmt.close();
}catch(Exception e){}
}
return allTobacco;
}

public int getCount() throws Exception{
int allCount=0;
PreparedStatement pstmt=null;
String sql="select count(*) from tobacco";
try{
pstmt=this.conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
allCount=rs.getInt(1);
}
rs.close();
}catch(Exception e){
throw e;
}finally{
try{
pstmt.close();
}catch(Exception e){}
}
return allCount;
}
}
DAO接口很简单,只是定义了一些需要的数据操作而已,上面的代码是接口的实现类,它只负责数据的操作,而没有负责数据库的连接,对于数据库的连接我单独写了一个类,当然这都是大家各自的喜好了。

DAOProxy类:
import java.util.List;

import com.liu.tobacco.dao.TobaccoDAO;
import com.liu.tobacco.dao.impl.TobaccoDAOImpl;
import com.liu.tobacco.dbc.DataBaseConnection;
import com.liu.tobacco.vo.Tobacco;

public class TobaccoDAOProxy implements TobaccoDAO {
private DataBaseConnection dbc=null;
TobaccoDAO dao=null;
public TobaccoDAOProxy(){
this.dbc=new DataBaseConnection();
this.dao=new TobaccoDAOImpl(this.dbc.getConnection());
}
@Override
public List<Tobacco> findAll(int currentPage,int pageSize) throws Exception {
// TODO Auto-generated method stub
List<Tobacco> allTobacco=null;
try{
allTobacco=this.dao.findAll(currentPage,pageSize);
}catch(Exception e){
throw e;
}finally{
this.dbc.close();
}
return allTobacco;
}

public int getCount() throws Exception{
int allCount=0;
try{
allCount=this.dao.getCount();
}catch(Exception e){
throw e;
}finally{
this.dbc.close();
}
return allCount;
}
}

代理类是做什么的呢?因为我数据库的连接是写在一个单独的类中,所以在代理类中它就调用数据库连接类和DAOImpl类。对于这个代理类我琢磨了一段时间,觉得完全没有必要,查了一些资料,发现这个代理类貌似就是但当一个中介的功能,它相当于包装了DAOImpl类。就像现在的房屋中介一样,我们找房子一般都是通过中介,然后中介去和房主沟通。对于我们而言则不是和房主直接沟通,这样有的时候会避免很多麻烦。现在想想现实生活这样的例子有很多,我却一直在由于代理类的存在必要性,有的时候就是糊涂。正所谓存在即合理,大牛们设计的模式肯定不会是无用的。

DAOFactory类:
import com.liu.tobacco.dao.TobaccoDAO;
import com.liu.tobacco.dao.proxy.TobaccoDAOProxy;

public class DAOFactory {
public static TobaccoDAO getTobaccoDAOInstance(){
return new TobaccoDAOProxy();
}
}

到这里基本上DAO就结束了,其实每个人的写法都不一样,我只是偏好这样写哈。
前天学习DAO,到现在对于DAO稍微有了一点点的理解吧,学习都是一个漫长的过程,但是学会了一点就会很开心,知足常乐。大家共勉!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值