项目开发流程
- 获取需求 (猪八戒网)
- 设计
数据库设计
实体类设计(一般是实体类对应一张表)
分层设计
com.westos.eshop.controller (servlet) 控制层 + jsp
com.westos.eshop.service (biz) 业务逻辑层
com.westos.eshop.dao 数据访问层(完成数据的增删改查)
com.westos.eshop.entity 实体类
domain 域对象
com.westos.eshop.util 工具类 - 编写代码
- 测试
- 部署运行
例如:电商后台管理项目 中的实体类
- 类别管理
id(编号) name(名字) pid(父类别编号) lvl(级别)
package com.westos.eshop.entity;
import java.io.Serializable;
/**
* 类别
*
* @author 寇祎欢
*/
public class Category implements Serializable {
private int id;
private String name;
private int pid;
private int sid;
private String path;
private int lvl;
/**
* 无参构造
*/
public Category (){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
/**
*
* @return 返回
*/
public int getLvl() {
return lvl;
}
public void setLvl(int lvl) {
this.lvl = lvl;
}
}
- 商品管理
id(编号) jdzy_shop_id(店铺编号) brand_id(品牌编号) name(商品名称)
origin_price 原始价
market_price 市场价
price 销售价
img 图片地址
category_id 类别编号
package com.westos.eshop.entity;
/*
id(编号) jdzy_shop_id(店铺编号) brand_id(品牌编号) name(商品名称)
origin_price 原始价
market_price 市场价
price 销售价
img 图片地址
category_id 类别编号
*/
import java.io.Serializable;
/**
* 商品类
* @author 寇祎欢
*/
public class Product implements Serializable {
private int id;
private int jdzy_shop_id;
private int brand_id;
private String name;
private int origin_price;
private int market_price;
private int price;
private String img;
private int categor_id;
/**
* 无参构造
*/
public Product(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getJdzy_shop_id() {
return jdzy_shop_id;
}
public void setJdzy_shop_id(int jdzy_shop_id) {
this.jdzy_shop_id = jdzy_shop_id;
}
public int getBrand_id() {
return brand_id;
}
public void setBrand_id(int brand_id) {
this.brand_id = brand_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOrigin_price() {
return origin_price;
}
public void setOrigin_price(int origin_price) {
this.origin_price = origin_price;
}
public int getMarket_price() {
return market_price;
}
public void setMarket_price(int market_price) {
this.market_price = market_price;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public int getCategor_id() {
return categor_id;
}
public void setCategor_id(int categor_id) {
this.categor_id = categor_id;
}
}
- 用户表
username
password
package com.westos.eshop.entity;
import java.io.Serializable;
/**
* 用户类
* @author 寇祎欢
*/
public class User implements Serializable {
private String username;
private String password;
/**
* 无参构造
*/
public User(){
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
/**
* 获取用户名
* @return返回用户名
*/
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
/**
* 获取密码
* @return返回密码
*/
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
问题及难点:
不输入条件
where name like ?
where price between ? and ?
where name like ? and (price between ? and ?)
public static List<Product> test (String name, Double lowPrice, Double highPrice, int m, int n) {
StringBuilder sql = new StringBuilder(256);
List<Object> values = new ArrayList<>();
sql.append("select * from product where 1=1");
if(name != null && !name.equals("")) {
sql.append(" and name like ?");
values.add(name);
}
if(lowPrice != null && highPrice !=null) {
sql.append(" and price between ? and ?");
values.add(lowPrice);
values.add(highPrice);
}
sql.append(" limit ?, ?");
values.add(m);
values.add(n);
System.out.println(sql);
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = Utils.getConnection();
stmt = conn.prepareStatement(sql.toString());
for (int i = 0; i < values.size(); i++) {
stmt.setObject(i+1, values.get(i));
}
rs = stmt.executeQuery();
List<Product> list = new ArrayList<>();
while(rs.next()) {
Product p = new Product();
list.add(p);
}
return list;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
Utils.close(rs, stmt, conn);
}
}