CRUD是Create(创建)、Read(读取)、Update(更新)和Delete(删除)
首先,肯定是要与数据交互的,所以我们先写一个bean类
代码如下:
public class Food {
private Integer id;
private String name;
private Double price;
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 Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Food(Integer id, String name, Double price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
public Food() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Food [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}
在bean类中创建的字段,与数据库中的字段相对应,
下面是Dao代码:
Dao接口:
public interface FoodDaoI {
//列表展示
List<Food> showFoodList();
//删除操作
void delFoodById(Integer id);
//添加操作
void addFood(Food food);
//更新前回显
Food findById(Integer id);
//更新
void updateFood(Food food);
}
Dao实现类:
public class FoodDaoImpl implements FoodDaoI{
private QueryRunner qr=new QueryRunner(JDBCUtils3.getDataSource());
public List<Food> showFoodList() {
String sql="select * from ee";
List<Food> list=null;
try {
list=qr.query(sql, new BeanListHandler<Food>(Food.class));
} catch (SQLException e) {
// TODO Auto-gener