一、架构
二、实现步骤
开源组件及jar文件:
数据库驱动包(1个)
C3P0连接池包(2 个)
DbUtils组件 (1个)
BeanUtils组件(2个)
FileUpload组件(2个)
配置
C3p0配置文件
分层:
Entity/dao/service/servlet
导入java包
entity类
/**
-
实体类设计
*/
public class FoodType {private int id;//-- 类别主键
private String typeName;// – 类别名称public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
}
Dao层
DAO层接口
/**
- dao接口设计
/
public interface IFoodTypeDao {
/*- 添加
/
void save(FoodType foodType);
/* - 更新
/
void update(FoodType foodType);
/* - 删除
/
void delete(int id);
/* - 根据主键查询
/
FoodType findById(int id);
/* - 查询全部
/
List getAll();
/* - 根据名称查询
*/
List getAll(String typeName);
}
- 添加
DAO层实体类
/**
- dao实现
*/
public class FoodTypeDao implements IFoodTypeDao {
@Override
public void delete(int id) {
String sql = "delete from foodType where id=?";
try {
JdbcUtils.getQuerrRunner().update(sql, id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public FoodType findById(int id) {
String sql = "select * from foodType where id=?";
try {
return JdbcUtils.getQuerrRunner().query(sql, new BeanHandler<FoodType>(FoodType.class), id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public List<FoodType> getAll() {
String sql = "select * from foodType";
try {
return JdbcUtils.getQuerrRunner().query(sql, new BeanListHandler<FoodType>(FoodType.class));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public List<FoodType> getAll(String typeName) {
String sql = "select * from foodType where typeName like ?";
try {
return JdbcUtils.getQuerrRunner()
.query(sql, new BeanListHandler<FoodType>(FoodType.class),"%" + typeName + "%");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void save(FoodType foodType) {
String sql = "INSERT INTO foodType(typeName) VALUES(?);";
try {
JdbcUtils.getQuerrRunner().update(sql,foodType.getTypeName());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void update(FoodType foodType) {
String sql = "update foodType set typeName=? where id=?";
try {
JdbcUtils.getQuerrRunner().update(sql, foodType.getTypeName(),foodType.getId());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}