BannerStudio---第四阶段考核项目(二)

简介: BannerStudio---第四阶段考核项目
  • 实现

1.CommodityDaoImpl

package org.lyl.taobao.dao.impl;
import org.lyl.taobao.api.entity.Commodity;
import org.lyl.taobao.api.utils.DruidConnectionUtils;
import org.lyl.taobao.dao.ICommodityDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/4/2 17:10
 */
public class CommodityDaoImpl implements ICommodityDao {
    DruidConnectionUtils druidConnectionUtils=new DruidConnectionUtils();
    @Override
    public boolean insertCommodity(Connection connection,Commodity commodity) {
        boolean flag = false;
        String sql = "insert into commodity(c_Name,c_Kind,c_Price,c_UserId) values(?,?,?,?);";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,commodity.getCommodityName());
            preparedStatement.setString(2,commodity.getCommodityKind());
            preparedStatement.setFloat(3,commodity.getCommodityPrice());
            preparedStatement.setInt(4,commodity.getCommodityUserId());
            int count = preparedStatement.executeUpdate();
            if (count == 1){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return flag;
    }
    @Override
    public List<Commodity> checkAllCommodity(Connection connection) {
        List<Commodity> commodityList = new ArrayList<>();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from Commodity;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                int c_Id = resultSet.getInt("c_Id");
                String c_Name = resultSet.getString("c_Name");
                String c_Kind = resultSet.getString("c_Kind");
                float c_Price = resultSet.getFloat("c_Price");
                commodityList.add(new Commodity(c_Id,c_Name,c_Kind,c_Price));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return commodityList;
    }
    @Override
    public Commodity checkCommodityByC_Id(Connection connection, Integer c_Id) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from commodity where c_Id = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,c_Id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                Commodity commodity = new Commodity(c_Id,resultSet.getString("c_Name"), resultSet.getString("c_Kind"), resultSet.getFloat("c_Price"), resultSet.getInt("c_UserId"));
                return commodity;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return null;
    }
    @Override
    public List<Commodity> checkCommodityById(Connection connection,Integer UserId) {
        List<Commodity> commodityList = new ArrayList<>();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from commodity where c_UserId = ? ;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,UserId);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                int c_Id = resultSet.getInt("c_Id");
                String c_Name = resultSet.getString("c_Name");
                String c_Kind = resultSet.getString("c_Kind");
                float c_Price = resultSet.getFloat("c_Price");
                commodityList.add(new Commodity(c_Id,c_Name,c_Kind,c_Price));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return commodityList;
    }
    @Override
    public boolean deleteCommodity(Connection connection, int c_id) {
        boolean flag = false;
        PreparedStatement preparedStatement = null;
        String sql = "delete from commodity where c_Id = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,c_id);
            if (preparedStatement.executeUpdate() == 1){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return flag;
    }
    @Override
    public boolean updateCommodity(Connection connection, int c_Id, String c_Name, float c_Price) {
        String sql = "update commodity set c_Name = ?,c_Price = ? where c_Id = ? ;";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,c_Name);
            preparedStatement.setFloat(2,c_Price);
            preparedStatement.setInt(3,c_Id);
            if(preparedStatement.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }
    @Override
    public float getCommodityPriceByC_Id(Connection connection, Integer c_Id) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select c_Price from commodity where c_Id = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,c_Id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                float price = resultSet.getFloat("c_Price");
                return price;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return 0;
    }
}
2.OrderDaoImpl
package org.lyl.taobao.dao.impl;
import org.lyl.taobao.api.entity.Order;
import org.lyl.taobao.api.utils.DruidConnectionUtils;
import org.lyl.taobao.dao.IOrderDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/4/9 20:46
 */
public class OrderDaoImpl implements IOrderDao {
    DruidConnectionUtils druidConnectionUtils=new DruidConnectionUtils();
    @Override
    public boolean addOrder(Connection connection, Order order) {
        PreparedStatement preparedStatement = null;
        String sql = "insert into `order`(userId,c_Id,merchantId) values (?,?,?);";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,order.getOrderUserId());
            preparedStatement.setInt(2,order.getOrderCommodityId());
            preparedStatement.setInt(3,order.getOrderMerchantId());
            if (preparedStatement.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }
    @Override
    public List<Order> checkOrder(Connection connection, Integer userId) {
        List<Order> orderList = new ArrayList<>();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql ="select * from `order` where userId = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                orderList.add(new Order(resultSet.getInt("orderId"),resultSet.getInt("userId"),resultSet.getInt("c_Id"),resultSet.getInt("merchantId")));
            }
            return orderList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return null;
    }
    @Override
    public boolean deleteOrder(Connection connection, Integer orderId) {
        PreparedStatement preparedStatement = null;
        String sql = "delete from `order` where orderId = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,orderId);
            if (preparedStatement.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }
}
3.ShopCartDaoImpl
package org.lyl.taobao.dao.impl;
import org.lyl.taobao.api.entity.ShopCart;
import org.lyl.taobao.api.utils.DruidConnectionUtils;
import org.lyl.taobao.dao.IShopCartDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/4/7 17:32
 */
public class ShopCartDaoImpl implements IShopCartDao {
    DruidConnectionUtils druidConnectionUtils=new DruidConnectionUtils();
    @Override
    public boolean addShopCart(Connection connection, ShopCart shopCart) {
        PreparedStatement preparedStatement = null;
        String sql = "insert into shopingcart(userId,c_Id,merchantId) values (?,?,?);";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,shopCart.getShopCartUserId());
            preparedStatement.setInt(2,shopCart.getShopCartCommodityId());
            preparedStatement.setInt(3,shopCart.getShopCartMerchantId());
            if (preparedStatement.executeUpdate() == 1) {
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }
    @Override
    public List<Integer> checkC_IdByUserId(Connection connection, Integer userId) {
        List<Integer> integerList = new ArrayList<>();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from shopingcart where userId = ?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                integerList.add(resultSet.getInt("c_Id"));
            }
            return integerList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return null;
    }
    @Override
    public boolean deleteShopCart(Connection connection, Integer userId, Integer c_Id) {
        PreparedStatement preparedStatement = null;
        String sql = "delete from shopingcart where userId = ? and c_Id = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            preparedStatement.setInt(2,c_Id);
            if(preparedStatement.executeUpdate() == 1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }
}
4.UserDaoImpl
package org.lyl.taobao.dao.impl;
import org.lyl.taobao.api.entity.User;
import org.lyl.taobao.api.utils.DruidConnectionUtils;
import org.lyl.taobao.dao.IUserDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/3/26 14:15
 */
public class UserDaoImpl implements IUserDao {
    DruidConnectionUtils druidConnectionUtils=new DruidConnectionUtils();
    @Override
    public boolean insertUser(Connection connection, User user) {
        String sql = "insert into user (userName,userPwd,balance,userGrade) values(?,?,?,?);";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getUserPassword());
            preparedStatement.setFloat(3,user.getBalance());
            preparedStatement.setString(4,user.getUserGrade());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            System.out.println("用户添加失败");
            e.printStackTrace();
            return false;
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement, null);
            return true;
        }
    }
    @Override
    public User registerUser(Connection connection, User user) {
        String sql = "select * from `user` where username = ? and userPwd = ? and userGrade = ?;";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user1 = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getUserPassword());
            preparedStatement.setString(3, user.getUserGrade());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                Integer userId = resultSet.getInt("userId");
                String imgPath = resultSet.getString("img");
                String userName = resultSet.getString("userName");
                String userPassword = resultSet.getString("userPwd");
                float balance = resultSet.getFloat("balance");
                String userGrade = resultSet.getString("userGrade");
                System.out.println( "dao--->imgPath"+imgPath);
                System.out.println( "dao--->Id"+userId);
                System.out.println( "dao--->balance"+balance);
                System.out.println( "dao--->userPassword"+userPassword);
                user1 = new User(userId,imgPath,userName,userPassword,balance,userGrade);
            }
            return user1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
    }
    @Override
    public List<User> checkAllUser(Connection connection) {
        List<User> userList = new ArrayList<>();
        String sql = "select * from user where userGrade = ?;";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"1");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int userId = resultSet.getInt("userId");
                String userName = resultSet.getString("userName");
                String userPwd = resultSet.getString("userPwd");
                int balance = resultSet.getInt("balance");
                String userGrade = resultSet.getString("userGrade");
                userList.add(new User(userId,userName,userPwd,balance,userGrade));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return userList;
    }
    @Override
    public List<User> checkUser(Connection connection, String userName) {
        List<User> userList = new ArrayList<>();
        String sql = "select * from user where userName = ?;";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                int userId = resultSet.getInt("userId");
                String name = resultSet.getString("userName");
                String userPwd = resultSet.getString("userPwd");
                int balance = resultSet.getInt("balance");
                String userGrade = resultSet.getString("userGrade");
                userList.add(new User(userId,name,userPwd,balance,userGrade));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return userList;
    }
    @Override
    public boolean deleteUser(Connection connection,String userName) {
        boolean flag = false;
        String sql = "delete from user where userName = ? and userGrade = ?;";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            preparedStatement.setString(2,"1");
            int count = preparedStatement.executeUpdate();
            if (count == 1) {
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return flag;
    }
    @Override
    public boolean updateUser(Connection connection,User user) {
        boolean flag = false;
        String sql = "update user set userPwd = ? where userName = ? and userGrade = ? ;";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUserPassword());
            preparedStatement.setString(2,user.getUserName());
            preparedStatement.setString(3, user.getUserGrade());
            if (preparedStatement.executeUpdate() == 1){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return flag;
    }
    @Override
    public Integer getUserIdByC_Id(Connection connection, Integer c_Id) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select c_UserId from commodity where c_Id = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,c_Id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                Integer userId = resultSet.getInt("c_UserId");
                return userId;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return null;
    }
    @Override
    public float getUserBalanceByUserId(Connection connection, Integer userId) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select balance from user where userId = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                float balance = resultSet.getFloat("balance");
                return balance;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return 0;
    }
    @Override
    public boolean reduceBalanceByUserId(Connection connection,float price,Integer userId) {
        PreparedStatement preparedStatement = null;
        String sql = "update user set balance = balance - "+ price +" where userId = ?;" ;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            if (preparedStatement.executeUpdate() == 1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }
    @Override
    public boolean addBalanceByUserId(Connection connection,float price,Integer userId) {
        PreparedStatement preparedStatement = null;
        String sql = "update user set balance = balance + "+ price +" where userId = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            if (preparedStatement.executeUpdate() == 1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }
    @Override
    public boolean addPhoto(Connection connection, String imgPath,User user) {
        PreparedStatement preparedStatement = null;
        String sql = "update `user` set img = ? where userId = ?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,imgPath);
            preparedStatement.setInt(2,user.getUserId());
            if (preparedStatement.executeUpdate() == 1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }
}

service层

  • 实现
    1.CommodityServiceImpl
package org.lyl.taobao.service;
import org.lyl.taobao.api.ICommodityService;
import org.lyl.taobao.api.entity.Commodity;
import org.lyl.taobao.api.utils.DruidConnectionUtils;
import org.lyl.taobao.dao.ICommodityDao;
import org.lyl.taobao.dao.impl.CommodityDaoImpl;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/4/2 17:26
 */
public class CommodityServiceImpl implements ICommodityService {
    DruidConnectionUtils druidConnectionUtils=new DruidConnectionUtils();
    ICommodityDao iCommodityDao = new CommodityDaoImpl();
    public boolean addCommodity(Commodity commodity) {
        return iCommodityDao.insertCommodity(druidConnectionUtils.getConnection(),commodity);
    }
    public List<Commodity> checkAllCommodity() {
        return iCommodityDao.checkAllCommodity(druidConnectionUtils.getConnection());
    }
    public Commodity checkCommodityByC_Id(Integer c_Id) {
        return iCommodityDao.checkCommodityByC_Id(druidConnectionUtils.getConnection(), c_Id);
    }
    public List<Commodity> checkCommodityById(Integer UserId) {
        return iCommodityDao.checkCommodityById(druidConnectionUtils.getConnection(),UserId);
    }
    public boolean deleteCommodity(int c_Id) {
        return iCommodityDao.deleteCommodity(druidConnectionUtils.getConnection(),c_Id);
    }
    public boolean updateCommodity(int c_Id, String c_Name, float c_Price) {
        return iCommodityDao.updateCommodity(druidConnectionUtils.getConnection(),c_Id,c_Name,c_Price);
    }
    public float getCommodityPriceByC_Id(Integer c_Id) {
        return iCommodityDao.getCommodityPriceByC_Id(druidConnectionUtils.getConnection(),c_Id);
    }
}
2.OrderServiceImpl
package org.lyl.taobao.service;
import org.lyl.taobao.api.IOrderService;
import org.lyl.taobao.api.entity.Order;
import org.lyl.taobao.api.utils.DruidConnectionUtils;
import org.lyl.taobao.dao.IOrderDao;
import org.lyl.taobao.dao.impl.OrderDaoImpl;
import java.sql.Connection;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/4/9 20:47
 */
public class OrderServiceImpl implements IOrderService {
    DruidConnectionUtils druidConnectionUtils=new DruidConnectionUtils();
    IOrderDao iOrderDao = new OrderDaoImpl();
    public boolean addOrder(Order order) {
        return iOrderDao.addOrder(druidConnectionUtils.getConnection(),order);
    }
    public List<Order> checkOrder(Integer userId) {
        return iOrderDao.checkOrder(druidConnectionUtils.getConnection(),userId);
    }
    public boolean deleteOrder(Integer orderId) {
        return iOrderDao.deleteOrder(druidConnectionUtils.getConnection(),orderId);
    }
}
3.ShopCartServiceImpl
package org.lyl.taobao.service;
import org.lyl.taobao.api.IShopCartService;
import org.lyl.taobao.api.entity.ShopCart;
import org.lyl.taobao.api.utils.DruidConnectionUtils;
import org.lyl.taobao.dao.IShopCartDao;
import org.lyl.taobao.dao.impl.ShopCartDaoImpl;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/4/7 17:33
 */
public class ShopCartServiceImpl implements IShopCartService {
    DruidConnectionUtils druidConnectionUtils=new DruidConnectionUtils();
    IShopCartDao iShopCartDao = new ShopCartDaoImpl();
    public boolean addShopCart(ShopCart shopCart) {
        return iShopCartDao.addShopCart(druidConnectionUtils.getConnection(),shopCart);
    }
    public List<Integer> checkC_IdByUserId(Integer userId) {
        return iShopCartDao.checkC_IdByUserId(druidConnectionUtils.getConnection(),userId);
    }
    public boolean deleteShopCart(Integer userId, Integer c_Id) {
        return iShopCartDao.deleteShopCart(druidConnectionUtils.getConnection(),userId,c_Id);
    }
}
4.UserServiceImpl
package org.lyl.taobao.service;
import org.lyl.taobao.api.IUserService;
import org.lyl.taobao.api.entity.User;
import org.lyl.taobao.api.utils.DruidConnectionUtils;
import org.lyl.taobao.dao.IUserDao;
import org.lyl.taobao.dao.impl.UserDaoImpl;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/3/26 19:15
 */
public class UserServiceImpl implements IUserService {
    IUserDao iUserDao=new UserDaoImpl();
    DruidConnectionUtils druidConnectionUtils=new DruidConnectionUtils();
    public boolean insertUser(User user) {
        return iUserDao.insertUser(druidConnectionUtils.getConnection(),user);
    }
    public User registerUser(User user) {
        return iUserDao.registerUser(druidConnectionUtils.getConnection(),user);
    }
    public List<User> checkAllUser() {
        return iUserDao.checkAllUser(druidConnectionUtils.getConnection());
    }
    public List<User> checkUser(String userName) {
        return iUserDao.checkUser(druidConnectionUtils.getConnection(),userName);
    }
    public boolean deleteUser(String userName) {
        return iUserDao.deleteUser(druidConnectionUtils.getConnection(),userName);
    }
    public boolean updateUser(User user) {
        return iUserDao.updateUser(druidConnectionUtils.getConnection(),user);
    }
    public Integer userIdByC_Id(Integer c_Id) {
        return iUserDao.getUserIdByC_Id(druidConnectionUtils.getConnection(),c_Id);
    }
    public float getUserBalanceByUserId(Integer userId) {
        return iUserDao.getUserBalanceByUserId(druidConnectionUtils.getConnection(), userId);
    }
    public boolean reduceBalanceByUserId(float price, Integer userId) {
        return iUserDao.reduceBalanceByUserId(druidConnectionUtils.getConnection(),price,userId);
    }
    public boolean addBalanceByUserId(float price, Integer userId) {
        return iUserDao.addBalanceByUserId(druidConnectionUtils.getConnection(),price,userId);
    }
    public boolean addPhoto(String imgPath,User user) {
        return iUserDao.addPhoto(druidConnectionUtils.getConnection(),imgPath,user);
    }
}

controller层

  • filter
    1.AccessToIntercept
package org.lyl.taobao.controller.filter;
import org.lyl.taobao.api.entity.User;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @Author: Re
 * @Date: 2021/4/16 11:05
 */
@WebFilter("/*")
public class AccessToIntercept implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化访问拦截");
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request1 = (HttpServletRequest) request;
        HttpServletResponse response1 = (HttpServletResponse) response;
        String path = String.valueOf(request1.getRequestURL());
        if (path.contains("index.jsp")||path.contains("/login.jsp")||path.contains("/registerUser.jsp")||path.contains("/upload")||path.contains("/registerUser")){
            chain.doFilter(request1,response1);
            return;
        }
        User user = (User) request1.getSession().getAttribute("user");
        if (user != null) {
            chain.doFilter(request1,response1);
            return;
        }
        response1.sendRedirect("registerUser.jsp");
    }
    @Override
    public void destroy() {
        System.out.println("拦截器被销毁");
    }
}
2.EncodeFilter
package org.lyl.taobao.controller.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @Author: Re
 * @Date: 2021/4/16 10:52
 */
@WebFilter("/*")
public class EncodeFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("");
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request1 = (HttpServletRequest) request;
        HttpServletResponse response1 = (HttpServletResponse) response;
        request1.setCharacterEncoding("UTF-8");
        response1.setContentType("text/html;utf-8");
        chain.doFilter(request1,response1);
    }
    @Override
    public void destroy() {
        System.out.println();
    }
}
  • listener
    1.OnlineListener
package org.lyl.taobao.controller.Listener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
 * @Author: Re
 * @Date: 2021/4/16 20:33
 */
@WebListener
public class OnlineListener implements HttpSessionListener {
    private int onlineNumber = 0;
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        onlineNumber++;
        se.getSession().getServletContext().setAttribute("onlineNumber",onlineNumber);
    }
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        onlineNumber--;
        se.getSession().getServletContext().setAttribute("onlineNumber",onlineNumber);
    }
}
  • 各种servlet文件
    1.AddCommodityServlet
package org.lyl.taobao.controller;
import org.lyl.taobao.api.ICommodityService;
import org.lyl.taobao.api.entity.Commodity;
import org.lyl.taobao.service.CommodityServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @Author: Re
 * @Date: 2021/4/2 17:52
 */
@WebServlet("/addCommodity")
public class AddCommodityServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(AddCommodityServlet.class);
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String c_Name = req.getParameter("c_Name");
        System.out.println(c_Name);
        String c_Kind = req.getParameter("c_Kind");
        String c_price = req.getParameter("c_Price");
        Integer c_UserId = Integer.parseInt(req.getParameter("userId"));
        float price = Float.valueOf(c_price);
        Commodity commodity = new Commodity(c_Name,c_Kind,price,c_UserId);
        logger.info(commodity.toString());
        ICommodityService iCommodityService = new CommodityServiceImpl();
        boolean judgment = iCommodityService.addCommodity(commodity);
        if (judgment) {
            req.setAttribute("msg","商品添加成功");
        } else {
            req.setAttribute("msg","商品添加失败");
        }
        req.getRequestDispatcher("checkCommodity.jsp").forward(req,resp);
    }
}
2.AddShopCartServlet
package org.lyl.taobao.controller;
import org.lyl.taobao.api.ICommodityService;
import org.lyl.taobao.api.IOrderService;
import org.lyl.taobao.api.IShopCartService;
import org.lyl.taobao.api.IUserService;
import org.lyl.taobao.api.entity.Commodity;
import org.lyl.taobao.api.entity.Order;
import org.lyl.taobao.api.entity.ShopCart;
import org.lyl.taobao.api.entity.User;
import org.lyl.taobao.service.CommodityServiceImpl;
import org.lyl.taobao.service.OrderServiceImpl;
import org.lyl.taobao.service.ShopCartServiceImpl;
import org.lyl.taobao.service.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/4/7 21:00
 */
@WebServlet("/addShopCart")
public class AddShopCartServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        IUserService iUserService = new UserServiceImpl();
        IShopCartService iShopCartService = new ShopCartServiceImpl();
        ICommodityService iCommodityService = new CommodityServiceImpl();
        IOrderService iOrderService = new OrderServiceImpl();
        if (req.getParameter("collect") != null) {
            Integer c_Id = Integer.parseInt(req.getParameter("collect"));
            Integer merchantId = iUserService.userIdByC_Id(c_Id);
            ShopCart shopCart = new ShopCart(userId,c_Id,merchantId);
            if (iShopCartService.addShopCart(shopCart)) {
                req.setAttribute("msg","已添加到购物车");
            } else {
                req.setAttribute("msg","添加购物车失败");
            }
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        } else if (req.getParameter("buy") != null){
            Integer c_Id = Integer.parseInt(req.getParameter("buy"));
            Integer merchantId = iUserService.userIdByC_Id(c_Id);
            Order order = new Order(userId,c_Id,merchantId);
            float balance = iUserService.getUserBalanceByUserId(userId);
            float price = iCommodityService.getCommodityPriceByC_Id(c_Id);
            if (balance >= price) {
                int num = 0;
                if (iUserService.reduceBalanceByUserId(price,userId)){
                    num++;
                }
                if (iUserService.addBalanceByUserId(price,merchantId)) {
                    num++;
                }
                if (num == 2){
                    if (iOrderService.addOrder(order)) {
                        req.setAttribute("msg","购买成功");
                        req.getRequestDispatcher("index.jsp").forward(req,resp);
                    }
                }
            } else {
                req.setAttribute("msg","您没钱了");
                req.getRequestDispatcher("index.jsp").forward(req,resp);
            }
        } else if (req.getParameter("intoCollect") != null){
            List<Integer> c_Ids = iShopCartService.checkC_IdByUserId(userId);
            List<Commodity> commodities = new ArrayList<>();
            for (int i = 0; i < c_Ids.size(); i++) {
                Commodity commodity = iCommodityService.checkCommodityByC_Id(c_Ids.get(i));
                commodities.add(commodity);
            }
            HttpSession session1 = req.getSession();
            session1.setAttribute("commoditiesMsg",commodities);
            req.getRequestDispatcher("collect.jsp").forward(req,resp);
        } else if (req.getParameter("homepage") != null){
            req.getRequestDispatcher("homepage.jsp").forward(req,resp);
        } else {
            List<Order> orderList = iOrderService.checkOrder(userId);
            List<Commodity> commodityList = new ArrayList<>();
            for (int i = 0; i < orderList.size(); i++) {
                commodityList.add(iCommodityService.checkCommodityByC_Id(orderList.get(i).getOrderCommodityId()));
            }
            System.out.println(commodityList.size());
            HttpSession session2 = req.getSession();
            session2.setAttribute("orderListMsg",orderList);
            session2.setAttribute("commodityListMsg",commodityList);
            req.getRequestDispatcher("checkOrder.jsp").forward(req,resp);
        }
    }
}
3.CheckCommodityServlet
package org.lyl.taobao.controller;
import org.lyl.taobao.api.ICommodityService;
import org.lyl.taobao.api.entity.Commodity;
import org.lyl.taobao.service.CommodityServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/4/3 15:25
 */
@WebServlet("/checkCommodity")
public class CheckCommodityServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ICommodityService iCommodityService = new CommodityServiceImpl();
        String check = req.getParameter("check");
        String userId = req.getParameter("userId");
        if (userId != null) {
            Integer id = Integer.valueOf(userId);
            List<Commodity> commodityList = iCommodityService.checkCommodityById(id);
            req.setAttribute("listMsg",commodityList);
            req.getRequestDispatcher("checkCommodity.jsp").forward(req,resp);
            return;
        }
        if (check != null) {
            if (check.equals("deleteCommodity")) {
                int c_Id;
                String[] strings = req.getParameterValues("deleteCommodityById");
                if (strings != null) {
                    for (int i = 0; i < strings.length; i++) {
                        c_Id = Integer.valueOf(strings[i]);
                        iCommodityService.deleteCommodity(c_Id);
                    }
                }
                req.getRequestDispatcher("checkCommodity.jsp").forward(req,resp);
                return;
            } else if (check.equals("addCommodity")){
                req.getRequestDispatcher("addCommodity.jsp").forward(req,resp);
                return;
            }
        }
        String c_Id = req.getParameter("updateCommodityById");
        req.setAttribute("c_Id",c_Id);
        req.getRequestDispatcher("updateCommodity.jsp").forward(req,resp);
    }
}
4.CheckShopCartServlet
package org.lyl.taobao.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @Author: Re
 * @Date: 2021/4/8 15:57
 */
@WebServlet("/")
public class CheckShopCartServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(InsertUserServlet.class);
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;utf-8");
    }
}
5.CheckUserServlet
package org.lyl.taobao.controller;
import org.lyl.taobao.api.IUserService;
import org.lyl.taobao.service.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/3/30 17:40
 */
@WebServlet("/checkUser")
public class CheckUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List users = null;
        String checkUser = req.getParameter("check");
        IUserService iUserService = new UserServiceImpl();
        if(checkUser.equals("allUsers")) {
            users = iUserService.checkAllUser();
            req.setAttribute("msg",users);
            req.getRequestDispatcher("checkUser.jsp").forward(req,resp);
        } else if (checkUser.equals("user")) {
            String userName = req.getParameter("checkUserName");
            users =  iUserService.checkUser(userName);
            if (users == null) {
                req.setAttribute("warning","用户名错误");
                req.getRequestDispatcher("checkUser.jsp").forward(req,resp);
                return;
            }
            req.setAttribute("msg",users);
            req.getRequestDispatcher("checkUser.jsp").forward(req,resp);
        }else if (checkUser.equals("deleteUser")){
            String[] strings = req.getParameterValues("deleteUserName");
            if(strings != null){
                for (int i = 0; i < strings.length; i++) {
                    iUserService.deleteUser(strings[i]);
                }
            }
            req.getRequestDispatcher("checkUser.jsp").forward(req,resp);
        }
    }
}
6.DeleteOrderServlet
package org.lyl.taobao.controller;
import org.lyl.taobao.api.IOrderService;
import org.lyl.taobao.service.OrderServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @Author: Re
 * @Date: 2021/4/11 9:47
 */
@WebServlet("/deleteOrder")
public class DeleteOrderServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer orderId = Integer.parseInt(req.getParameter("cancel"));
        IOrderService iOrderService = new OrderServiceImpl();
        if (iOrderService.deleteOrder(orderId)) {
            req.setAttribute("msg","订单取消成功");
        } else {
            req.setAttribute("msg","订单取消失败");
        }
        req.getRequestDispatcher("checkOrder.jsp").forward(req,resp);
    }
}
相关文章
|
8月前
|
存储 监控 搜索推荐
第十二章项目沟通管理和干系人管理(选择2分,偶尔案例20分)
第十二章项目沟通管理和干系人管理(选择2分,偶尔案例20分)
190 0
|
8月前
|
安全
第五章项目立项管理(选择4分,案例偶尔考)
第五章项目立项管理(选择4分,案例偶尔考)
|
8月前
|
项目管理 计算机视觉
软考系统集成知识精讲第九章项目成本管理(选择4分,案例13分)
软考系统集成知识精讲第九章项目成本管理(选择4分,案例13分)
|
项目管理
【项目开发计划制定工作经验之谈】
【项目开发计划制定工作经验之谈】
130 1
|
程序员 开发工具
[老文拾遗]如果我当上技术经理如何展开工作(三)
[老文拾遗]如果我当上技术经理如何展开工作(三)
|
前端开发 Java API
BannerStudio---第四阶段考核项目(一)
BannerStudio---第四阶段考核项目
BannerStudio---第四阶段考核项目(一)
|
数据采集 运维 数据管理
信息部门牵头实施的主数据项目效果不好,如何挽救?
最近和大型企业的信息部门就主数据项目的实施进行了很多次交流,大家普遍存在一个非常困扰问题:信息部门牵头实施的主数据效果不好,怎么去挽救?怎么去改善?
信息部门牵头实施的主数据项目效果不好,如何挽救?

热门文章

最新文章