用session实现购物车(含设计分析及源代码)part2

现在来编写domain层、dao层、util层、service层

domain层编写

建立一个名为Product的类,属性对应的是数据库里面的数据

在建议一个名为CarProduct的类,对应的是保存用户购买的产品

如下:


Product类源码:

package cn.zc.domain;
/*与数据库对应的商品类*/
public class Product {
	private Integer id;//存储商品id
	private String name;//存储商品名称
	private Double price;//存储商品价格
	private Integer memory;//存储商品存货量
	private String summary;//存储商品简介
	
	public Product() {
		super();
	}

	public Product(Integer id, String name, Double price, Integer memory,
			String summary) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.memory = memory;
		this.summary = summary;
	}

	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 Integer getMemory() {
		return memory;
	}

	public void setMemory(Integer memory) {
		this.memory = memory;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}
	
	
}

CarProduct类源码:

package cn.zc.domain;
/*已经添加到购物车中商品类*/
public class CarProduct {

	private String name;//存储商品名称
	private Double price;//存储商品价格
	private Integer buyNum;//存储商品购买的数量
	public  CarProduct(Product entity,int buyNum){
		this.setName(entity.getName());
		this.setPrice(entity.getPrice());
		this.setBuyNum(buyNum);
	}

	public Integer getBuyNum() {
		return buyNum;
	}

	public void setBuyNum(Integer buyNum) {
		this.buyNum = buyNum;
	}

	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;
	}
}

util层的编写:

编写JdbcUtil类获取连接对象

如下结构:


JdbcUtil源码:

package cn.zc.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcUtil {
	private static Connection con;// 连接对象
	/*获取连接对象的方法*/
	public static Connection getConnection(){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			/*getConnection()方法的三个参数是:1、数据库的名称2、数据库管理员名称3、管理员密码*/
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/goods","root","zc");
		}catch(Exception e){
			e.printStackTrace();
		}
		return con;
	}
	/*关闭对象释放资源方法*/
	public static void release(ResultSet rs,PreparedStatement ps,Connection con){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(ps!=null){
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


dao层的编写:

编写名为ProductDao的接口和实现接口的ProductDaoImpl实现类

结构:


ProductDao源码:

package cn.zc.dao;

import java.util.List;

import cn.zc.domain.Product;

public interface ProductDao {
	abstract public List<Product> findAll();//查询所有商品信息
	abstract public Product findById(Integer id);//查询指定id的商品
}
ProductDaoImpl实现类源码:

package cn.zc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.zc.domain.Product;
import cn.zc.util.JdbcUtil;

public class ProductDaoImpl implements ProductDao {
	private  Connection con;//连接对象
	private PreparedStatement ps; //预处理对象
	private ResultSet rs;//结果集对象
	@Override
	public List<Product> findAll() {
		/*第一步:定义返回值变量*/
		List <Product>entities=new ArrayList<Product>();
		/*第二步:获取连接对象*/
		con=JdbcUtil.getConnection();
		/*第三步:定义sql语句*/
		String sql="select id,name,price,memory,summary from product";
		/*第四步:根据sql语句生成预处理对象*/
		try {
			ps=con.prepareStatement(sql);
			/*第五步:为占位符赋值*/
			/* 第六步:提交sql语句 */
			rs = ps.executeQuery();
			/*第七步:判断*/
			while(rs.next()){
				Product entity=new Product();
				entity.setId(rs.getInt("id"));
				entity.setName(rs.getString("name"));
				entity.setPrice(rs.getDouble("price"));
				entity.setMemory(rs.getInt("memory"));
				entity.setSummary(rs.getString("summary"));
				entities.add(entity);
			}
			/*第八步:释放资源*/
			JdbcUtil.release(rs, ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return entities;
	}
	@Override
	public Product findById(Integer id) {
		/*第一步:声明返回值变量*/
		Product entity=new Product();
		/*第二步:获取连接对象*/
		con=JdbcUtil.getConnection();
		/*第三步:定义sql语句*/
		String sql="select id,name,price,memory,summary from product where id=?";
		/*第四步:根据sql语句生成预处理对象*/
		try {
			ps=con.prepareStatement(sql);
			/*第五步:为占位符赋值*/
			int index=1;
			ps.setInt(index++, id);
			/*第六步:提交sql语句*/
			rs=ps.executeQuery();
			/*第七步:判断*/
			if(rs.next()){
				entity.setId(rs.getInt("id"));
				entity.setName(rs.getString("name"));
				entity.setPrice(rs.getDouble("price"));
				entity.setMemory(rs.getInt("memory"));
				entity.setSummary(rs.getString("summary"));
			}
			/*第八步:释放资源*/
			JdbcUtil.release(rs, ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return entity;
	}
}

service层的编写
编写ProductService接口和实现类ProductServiceImpl

结构:


ProductService借口继承ProductDao即可。

ProductService源码:

package cn.zc.service;

import cn.zc.dao.ProductDao;

public interface ProductService extends ProductDao {

}
ProductServiceImpl类实现ProductService借口调用ProductDaoImpl中的方法即可。

ProductServiceImpl类源码:

package cn.zc.service;

import java.util.List;

import cn.zc.dao.ProductDaoImpl;
import cn.zc.domain.Product;

public class ProductServiceImpl implements ProductService {
	private ProductDaoImpl pdi=new ProductDaoImpl();//定义私有属性ProductDaoImpl类的实体,用来调用里面的方法
	@Override
	public List<Product> findAll() {
		// TODO Auto-generated method stub
		return pdi.findAll();
	}
	@Override
	public Product findById(Integer id) {
		// TODO Auto-generated method stub
		return pdi.findById(id);
	}

}



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值