Dao编程模型(完善后)

Dao编程模型(完善后)

说明

上一篇文章Dao编程模型中,最后的几个功能我只完成了相对较复杂的两个,其它由于时间原因未完善,这篇文章是对于上一篇文章的补充完善,并加了少量相关注释
其中做补充的只有两处代码:country的dao的实现-countryDaoImpl测试功能–daotest
其他部分代码请大家参考上一篇文章,请大家谅解浏览文章带来的不便,感谢!

代码展示

country的Dao的实现

package jdbc.impl;

import jdbc.dao.ICountryDao;
import jdbc.pojo.country;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jdbc.util.jdbcSqlFactory;
public class CountryDaoImpl implements ICountryDao {
    private jdbcSqlFactory sqlFactory=new jdbcSqlFactory();

    //增加一条记录
    @Override
    public void save(country pojo) {
        String insertSql="insert into country values (null,?,?)";
        sqlFactory.prepareUpdate(insertSql,pojo.getCname(),pojo.getLocation());
    }

    //删除一条记录
    @Override
    public void cancel(Integer integer) {
        String cancelSql="delete from country where cid="+integer;
        sqlFactory.executeUpdate(cancelSql);
    }

    //修改记录
    @Override
    public void modify(country pojo) {
        String modifySql="update country set cname=?,location=? where cid=?";
        sqlFactory.prepareUpdate(modifySql,pojo.getCname(),pojo.getLocation(),pojo.getCid());
    }

    //按条件查询
    @Override
    public List<country> queryBycondition(country pojo) {
        List<country> countries=new ArrayList<>();

        String querySql="select * from country where 1=1 ";
        if (pojo!=null){
            if (pojo.getCid()!=0){
                querySql+="and cid="+pojo.getCid();
            }
            if (pojo.getCname()!=null&&!"".equals(pojo.getCname())){
                querySql+="and cname like '%"+pojo.getCname()+"%'";
            }
            if (pojo.getLocation()!=null&&!"".equals(pojo.getLocation())){
                querySql+="and location like '%"+pojo.getLocation()+"%'";
            }
        }
        List<Map<String,Object>> table=sqlFactory.executeQuery(querySql);

        for (Map<String,Object> row: table){
            country cou=new country();
            cou.setCid(Integer.valueOf(row.get("cid")+""));
            cou.setCname(String.valueOf(row.get("cname")));
            cou.setLocation(String.valueOf(row.get("location")));

            countries.add(cou);
        }

        return countries;
    }

    //查询一条记录
    @Override
    public country queryOne(Integer integer) {
        country coun=new country();
        String querySql="select * from country where cid="+integer;
        List<Map<String,Object>> table =new ArrayList<>();
        table= sqlFactory.executeQuery(querySql);
        for (Map<String,Object> row:table){
            coun.setCid(Integer.valueOf(row.get("cid")+""));
            coun.setCname(String.valueOf(row.get("cname")));
            coun.setLocation(String.valueOf(row.get("location")));
        }

        return coun;
    }
}

测试功能

import jdbc.util.jdbcSqlFactory;
import jdbc.pool.ConnectionPool;
import jdbc.pojo.country;
import jdbc.impl.CountryDaoImpl;
import jdbc.dao.ICountryDao;


public class daotest {
    public static void main(String[] args){
        country coun=new country();
        coun.setCname("song");  //数据库表设置了cid主键自增长,所以不用设置cid
        coun.setLocation("do not konw");
        country coun1=new country();
        coun1.setCid(5);
        coun1.setCname("modifysong");
        coun1.setLocation("newlocation");

        CountryDaoImpl countryDao=new CountryDaoImpl();

        //save
        countryDao.save(coun);
        //queryBycondition
        System.out.println(countryDao.queryBycondition(coun).toString());
        //queryOne
        System.out.println(countryDao.queryOne(5).toString());
        //cancel
        countryDao.cancel(6);
        System.out.println(countryDao.queryOne(6).toString());
        //modify
        countryDao.modify(coun1);
        System.out.println(countryDao.queryOne(5).toString());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的大男子主义

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值