模拟用户操作购物车添加商品修改数量

项目结构

模拟用户操作购物车添加商品修改数量_第1张图片

数据库

JdbcUtil.java

package com.jdbc.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

public class JdbcUtil {
	private static BasicDataSource dataSource = null;
	static{
		dataSource = new BasicDataSource();
		Properties prop = new Properties();
		try {
			prop.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
			String url = prop.getProperty("jdbc.url");
			String username = prop.getProperty("jdbc.username");
			String password = prop.getProperty("jdbc.password");
			String initialSizeStr = prop.getProperty("jdbc.initialSize");
			int initialSize = Integer.parseInt(initialSizeStr);
			String maxActiveStr = prop.getProperty("jdbc.maxActive");
			int maxActive = Integer.parseInt(maxActiveStr);
			String maxWaitStr = prop.getProperty("jdbc.maxWait");
			int maxWait = Integer.parseInt(maxWaitStr);
			try {
				Class.forName("com.mysql.jdbc.Driver");
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dataSource.setUrl(url);
			dataSource.setUsername(username);
			dataSource.setPassword(password);
			dataSource.setInitialSize(initialSize);
			dataSource.setMaxActive(maxActive);
			dataSource.setMaxWait(maxWait);	
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	public static Connection getConn() throws SQLException{
		return dataSource.getConnection();
	}
	public static void close(Connection conn){
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) throws SQLException {
		System.out.println(dataSource.getUrl()+getConn());
	}
}

db.properties

jdbc.url=jdbc:mysql://localhost:3306/gjxfj?useSSL=true
jdbc.username=root
jdbc.password=root
jdbc.initialSize=20
jdbc.maxActive=100
jdbc.maxWait=5000

CartItem.java

package com.jdbc.entity;

import java.io.Serializable;
import java.sql.Timestamp;

public class CartItem implements Serializable{

	private static final long serialVersionUID = 1L;
	private Integer id;
	private Integer userId;
	private Integer itemId;
	private int num;
	/*购物车信息的状态 1.未被删除 2.被删除*/
	private int status;
	private Timestamp created;
	private Timestamp updated;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public Integer getItemId() {
		return itemId;
	}
	public void setItemId(Integer itemId) {
		this.itemId = itemId;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public Timestamp getCreated() {
		return new Timestamp(System.currentTimeMillis());
	}

	public Timestamp getUpdated() {
		return new Timestamp(System.currentTimeMillis());
	}
	@Override
	public String toString() {
		return "CartItem [id=" + id + ", userId=" + userId + ", itemId=" + itemId + ", num=" + num + ", status="
				+ status + ", created=" + created + ", updated=" + updated + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CartItem other = (CartItem) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	public CartItem(Integer id, Integer userId, Integer itemId, int num, int status, Timestamp created,
			Timestamp updated) {
		super();
		this.id = id;
		this.userId = userId;
		this.itemId = itemId;
		this.num = num;
		this.status = status;
		this.created = created;
		this.updated = updated;
	}
	public CartItem() {
		super();
	}
	public CartItem(Integer id, Integer userId, Integer itemId, int num, int status) {
		super();
		this.id = id;
		this.userId = userId;
		this.itemId = itemId;
		this.num = num;
		this.status = status;
	}
	
	
}

CartItemImpl.java

package com.jdbc.dao.impl;

import java.sql.SQLException;

import javax.sql.rowset.CachedRowSet;

import com.jdbc.entity.CartItem;

public class CartItemImpl extends BaseDao{
	/**
	 * 根据用户id和商品id查询购物车信息
	 * @param userId 用户id
	 * @param itemId 商品id
	 * @return CartItem 购物车中商品信息
	 * @throws SQLException 
	 */
	public CartItem findByUserIdItemId(Integer userId,Integer itemId) throws SQLException{
		String sql = "select * from ajia_cart_item where userId = ? and itemId = ?";
		CartItem cartItem = null;
		CachedRowSet rs = findData(sql, userId,itemId);
		if (rs.next()) {
			cartItem = new CartItem(rs.getInt("id"),rs.getInt("userId"),
					rs.getInt("itemId"),rs.getInt("num"),rs.getInt("status"));
		}
		return cartItem;
	}
	/**
	 * 修改购物车商品信息的数量,状态,更新时间
	 * @param cartItem 被修改数量和状态的
	 *                 原购物车商品信息
	 * @return int 更新的记录数
	 */
	public int updateStatus(CartItem cartItem){
		String sql = "update ajia_cart_item set num = ?,status = ?,updated = ? where id = ?";
		return update(sql, cartItem.getNum(),cartItem.getStatus(),cartItem.getUpdated()
				,cartItem.getId());
	}
	/**
	 * 修改购物车商品信息的数量,更新时间
	 * @param cartItem 被添加数量原购物车商品信息
	 * @return int 更新的记录数
	 */
	public int updateNum(CartItem cartItem){
		String sql = "update ajia_cart_item set num = ?,updated = ? where id = ?";
		return update(sql, cartItem.getNum(),cartItem.getUpdated(),cartItem.getId());
	}
	/**
	 * 添加商品到购物车
	 * @param cartItem 购物车信息
	 * @return int 添加信息的数量 
	 */
	
	public int save(CartItem cartItem){
		String sql = "insert into ajia_cart_item(userId,itemId,num,status,created,updated) "
				+ "values(?,?,?,?,?,?)";
		return update(sql, cartItem.getUserId(),cartItem.getItemId(),cartItem.getNum(),
				cartItem.getStatus(),cartItem.getCreated(),cartItem.getUpdated());
		
	}
	public static void main(String[] args) throws SQLException {
		/**
		 * 查询被添加商品是否是同一个用户 
		 * user_id item_id
		 * 
		 * 如果存在数据,再查询购物车信息的状态
		 * 如果status=2,改数量,改状态2-1,改时间
		 * 
		 * 如果status=1,改数量
		 *   (原来的数量+新的数量),改时间
		 * 如果不存在数据,添加信息到购物车
		 */
		CartItemImpl dao = new CartItemImpl();
		Integer userId  = 11;
		Integer itemId = 111;
		int num = 5;
		CartItem cartItem = dao.findByUserIdItemId(userId, itemId);
		if (cartItem!=null) {
			if (cartItem.getStatus()==1) {
				CartItem cartItem1 = new CartItem();
				cartItem1.setId(cartItem.getId());
				cartItem1.setNum(cartItem.getNum()+num);
				dao.updateNum(cartItem1);
			}else if (cartItem.getStatus()==2) {
				CartItem cartItem1 = new CartItem();				
				cartItem1.setId(cartItem.getId());
				cartItem1.setStatus(1);
				cartItem1.setNum(num);
				dao.updateStatus(cartItem1);
			}
		}else{
			CartItem cartItem1 = new CartItem();
			cartItem1.setUserId(userId);
			cartItem1.setItemId(itemId);
			cartItem1.setNum(num);
			cartItem1.setStatus(1);
			dao.save(cartItem1);
		}
		
		
	}
	
	
}

你可能感兴趣的:(模拟用户操作购物车添加商品修改数量)