说明
上一篇文章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");
coun.setLocation("do not konw");
country coun1=new country();
coun1.setCid(5);
coun1.setCname("modifysong");
coun1.setLocation("newlocation");
CountryDaoImpl countryDao=new CountryDaoImpl();
countryDao.save(coun);
System.out.println(countryDao.queryBycondition(coun).toString());
System.out.println(countryDao.queryOne(5).toString());
countryDao.cancel(6);
System.out.println(countryDao.queryOne(6).toString());
countryDao.modify(coun1);
System.out.println(countryDao.queryOne(5).toString());
}
}
|