public void testBatchSaveDog(){
Dogdao dogDao = new DogDaoImpl2();
Dog dog1 = new Dog("a",100,100,"t1");
Dog dog2 = new Dog("b",100,90,"t2");
Dog dog3 = new Dog("c",90,90,"t3");
ArrayList<Dog> dogs = new ArrayList<>();
dogs.add(dog1);
dogs.add(dog2);
dogs.add(dog3);Integer num = dogDao.batchSaveDog(dogs);
System.out.println(num);
}
批量删除
实现类
publicInteger batchDelDog(List<Integer> ids) {
String sql="delete from dog where id in (";for(Integer id :
ids) {
sql+= id +",";
}
sql=sql.substring(0,sql.length()-1);sql+=")";
System.out.println(sql);//return null;return super.executeUpdate(sql);
}
测试类
public void testBatchDelMaster() {
MasterDao masterDao = new MasterDaoImpl();
ArrayList<Integer> ids = new ArrayList<>();
ids.add(11);
ids.add(12);
ids.add(13);Integerinteger= masterDao.batchDelDog(ids);
System.out.println(integer);
}
批量修改
实现类
publicInteger batchUpdateDog(List<Dog> dogs) {
String sql="";int num =0;for(Dog dog :
dogs) {
sql="update dog set";if(dog.getName()!=null&& dog.getName()!=""){
sql+=" name = '"+ dog.getName()+"',";
}
if(dog.getHealth()!=null){
sql+=" health = "+ dog.getHealth()+",";
}
if(dog.getLove()!=null){
sql+=" love = "+ dog.getLove()+",";
}
if(dog.getStrain()!=null&& dog.getStrain()!=""){
sql+=" strain = '"+ dog.getStrain()+"',";
}
sql=sql.substring(0,sql.length()-1)+" where id = "+ dog.getId()+";";
System.out.println(sql);
num += super.executeUpdate(sql);
}
return num;
}
测试类
public void testBatchUpdateMaster() {
MasterDao masterDao = new MasterDaoImpl();
Master master1 = new Master();
master1.setPid(18);
master1.setName("旺仔");
Master master2 = new Master();
master1.setPid(19);
master1.setYearnum(5);
Master master3 = new Master();
master1.setPid(20);
master1.setAge(50);
ArrayList<Master> masters = new ArrayList<>();
masters.add(master1);
masters.add(master2);
masters.add(master3);Integerinteger= masterDao.batchUpdateMaster(masters);
System.out.println(integer);
}