今天的购物车比昨天多了一些新功能
理解分层:数据访问层、业务逻辑层、视图层
biz.imp与biz业务逻辑接口类
GoodsBiz
/**
*商品 订单的业务逻辑层接口
* @author zjjt
*
*/
public interface GoodsBiz {
/**
* 查询所有数据
* @return 返回查询到的集合
*/
public ArrayList<Goods>getAll();
/**
* 根据编号查询商品对象
* @param bid要查询对象的商品编号
* @return 查询到返回商品对象,否则返回null
*/
public Goods getById(int bid);
}
IOrderItemBiz
public interface IOrderItemBiz {
/**
* 查询指定用户的所有订单
* @param uuid 用户编号
* @return 返回订单集合
*/
public ArrayList<OrderItem> getAll(int uuid);
/**
* 查询用户的某个订单
* @param oid 订单编号
* @return 成功就返回订单对象,失败返回null
*/
public OrderItem getById(int oid);
/**
* 删除订单
* @param bid 商品编号
* @return 成功返回true,失败返回false
*/
public boolean deleteById(int bid,int suid);
/**
* 添加订单
* @param ds 要添加的订单对象
* @return 成功返回1,失败返回0
*/
public boolean taddId( OrderItem ds,double s);
/**
* 修改订单数量
* @param oid 订单编号
* @param number 订单商品数量
* @return 成功返回true,失败返回false
*/
public boolean updateByOid(int oid,int number,double s);
/**
* 求订单总价一共是多少
*/
public double getsum();
}
IUserBiz
/**
* 用户的业务逻辑层接口
* @author d
*
*/
public interface IUserBiz {
/**
* 用户登录方法
* @param uname 用户名
* @param upwd 用户密码
* @return 登录成功返回用户对象,否则返回null
*/
public User login(String uname,String upwd);
/**
* 用户注册方法
* @param user 要注册的用户
* @return 成功返回true,失败返回false
*/
public boolean register(User user);
}
biz.imp类
Imp_GoodsBiz
public class Imp_GoodsBiz implements GoodsBiz{
GoodsDao ss=new Imp_GoodsDao();
@Override
public ArrayList<Goods> getAll() {
//调用数据访问层的 查询方法
return ss.getAll();
}
@Override
public Goods getById(int bid) {
//调用数据访问层的单个查询方法
return ss.getById(bid);
}
}
Imp_OrderItemBiz
public class Imp_OrderItemBiz implements IOrderItemBiz {
//实例化数据访问层对象
IOrderItemDao iot = new Imp_OrderItemDao();
@Override
public ArrayList<OrderItem> getAll(int uuid) {
//调用数据访问层的 查询方法
return iot.getAll(uuid);
}
@Override
public OrderItem getById(int oid) {
//调用数据访问层的单个查询方法
return iot.getById(oid);
}
@Override
public boolean deleteById(int oid,int suid) {
//调用数据访问层的删除方法
return iot.deleteById(oid,suid)==1?true:false;
}
@Override
public boolean updateByOid(int oid, int number,double s) {
//调用数据访问层的修改方法
return iot.updateByOid(oid, number,s)>0?true:false;
}
@Override
public boolean taddId(OrderItem ds,double s) {
//调用数据访问层的添加方法
return iot.taddId(ds,s)>0?true:false;
}
@Override
public double getsum() {
return iot.getsum();
}
}
Imp_UserBiz
/**
* 用户的业务逻辑层实现类
* @author T277
*
*/
public class Imp_UserBiz implements IUserBiz {
//多态
Imp_UserDao iud = new Imp_UserDao();
public User login(String uname, String upwd) {
return iud.login(uname, upwd);
}
public boolean register(User user) {
return iud.register(user)==1?true:false;
}
}
数据访问层接口
GoodsDao
/**
* 商品的数据访问层接口
* @author zjjt
*
*/
public interface GoodsDao {
/**
* 查询所有数据
* @return 返回查询到的集合
*/
public ArrayList<Goods>getAll();
/**
* 根据编号查询商品对象
* @param bid要查询对象的商品编号
* @return 查询到返回商品对象,否则返回null
*/
public Goods getById(int bid);
}
IOrderItemDao
/**
* 订单的数据访问层接口
* @author T277
*
*/
public interface IOrderItemDao {
/**
* 查询指定用户的所有订单
* @param uuid 用户编号
* @return 返回订单集合
*/
public ArrayList<OrderItem> getAll(int uuid);
/**
* 查询用户的某个订单
* @param oid 订单编号
* @return 成功就返回订单对象,失败返回null
*/
public OrderItem getById(int oid);
/**
* 删除订单
* @param bid 订单编号
* @return 成功返回1,失败返回0
*/
public int deleteById(int bid,int suid);
/**
* 添加订单
* @param ds 要添加的订单对象
* @return 成功返回1,失败返回0
*/
public int taddId( OrderItem ds,double s);
/**
* 修改订单数量
* @param oid 订单编号
* @param number 订单商品数量
* @return 成功返回1,失败返回0
*/
public int updateByOid(int oid,int number,double s);
/**
* 求订单总价一共是多少
*/
public double getsum();
}
IUserDao
/**
* 用户的数据访问层接口
* @author d
*
*/
public interface IUserDao {
/**
* 用户登录方法
* @param uname 用户名
* @param upwd 用户密码
* @return 登录成功返回用户对象,否则返回null
*/
public User login(String uname,String upwd);
/**
* 用户注册方法
* @param user 要注册的用户
* @return 成功返回1,失败返回0
*/
public int register(User user);
}
数据访问层实现类dao.imp
Imp_GoodsDao
/**
* 商品的数据访问层实现类
* @author zjjt
*
*/
public class Imp_GoodsDao implements GoodsDao {
@Override
public ArrayList<Goods> getAll() {
ArrayList<Goods> nlist = new ArrayList<>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
String sql = "select * from goods";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
nlist.add(new Goods(rs.getInt(1),rs.getString(2),rs.getDouble(3),rs.getString(4),rs.getString(5)));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return nlist;
}
@Override
public Goods getById(int bid) {
Goods nlist = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
String sql = "select * from goods where bid="+bid;
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()) {
nlist=new Goods(rs.getInt(1),rs.getString(2),rs.getDouble(3),rs.getString(4),rs.getString(5));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return nlist;
}
}
Imp_OrderItemDao
/**
* 订单的数据访问层实现类
* @author T277
*
*/
public class Imp_OrderItemDao implements IOrderItemDao {
public ArrayList<OrderItem> getAll(int uuid) {
ArrayList<OrderItem> nlist = new ArrayList<>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
String sql = "select * from OrderItem where suid="+uuid;
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
nlist.add(new OrderItem(rs.getInt(1), uuid, rs.getInt(3), rs.getInt(4), rs.getDouble(5), rs.getDate(6), rs.getString(7)));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return nlist;
}
public OrderItem getById(int oid) {
OrderItem nlist = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
String sql = "select * from OrderItem where bid="+oid;
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()) {
nlist=(new OrderItem(oid, rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getDouble(5), rs.getDate(6), rs.getString(7)));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return null;
}
public int deleteById(int bid,int suid) {
int i = 0;
Connection con = null;
PreparedStatement ps = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("delete OrderItem where bid="+bid+"and suid="+suid);
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, null);
}
return i;
}
public int updateByOid(int oid, int number,double s) {
Goods nlist = null;
int i = 0;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("update OrderItem set gnumber=?,gsumprice=? where oid="+oid);
ps.setInt(1, number);
ps.setDouble(2, number*s);
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, null);
}
return i;
}
@Override
public int taddId(OrderItem ds,double s) {
// TODO Auto-generated method stub
int i = 0;
Connection con = null;
PreparedStatement ps = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("insert into OrderItem values(?,?,?,?,?,sysdate,?)");
ps.setInt(1, DBHelper.getNextId("OrderItem","oid"));
ps.setInt(2, ds.getUuid());
ps.setInt(3, ds.getBid());
ps.setInt(4, ds.getGnumber());
ps.setDouble(5,ds.getGnumber()*s);
ps.setString(6, ds.getOzt());
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, null);
}
return i;
}
@Override
public double getsum() {
double i=0;
ResultSet rs = null;
Connection con = null;
PreparedStatement ps = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select sum(gsumprice) from orderitem");
rs=ps.executeQuery();
if(rs.next()) {
i=rs.getDouble(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, null);
}
return i;
}
public static void main(String[] args) {
System.out.println(new Imp_OrderItemDao().deleteById(2,1));
}
}
Imp_UserDao
/**
* 用户的数据访问层实现类
* @author d
*
*/
public class Imp_UserDao implements IUserDao {
public User login(String uname, String upwd) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select * from dlbg where name=? and upwd=?");
ps.setString(1, uname);
ps.setString(2, upwd);
rs = ps.executeQuery();
if(rs.next()) {
user = new User(rs.getInt(1), uname, upwd, rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return user;
}
public int register(User user) {
int i = 0;
Connection con = null;
PreparedStatement ps = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("insert into dlbg values(?,?,?,?,?,?,?)");
ps.setInt(1, DBHelper.getNextId("dlbg","suid"));
ps.setString(2, user.getName());
ps.setString(3, user.getUpwd());
ps.setString(4, user.getXbs());
ps.setString(5, user.getAih());
ps.setString(6, user.getDz());
ps.setString(7, user.getFinfo());
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, null);
}
return i;
}
}
今天比昨天多了个实体类
User
private int suid;
private String name;
private String upwd;
private String xbs;
private String aih;
private String dz;
private String finfo;