首页 > 基础资料 博客日记

SMBMS超市管理系统(Javaweb项目)

2023-07-29 18:01:31基础资料围观228

Java资料网推荐SMBMS超市管理系统(Javaweb项目)这篇文章给大家,欢迎收藏Java资料网享受知识的乐趣

SMBMS超市管理系统(Javaweb项目)

项目环境

  1. jdk 1.8.0_261
  2. mysql 8.0.21
  3. IDEA 2021.2.2

项目架构



数据库

项目源代码

filer

字符编码过滤器

package filer;
import javax.servlet.*;
import java.io.IOException;
    
public class CharacterEncodingFilter implements Filter {
    /*
    字符编码过滤器
    */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}

用户登录过滤器

 package filer;
import pojo.User;
import util.Constants;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SysFilter implements Filter {
    /*
   登录拦截
   */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        //过滤器,从Session中获取用户
        User user = (User) request.getSession().getAttribute(Constants.USER_SESSION);

        //已经被移除或者注销了,或者未登录
        if (user == null) {
            response.sendRedirect("/smbms/error.jsp");
        } else {
            chain.doFilter(req, resp);
        }

    }

    @Override
    public void destroy() {
    }
}

pojo层

package pojo;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;

@Data
public class Bill {
    private Integer id; //id
    private String billCode; //账单编码
    private String productName; //商品名称
    private String productDesc; //商品描述
    private String productUnit; //商品单位
    private BigDecimal productCount; //商品数量
    private BigDecimal totalPrice; //总金额
    private Integer isPayment; //是否支付
    private Integer providerId; // 供应商ID
    private Integer createdBy; //创建者
    private Date creationDate; //创建时间
    private Integer modifyBy; // 更新者
    private Date modifyDate; //更新时间
    private String providerName; //供应商名称
}
package pojo;
import lombok.Data;
import java.util.Date;

@Data
public class Provider {
    private Integer id; //id
    private String proCode;  //供应商编码
    private String proName;  //供应商名称
    private String proDesc; //供应商描述
    private String proContact;  // 供应商联系人
    private String proPhone; //供应商电话
    private String proAddress;  // 供应商地址
    private String proFax;  //供应商传真
    private Integer createdBy;  // 创建者
    private Date creationDate;  // 创建时间
    private Integer modifyBy;  //更新者
    private Date modifyDate;  //更新时间
}
package pojo;
import lombok.Data;
import java.util.Date;

@Data
public class Role {
    private  Integer id; //id
    private String roleCode; //角色编码
    private String roleName; //角色名称
    private Integer createdBy; //创建者
    private Date creationDate; //创建时间
    private Integer modifyBy; //更新者
    private Date modifyDate; //更新时间
}
package pojo;

import lombok.Data;

import java.util.Date;

@Data
public class User {
    private Integer id; //id
    private String userCode; //用户编码
    private String userName; //用户名称
    private String userPassword; //用户密码
    private Integer gender; //性别
    private Date birthday; //出生日期
    private String phone; //电话
    private String address; //地址
    private Integer userRole; //用户角色
    private Integer createdBy; //创建者
    private Date creationDate; //创建时间
    private Integer modifyBy; //更新者
    private Date modifyDate; //更新时间
    private Integer age; //年龄
    private String userRoleName; //用户角色名称

    public String getUserRoleName() {
        return userRoleName;
    }

    public void setUserRoleName(String userRoleName) {
        this.userRoleName = userRoleName;
    }

    public Integer getAge() {
        Date date = new Date();
        Integer age = date.getYear() - birthday.getYear();
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

Service层

bill

package service.bill;
import pojo.Bill;
import java.util.List;

public interface BillService {
    
    public List<Bill> getBillList(String queryProductName, String providerList_id, String queryIsPayment01);

    public boolean add(Bill bill);

    public Bill getBill(String billCode);

    public boolean deleteBill(String billCode);

    public int updateBill(Bill bill);
}
package service.bill;
import dao.BaseDao;
import dao.bill.BillDao;
import dao.bill.BillDaoImpl;
import org.junit.jupiter.api.Test;
import pojo.Bill;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BillServiceImpl implements BillService {
    //业务层都会调用dao层,所以我们要引入Dao层;
    private BillDao billDao;

    public BillServiceImpl() {
        billDao = new BillDaoImpl();
    }

    @Override
    public List<Bill> getBillList(String queryProductName, String providerList_id, String queryIsPayment01) {
        Connection connection = null;
        List<Bill> billList = null;
        System.out.println("queryProductName--->" + queryProductName);
        System.out.println("providerList_id--->" + providerList_id);
        System.out.println("queryIsPayment01--->" + queryIsPayment01);
        try {
            connection = BaseDao.getConnection();
            billList = billDao.getBillList(connection, queryProductName, providerList_id, queryIsPayment01);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return billList;

    }

    @Override
    public boolean add(Bill bill) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务管理
            int updateRows = billDao.add(connection, bill);
            connection.commit();
            if (updateRows > 0) {
                flag = true;
                System.out.println("add success");
            } else {
                System.out.println("add failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("rollback=====");
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public Bill getBill(String billCode) {
        Connection connection = null;
        Bill bill = null;
        try {
            connection = BaseDao.getConnection();
            bill = billDao.getBill(connection, billCode);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return bill;
    }

    @Override
    public boolean deleteBill(String billCode) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if (billDao.deleteUserById(connection, billCode) > 0)
                flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public int updateBill(Bill bill) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = billDao.updateBill(connection, bill);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return count;
    }

    /*
    测试方法
    */
    @Test
    public void test1() {
        BillServiceImpl billService = new BillServiceImpl();
        Bill bill = new Bill();
        bill.setBillCode("11111");
        int count = billService.updateBill(bill);
        System.out.println(count);
    }
    @Test
    public void test() {
        BillServiceImpl billService = new BillServiceImpl();
        Bill bill = billService.getBill("BILL2016_002");
        System.out.println(bill);
    }

}

provider

package service.provider;
import pojo.Provider;
import java.util.List;

public interface ProviderServiceDao {
    //订单页面获取供应商列表
    public List<Provider> getProviderList();

    //获取全部供应商信息
    public List<Provider> getProviderList_Pro(String queryProCode,String queryProName);

    public boolean add(Provider provider);

    public Provider getProvider(String proid);

   public int updateProvider(Provider provider);

    public boolean deleteProvider(String proid);
}
package service.provider;
import dao.BaseDao;
import dao.provider.ProviderDao;
import dao.provider.ProviderDaoImpl;
import org.junit.jupiter.api.Test;
import pojo.Provider;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ProviderServiceImpl implements ProviderServiceDao {
    private ProviderDao providerDao;

    public ProviderServiceImpl() {
        providerDao = new ProviderDaoImpl();
    }

    @Override
    public List<Provider> getProviderList() {
        Connection connection = null;
        List<Provider> providerList = null;
        try {
            connection = BaseDao.getConnection();
            providerList = providerDao.getProviderList(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return providerList;
    }

    @Override
    public List<Provider> getProviderList_Pro(String queryProCode, String queryProName) {
        Connection connection = null;
        List<Provider> providerList = null;
        try {
            connection = BaseDao.getConnection();
            providerList = providerDao.getProviderList_Pro(connection, queryProCode, queryProName);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return providerList;
    }

    @Override
    public boolean add(Provider provider) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务管理
            int updateRows = providerDao.add(connection, provider);
            connection.commit();
            if (updateRows > 0) {
                flag = true;
                System.out.println("add success");
            } else {
                System.out.println("add failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("rollback=====");
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public Provider getProvider(String proid) {
        Connection connection = null;
        Provider provider = null;
        try {
            connection = BaseDao.getConnection();
            provider = providerDao.getProvider(connection, proid);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return provider;
    }

    @Override
    public int updateProvider(Provider provider) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = providerDao.updateProvider(connection, provider);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return count;
    }

    @Override
    public boolean deleteProvider(String proid) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if (providerDao.deleteProviderById(connection, proid) > 0)
                flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    /*
    测试方法
    */
    @Test
    public void test() {
        ProviderServiceImpl providerService = new ProviderServiceImpl();
        List<Provider> providerList = providerService.getProviderList();
        for (Provider provider : providerList
        ) {
            System.out.println(provider.getProName());

        }
    }
}

role

package service.role;
import pojo.Role;
import java.util.List;
public interface RoleService {
    //获取角色列表
    public List<Role> getRoleList();
}
package service.role;
import dao.BaseDao;
import dao.role.RoleDao;
import dao.role.RoleDaoImpl;
import org.junit.jupiter.api.Test;
import pojo.Role;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class RoleServiceImpl implements RoleService {
    /*
    引入Dao
    */
    private RoleDao roleDao;

    public RoleServiceImpl() {
        roleDao = new RoleDaoImpl();
    }
    
    @Override
    public List<Role> getRoleList() {
        Connection connection = null;
        List<Role> roleList = null;
        try {
            connection = BaseDao.getConnection();
            roleList = roleDao.getRoleList(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return roleList;
    }

    /*
    测试方法
    */

    @Test
    public void test() {
        RoleServiceImpl roleService = new RoleServiceImpl();
        List<Role> roleList = roleService.getRoleList();
        for (Role role : roleList
        ) {
            System.out.println(role.getRoleName());

        }
    }
}

user

package service.user;
import pojo.User;
import java.util.List;

public interface UserService {
    //用户登录
    public User login(String userCode, String password);

    //根据用户ID修改密码
    public boolean updatePwd(int id, String pwd);

    //查询记录数
    public int getUserCount(String username, int userRole);

    //根据条件查询用户列表
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize);

    //增加用户信息
    public boolean add(User user);

    //删除用户
    public boolean deleteUserById(Integer delId);

    //查看用户信息
    public User getUser(int userID);

    public int updateUser(User user);
}
package service.user;

import dao.BaseDao;
import dao.user.UserDao;
import dao.user.UserDaoImpl;
import org.junit.jupiter.api.Test;
import pojo.User;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {

    //业务层都会调用dao层,所以我们要引入Dao层;
    private UserDao userDao;
    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    /*
        用户登录
    */
    @Override
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            //通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection, userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    @Override
    public boolean updatePwd(int id, String pwd) {
        Connection connection = null;
        boolean flag = false;
        
        //修改密码
        try {
            connection = BaseDao.getConnection();
            if (userDao.updatePwd(connection, id, pwd) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public int getUserCount(String username, int userRole) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection, username, userRole);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return count;
    }

    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName, queryUserRole, currentPageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return userList;
    }

    @Override
    public boolean add(User user) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务管理
            int updateRows = userDao.add(connection, user);
            connection.commit();
            if (updateRows > 0) {
                flag = true;
                System.out.println("add success");
            } else {
                System.out.println("add failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("rollback=====");
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public boolean deleteUserById(Integer delId) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if (userDao.deleteUserById(connection, delId) > 0)
                flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public User getUser(int userID) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUser(connection, userID);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    @Override
    public int updateUser(User user) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.updateUser(connection, user);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return count;
    }
    
    /*
    测试方法
    * */
    @Test
    public void test01(){
        UserServiceImpl userService = new UserServiceImpl();
        int userCount = userService.getUserCount(null, 3);
        System.out.println(userCount);
    }
    @Test
    public void test02(){
        UserServiceImpl userService = new UserServiceImpl();
        User admin = userService.login("admin", "1346");
        System.out.println(admin.getUserPassword());

    }

}


Servlet层

LoginServlet

package servlet.user;
import pojo.User;
import service.user.UserServiceImpl;
import util.Constants;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    /*
     登录
     Servlet:控制层,调用业务层代码
    */

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LoginServlet ---start...");
        //获取前端输入的用户名和密码
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");

        //和数据库中的密码进行对比,调用业务层;
        UserServiceImpl userService = new UserServiceImpl();
        User user = userService.login(userCode, userPassword);

        //与数据库的数据对比,检验密码是否正确
        if (user != null && userPassword.equals(user.getUserPassword())) {
            //查有此人,可以登录
            //将用户的信息放到Session中;
            req.getSession().setAttribute(Constants.USER_SESSION, user);
            //跳转到主页
            resp.sendRedirect("jsp/frame.jsp");
        } else {
           /*
           查无此人,无法登录
           转发回登录页面,顺带提示它,用户名或者密码错误
           */
            req.setAttribute("error", "用户名或者密码不正确");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

LogoutServlet

package servlet.user;
import util.Constants;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LogoutServlet extends HttpServlet {
    /*
        注销
    */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        移除用户的Constants.USER_SESSION
        并转发回登录页面
        */
        req.getSession().removeAttribute(Constants.USER_SESSION);
        resp.sendRedirect(req.getContextPath() + "/login.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

UserServlet

package servlet.user;
import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.util.StringUtils;
import pojo.Role;
import pojo.User;
import service.role.RoleServiceImpl;
import service.user.UserService;
import service.user.UserServiceImpl;
import util.Constants;
import util.PageSupport;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("savepwd") && !StringUtils.isNullOrEmpty(method)) {
            //修改密码
            this.updatePwd(req, resp);
        } else if (method.equals("pwdmodify") && !StringUtils.isNullOrEmpty(method)) {
            //验证旧密码,session中有用户的密码
            this.pwdModify(req, resp);
        } else if (method.equals("query") && !StringUtils.isNullOrEmpty(method)) {
            //查
            this.query(req, resp);
        } else if (method.equals("add") && !StringUtils.isNullOrEmpty(method)) {
            //增
            this.add(req, resp);
        } else if (method.equals("deluser") && !StringUtils.isNullOrEmpty(method)) {
            //删
            this.delUser(req, resp);
        } else if (method.equals("view") && !StringUtils.isNullOrEmpty(method)) {
            //查看详情信息
            this.getUser(req, resp, "userview.jsp");
        } else if (method.equals("modify") && !StringUtils.isNullOrEmpty(method)) {
            //得到要修改的对象
            this.getUser02(req, resp, "usermodify.jsp");
        } else if (method.equals("modifyexe") && !StringUtils.isNullOrEmpty(method)) {
            //改
            this.updateUser(req, resp);
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }


    //修改用户
    private void updateUser(HttpServletRequest req, HttpServletResponse resp) {
        String uid = req.getParameter("uid");
        String userName = req.getParameter("userName");
        String gender = req.getParameter("gender");
        String birthday = req.getParameter("birthday");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String userRole = req.getParameter("userRole");
        User user = null;
        try {
            if (!StringUtils.isNullOrEmpty(uid)) {
                user = new User();
                user.setUserName(userName);
                user.setGender(Integer.valueOf(gender));
                user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
                user.setPhone(phone);
                user.setAddress(address);
                user.setUserRole(Integer.valueOf(userRole));
                user.setId(Integer.valueOf(uid));
                Object o = req.getSession().getAttribute(Constants.USER_SESSION);
                user.setModifyBy(((User) o).getId());
                user.setModifyDate(new Date());
                UserServiceImpl userService = new UserServiceImpl();
                int i = userService.updateUser(user);
                if (i == 1) {
                    //query
                    resp.sendRedirect(req.getContextPath() + "/jsp/user.do?method=query&uid=" + uid);
                    //req.getRequestDispatcher("/jsp/user.do?method=view&uid="+uid).forward(req,resp);
                    /*resp.sendRedirect(req.getContextPath()+"/jsp/user.do?method=view&uid="+uid);*/
                } else {
                    req.getRequestDispatcher("usermodify.jsp").forward(req, resp);
                }
            } else {
                req.getRequestDispatcher("usermodify.jsp").forward(req, resp);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*修改用户*/
    private void getUser02(HttpServletRequest req, HttpServletResponse resp, String url) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        if (!StringUtils.isNullOrEmpty(uid)) {
            UserServiceImpl userService = new UserServiceImpl();
            User user = userService.getUser(Integer.parseInt(uid));
            req.setAttribute("user", user);
            req.getRequestDispatcher(url).forward(req, resp);
        }

    }

    /*查看用户*/
    private void getUser(HttpServletRequest req, HttpServletResponse resp, String url) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        if (!StringUtils.isNullOrEmpty(uid)) {
            UserServiceImpl userService = new UserServiceImpl();
            User user = userService.getUser(Integer.parseInt(uid));
            req.setAttribute("user", user);
            req.getRequestDispatcher(url).forward(req, resp);
        }
    }

    /*删除用户*/
    private void delUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("uid");
        Integer delId = 0;
        try {
            delId = Integer.parseInt(id);
        } catch (Exception e) {
            delId = 0;
        }
        HashMap<String, String> resultMap = new HashMap<>();
        if (delId <= 0) {
            resultMap.put("delResult", "notexist");
        } else {
            UserService userService = new UserServiceImpl();
            if (userService.deleteUserById(delId)) {
                resultMap.put("delResult", "true");
            } else {
                resultMap.put("delResult", "false");
            }
        }
        //把resultMap转换成json对象输出
        resp.setContentType("application/json");
        PrintWriter outPrintWriter = resp.getWriter();
        outPrintWriter.write(JSONArray.toJSONString(resultMap));
        outPrintWriter.flush();
        outPrintWriter.close();
    }

    /*增加用户*/
    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {


        System.out.println("add()==========");
        //获取前端的参数
        String userCode = req.getParameter("userCode");
        String userName = req.getParameter("userName");
        String userPassword = req.getParameter("userPassword");
        String gender = req.getParameter("gender");
        String birthday = req.getParameter("birthday");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String userRole = req.getParameter("userRole");

        User user = new User();
        user.setUserCode(userCode);
        user.setUserName(userName);
        user.setUserPassword(userPassword);
        user.setAddress(address);
        try {
            user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setGender(Integer.valueOf(gender));
        user.setPhone(phone);
        user.setUserRole(Integer.valueOf(userRole));
        user.setCreatedBy(((User) req.getSession().getAttribute(Constants.USER_SESSION)).getId());

        UserServiceImpl userService = new UserServiceImpl();
        if (userService.add(user)) {
            resp.sendRedirect(req.getContextPath() + "/jsp/user.do?method=query");
        } else {
            req.getRequestDispatcher("useradd.jsp").forward(req, resp);
        }

    }

    /* 查询*/
    public void query(HttpServletRequest req, HttpServletResponse resp) {
        //查询用户列表

        //从前端获取数据
        String queryUserName = req.getParameter("queryname");
        String temp = req.getParameter("queryUserRole");
        String pageIndex = req.getParameter("pageIndex");
        int queryUserRole = 0;

        //获取用户列表
        UserServiceImpl userService = new UserServiceImpl();
        List<User> userList = null;

        //第一次走这个请求,一定是第一页,页面大小固定的
        int pageSize = 5;//可以把这个配置到配置文件中,方便后期修改
        int currentPageNo = 1;
        if (queryUserName == null) {
            queryUserName = "";
        }
        if (temp != null && !temp.equals("")) {
            queryUserRole = Integer.parseInt(temp); //给查询赋值 0,1,2,3
        }
        if (pageIndex != null) {
            currentPageNo = Integer.parseInt(pageIndex);
        }

        //获取用户的总数(分页:上一页,下一页的情况)
        int totalCount = userService.getUserCount(queryUserName, queryUserRole);
        //总页数支持
        PageSupport pageSupport = new PageSupport();
        pageSupport.setCurrentPageNo(currentPageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);

        int totalPageCount = ((int) (totalCount / pageSize)) + 1;//总共有几页

        //控制首页和尾页
        //如果页面要小于1了,就显示第一页的东西
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {//当前页面大于了最后一页
            currentPageNo = totalPageCount;
        }

        //获取用户列表展示
        userList = userService.getUserList(queryUserName, queryUserRole, currentPageNo, pageSize);
        req.setAttribute("userList", userList);

        RoleServiceImpl roleService = new RoleServiceImpl();
        List<Role> roleList = roleService.getRoleList();
        req.setAttribute("roleList", roleList);
        req.setAttribute("totalCount", totalCount);
        req.setAttribute("currentPageNo", currentPageNo);
        req.setAttribute("totalPageCount", totalPageCount);
        req.setAttribute("queryUserName", queryUserName);
        req.setAttribute("queryUserRole", queryUserRole);

        //返回前端
        try {
            req.getRequestDispatcher("userlist.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /*修改密码*/
    public void updatePwd(HttpServletRequest req, HttpServletResponse resp) {
        //从Session里面拿ID;
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        String newpassword = req.getParameter("newpassword");

        System.out.println("UserServlet:" + newpassword);

        boolean flag = false;

        System.out.println(o != null);
        System.out.println(StringUtils.isNullOrEmpty(newpassword));

        if (o != null && newpassword != null) {
            UserService userService = new UserServiceImpl();
            flag = userService.updatePwd(((User) o).getId(), newpassword);

            if (flag) {
                req.setAttribute("message", "修改密码成功,请退出,所以新密码登录");
                //密码修改成功,移除当前的Session
                req.getSession().removeAttribute(Constants.USER_SESSION);
            } else {
                req.setAttribute("message", "密码修改失败");
            }
        } else {
            req.setAttribute("message", "新密码有问题");
        }
        try {
            req.getRequestDispatcher("pwdmodify.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*验证旧密码,session中有用户的密码*/
    public void pwdModify(HttpServletRequest req, HttpServletResponse resp) {
        //从Session里面拿ID;
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        String oldpassword = req.getParameter("oldpassword");

        //万能的Map:结果集
        Map<String, String> resultMap = new HashMap<String, String>();

        if (o == null) {//Session失效了,session过期了
            resultMap.put("result", "sessionerror");
        } else if (StringUtils.isNullOrEmpty(oldpassword)) {//输入的密码为空
            resultMap.put("result", "error");
        } else {
            String userPassword = ((User) o).getUserPassword();//Session中用户的密码
            if (oldpassword.equals(userPassword)) {
                resultMap.put("result", "true");
            } else {
                resultMap.put("result", "false");
            }
        }
        try {
            resp.setContentType("application/json");
            PrintWriter writer = resp.getWriter();
            //JSONArray 阿里巴巴的JSON工具类,转换格式
            writer.write(JSONArray.toJSONString(resultMap));
            writer.flush();
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}

BillServlet

package servlet.bill;

import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.util.StringUtils;
import pojo.Bill;
import pojo.Provider;
import service.bill.BillService;
import service.bill.BillServiceImpl;
import service.provider.ProviderServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

public class BillServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        if (method.equals("query") && !StringUtils.isNullOrEmpty(method)) {
            //查
            this.query(req, resp);
        } else if (method.equals("add") && !StringUtils.isNullOrEmpty(method)) {
            //增
            this.add(req, resp);
        } else if (method.equals("view") && !StringUtils.isNullOrEmpty(method)) {
            //查看详情信息
            this.getBill(req, resp, "billview.jsp");
        } else if (method.equals("delbill") && !StringUtils.isNullOrEmpty(method)) {
            //删
            this.delBill(req, resp);
        } else if (method.equals("modify") && !StringUtils.isNullOrEmpty(method)) {
            //得到要修改的对象
            this.getBill02(req, resp, "billmodify.jsp");
        } else if (method.equals("modifysave") && !StringUtils.isNullOrEmpty(method)) {
            //改
            this.updateUser(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private void updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        从前端获取数据
        */
        String billCode = req.getParameter("billCode");
        String productName = req.getParameter("productName");
        String productUnit = req.getParameter("productUnit");
        String productCount = req.getParameter("productCount");
        String totalPrice = req.getParameter("totalPrice");
        String providerId = req.getParameter("providerId");
        String isPayment = req.getParameter("isPayment");
        Bill bill = null;
        try {
            if (!StringUtils.isNullOrEmpty(billCode)) {
                bill = new Bill();

                bill.setBillCode(billCode);
                bill.setProductName(productName);
                bill.setProductUnit(productUnit);
                bill.setProductCount(new BigDecimal(productCount));
                bill.setTotalPrice(new BigDecimal(totalPrice));
                bill.setProviderId(Integer.valueOf(providerId));
                bill.setIsPayment(Integer.valueOf(isPayment));

                BillServiceImpl billService = new BillServiceImpl();
                int i = billService.updateBill(bill);
                if (i == 1) {
                    resp.sendRedirect(req.getContextPath() + "/jsp/bill.do?method=query");
                } else {
                    req.getRequestDispatcher("billmodify.jsp").forward(req, resp);
                }
            } else {
                req.getRequestDispatcher("billmodify.jsp").forward(req, resp);
            }
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void getBill02(HttpServletRequest req, HttpServletResponse resp, String url) throws ServletException, IOException {
        String billCode = req.getParameter("billid");
        BillServiceImpl billService = new BillServiceImpl();
        Bill bill = billService.getBill(billCode);
        req.setAttribute("bill", bill);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    private void delBill(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String billCode = req.getParameter("billid");
        HashMap<String, String> resultMap = new HashMap<>();
        BillService billService = new BillServiceImpl();
        if (billService.deleteBill(billCode)) {
            resultMap.put("delResult", "true");
        } else {
            resultMap.put("delResult", "false");
        }

        //把resultMap转换成json对象输出
        resp.setContentType("application/json");
        PrintWriter outPrintWriter = resp.getWriter();
        outPrintWriter.write(JSONArray.toJSONString(resultMap));
        outPrintWriter.flush();
        outPrintWriter.close();
    }

    private void getBill(HttpServletRequest req, HttpServletResponse resp, String url) throws ServletException, IOException {
        String billCode = req.getParameter("billid");
        BillServiceImpl billService = new BillServiceImpl();
        Bill bill = billService.getBill(billCode);
        req.setAttribute("bill", bill);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        从前端获取数据
        */
        String billCode = req.getParameter("billCode");
        String productName = req.getParameter("productName");
        String productUnit = req.getParameter("productUnit");
        String productCount = req.getParameter("productCount");
        String totalPrice = req.getParameter("totalPrice");
        String providerId = req.getParameter("providerId");
        String isPayment = req.getParameter("isPayment");

        Bill bill = new Bill();
        bill.setBillCode(billCode);
        bill.setProviderName(productName);
        bill.setProductUnit(productUnit);
        BigDecimal productCount01 = new BigDecimal(productCount);
        bill.setProductCount(productCount01);
        BigDecimal totalPrice01 = new BigDecimal(totalPrice);
        bill.setProductCount(totalPrice01);
        bill.setProviderId(Integer.valueOf(providerId));
        bill.setIsPayment(Integer.valueOf(isPayment));

        BillServiceImpl billService = new BillServiceImpl();
        if (billService.add(bill)) {
            resp.sendRedirect(req.getContextPath() + "/jsp/bill.do?method=query");
        } else {
            req.getRequestDispatcher("billadd.jsp").forward(req, resp);
        }

    }

    private void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        获取前端的数据
        */

        //商品名称
        String queryProductName = req.getParameter("queryProductName");
        //供应商
        String providerList_id = req.getParameter("queryProviderId");
        //是否付款
        String queryIsPayment01 = req.getParameter("queryIsPayment");
        //获取订单列表
        BillServiceImpl billService = new BillServiceImpl();
        List<Bill> billList = null;
        //加展示
        billList = billService.getBillList(queryProductName, providerList_id, queryIsPayment01);
        req.setAttribute("billList", billList);
        //商品名称
        req.setAttribute("queryProductName", queryProductName);
        //供应商
        ProviderServiceImpl providerService = new ProviderServiceImpl();
        List<Provider> providerList = providerService.getProviderList();
        req.setAttribute("providerList", providerList);
        //是否付款
        req.setAttribute("queryIsPayment", queryIsPayment01);
        //返回前端
        try {
            req.getRequestDispatcher("billlist.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ProviderServlet

package servlet.provider;
import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.util.StringUtils;
import pojo.Provider;
import service.provider.ProviderServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class ProviderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("query") && !StringUtils.isNullOrEmpty(method)) {
            //查
            this.query(req, resp);
        } else if (method.equals("add") && !StringUtils.isNullOrEmpty(method)) {
            //增
            this.add(req, resp);
        } else if (method.equals("view") && !StringUtils.isNullOrEmpty(method)) {
            //查看详情信息
            this.getprovider(req, resp, "providerview.jsp");
        } else if (method.equals("modify") && !StringUtils.isNullOrEmpty(method)) {
            //得到要修改的对象
            this.getprovider(req, resp, "providermodify.jsp");
        } else if (method.equals("modifysave") && !StringUtils.isNullOrEmpty(method)) {
            //改
            this.updateProvider(req, resp);
        } else if (method.equals("delprovider") &&!StringUtils.isNullOrEmpty(method)) {
            //删
            this.delProvider(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private void delProvider(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String proid = req.getParameter("proid");
        HashMap<String, String> resultMap = new HashMap<>();
        ProviderServiceImpl providerService = new ProviderServiceImpl();

        if (providerService.deleteProvider(proid)) {
            resultMap.put("delResult", "true");
        } else {
            resultMap.put("delResult", "false");
        }
        //把resultMap转换成json对象输出
        resp.setContentType("application/json");
        PrintWriter outPrintWriter = resp.getWriter();
        outPrintWriter.write(JSONArray.toJSONString(resultMap));
        outPrintWriter.flush();
        outPrintWriter.close();
    }

    private void updateProvider(HttpServletRequest req, HttpServletResponse resp) {
        String proid = req.getParameter("proID");
        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proAddress = req.getParameter("proAddress");
        String proFax = req.getParameter("proFax");
        String proDesc = req.getParameter("proDesc");

        Provider provider = null;
        try {
            if (!StringUtils.isNullOrEmpty(proid)) {
                provider = new Provider();
                provider.setId(Integer.valueOf(proid));
                provider.setProCode(proCode);
                provider.setProName(proName);
                provider.setProContact(proContact);
                provider.setProPhone(proPhone);
                provider.setProAddress(proAddress);
                provider.setProFax(proFax);
                provider.setProDesc(proDesc);
                ProviderServiceImpl providerService = new ProviderServiceImpl();
                int i = providerService.updateProvider(provider);
                if (i == 1) {
                    resp.sendRedirect(req.getContextPath() + "/jsp/provider.do?method=query");
                } else {
                    req.getRequestDispatcher("providermodify.jsp").forward(req, resp);
                }
            } else {
                req.getRequestDispatcher("providermodify.jsp").forward(req, resp);
            }
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void getprovider(HttpServletRequest req, HttpServletResponse resp, String url) throws ServletException, IOException {
        String proid = req.getParameter("proid");
        ProviderServiceImpl providerService = new ProviderServiceImpl();
        Provider provider = providerService.getProvider(proid);
        req.setAttribute("provider", provider);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        从前端获取数据
        */
        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proAddress = req.getParameter("proAddress");
        String proFax = req.getParameter("proFax");
        String proDesc = req.getParameter("proDesc");

        Provider provider = new Provider();
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProDesc(proDesc);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProAddress(proAddress);
        provider.setProFax(proFax);
        provider.setCreationDate(new Date());

        ProviderServiceImpl providerService = new ProviderServiceImpl();
        if (providerService.add(provider)) {
            resp.sendRedirect(req.getContextPath() + "/jsp/provider.do?method=query");
        } else {
            req.getRequestDispatcher("provideradd.jsp").forward(req, resp);
        }

    }

    private void query(HttpServletRequest req, HttpServletResponse resp) {
         /*
        获取前端的数据
        */
        //供应商编码
        String queryProCode = req.getParameter("queryProCode");
        //供应商名称
        String queryProName = req.getParameter("queryProName");
        //供应商信息展示
        ProviderServiceImpl providerService = new ProviderServiceImpl();
        List<Provider> providerList = null;
        providerList = providerService.getProviderList_Pro(queryProCode, queryProName);
        req.setAttribute("providerList", providerList);
        req.setAttribute("queryProCode", queryProCode);
        req.setAttribute("queryProName", queryProName);
        //返回前端
        try {
            req.getRequestDispatcher("providerlist.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}


项目运行结果

登录界面

主页界面

订单管理界面

供应商管理界面

用户管理界面

密码修改界面


文章来源:https://www.cnblogs.com/jxzy/p/16506245.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云