2025春开学考——仓库管理系统(代码)

一、数据库设计:

User类

点击查看代码
drop table if exists tb_user;
create table tb_user(
    userID varchar(20),
    password varchar(20),
    position int
);
warehouse类
点击查看代码
CREATE TABLE warehouse (
    warehouseCode VARCHAR(50) UNIQUE NOT NULL ,  -- 仓库编码,唯一且不能为空
    warehouseName VARCHAR(100) NOT NULL,        -- 仓库名称,不能为空
    location VARCHAR(200),                      -- 仓库地址
    capacity DECIMAL(10, 2),                    -- 仓库容量,可存储数值,例如存储货物的重量或体积
    contactPerson VARCHAR(50),                 -- 仓库联系人
    contactPhone VARCHAR(20)                   -- 联系人电话
);
material_category类
点击查看代码
CREATE TABLE material_category (
    materialCode VARCHAR(50) NOT NULL UNIQUE PRIMARY KEY,
    materialName VARCHAR(100) NOT NULL,
    specification VARCHAR(100),
    material VARCHAR(100),
    -- 可根据需求添加其他字段,如备注等
    remarks TEXT
);
material_ledger_detail类
点击查看代码
CREATE TABLE material_ledger_detail (
    -- 台账编号,作为主键,唯一标识每条记录
    ledgerId VARCHAR(12) PRIMARY KEY NOT NULL,
    -- 物资编码,用于关联物资类别表,不允许为空
    materialCode VARCHAR(50) NOT NULL,
    -- 操作类别,使用 ENUM 类型限制为 '入库' 或 '出库',不允许为空
    operationType ENUM('入库', '出库') NOT NULL,
    -- 物资数量,使用 DECIMAL 类型存储,精度为 10,小数位为 2,不允许为空
    quantity DECIMAL(10, 2) NOT NULL,
    -- 计量单位,如 '个'、'箱' 等,不允许为空
    unit VARCHAR(20) NOT NULL,
    -- 存放地点(仓库号),不允许为空
    storageLocation VARCHAR(20) NOT NULL,
    -- 操作日期,记录操作发生的时间,默认值为当前时间
    operationDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

二、分别对应的pojo设计
User类

点击查看代码
package com.QixunQiu.pojo;

public class User {
    private String userID;
    private String password;
    private Integer position;

    // Getters and Setters
    public String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getPosition() {
        return position;
    }

    public void setPosition(Integer position) {
        this.position = position;
    }

    // toString method
    @Override
    public String toString() {
        return "TbUser{" +
                "userID='" + userID + '\'' +
                ", password='" + password + '\'' +
                ", position=" + position +
                '}';
    }
}
Warehouse类
点击查看代码
package com.QixunQiu.pojo;

import java.math.BigDecimal;

public class Warehouse {
    private String warehouseCode;  // 仓库编码
    private String warehouseName; // 仓库名称
    private String location;      // 仓库地址
    private BigDecimal capacity;  // 仓库容量
    private String contactPerson; // 仓库联系人
    private String contactPhone;  // 联系人电话

    // Getters and Setters
    public String getWarehouseCode() {
        return warehouseCode;
    }

    public void setWarehouseCode(String warehouseCode) {
        this.warehouseCode = warehouseCode;
    }

    public String getWarehouseName() {
        return warehouseName;
    }

    public void setWarehouseName(String warehouseName) {
        this.warehouseName = warehouseName;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public BigDecimal getCapacity() {
        return capacity;
    }

    public void setCapacity(BigDecimal capacity) {
        this.capacity = capacity;
    }

    public String getContactPerson() {
        return contactPerson;
    }

    public void setContactPerson(String contactPerson) {
        this.contactPerson = contactPerson;
    }

    public String getContactPhone() {
        return contactPhone;
    }

    public void setContactPhone(String contactPhone) {
        this.contactPhone = contactPhone;
    }

    // toString method
    @Override
    public String toString() {
        return "Warehouse{" +
                "warehouseCode='" + warehouseCode + '\'' +
                ", warehouseName='" + warehouseName + '\'' +
                ", location='" + location + '\'' +
                ", capacity=" + capacity +
                ", contactPerson='" + contactPerson + '\'' +
                ", contactPhone='" + contactPhone + '\'' +
                '}';
    }
}
MaterialCategory类
点击查看代码
package com.QixunQiu.pojo;

public class MaterialCategory {
    private String materialCode;  // 物料编码,主键
    private String materialName; // 物料名称
    private String specification; // 规格
    private String material; // 物料类型
    private String remarks; // 备注

    // Getters and Setters
    public String getMaterialCode() {
        return materialCode;
    }

    public void setMaterialCode(String materialCode) {
        this.materialCode = materialCode;
    }

    public String getMaterialName() {
        return materialName;
    }

    public void setMaterialName(String materialName) {
        this.materialName = materialName;
    }

    public String getSpecification() {
        return specification;
    }

    public void setSpecification(String specification) {
        this.specification = specification;
    }

    public String getMaterial() {
        return material;
    }

    public void setMaterial(String material) {
        this.material = material;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    // toString method
    @Override
    public String toString() {
        return "MaterialCategory{" +
                "materialCode='" + materialCode + '\'' +
                ", materialName='" + materialName + '\'' +
                ", specification='" + specification + '\'' +
                ", material='" + material + '\'' +
                ", remarks='" + remarks + '\'' +
                '}';
    }
}
MaterialLedgerDetail类
点击查看代码
package com.QixunQiu.pojo;

import java.math.BigDecimal;
import java.sql.Timestamp;

public class MaterialLedgerDetail {
    private String ledgerId; // 台账编号,主键
    private String materialCode; // 物资编码
    private String operationType; // 操作类别:'入库' 或 '出库'
    private BigDecimal quantity; // 物资数量
    private String unit; // 计量单位
    private String storageLocation; // 存放地点(仓库号)
    private Timestamp operationDate; // 操作日期,默认为当前时间

    // Getters and Setters
    public String getLedgerId() {
        return ledgerId;
    }

    public void setLedgerId(String ledgerId) {
        this.ledgerId = ledgerId;
    }

    public String getMaterialCode() {
        return materialCode;
    }

    public void setMaterialCode(String materialCode) {
        this.materialCode = materialCode;
    }

    public String getOperationType() {
        return operationType;
    }

    public void setOperationType(String operationType) {
        this.operationType = operationType;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getStorageLocation() {
        return storageLocation;
    }

    public void setStorageLocation(String storageLocation) {
        this.storageLocation = storageLocation;
    }

    public Timestamp getOperationDate() {
        return operationDate;
    }

    public void setOperationDate(Timestamp operationDate) {
        this.operationDate = operationDate;
    }

    // toString method
    @Override
    public String toString() {
        return "MaterialLedgerDetail{" +
                "ledgerId='" + ledgerId + '\'' +
                ", materialCode='" + materialCode + '\'' +
                ", operationType='" + operationType + '\'' +
                ", quantity=" + quantity +
                ", unit='" + unit + '\'' +
                ", storageLocation='" + storageLocation + '\'' +
                ", operationDate=" + operationDate +
                '}';
    }
}
三、所有的Mapper接口与对应的映射.xml User类接口
点击查看代码
package com.QixunQiu.mapper;

import com.QixunQiu.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {

    @Select("select * from tb_user where UserID = #{UserID} and Password = #{Password}")
    User select(@Param("UserID") String UserID, @Param("Password")  String Password);

    int updatePassword(User user);

    void addUser(User user);

    List<User> selectALL();

    @Select("select * from tb_user where UserID = #{UserID} ")
    User selectByUserID(@Param("UserID") String UserID);

    void deleteByUserID(String UserID);
}

User.xml
点击查看代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.QixunQiu.mapper.UserMapper">
    <insert id="addUser">
        insert into tb_user (UserID, Password)
        values (#{UserID}, #{Password});
    </insert>


    <update id="updatePassword" >
        update tb_user
        <set>
            <if test="Password != null and Password != ''">
                Password = #{Password},
            </if>
        </set>
        where UserID = #{UserID};
    </update>

    <delete id="deleteByUserID">
        delete from tb_user where UserID = #{UserID}
    </delete>

    <select id="selectALL" resultType="com.QixunQiu.pojo.User">
        select *
        from tb_user ;
    </select>
</mapper>
Warehouse类接口
点击查看代码
package com.QixunQiu.mapper;

import com.QixunQiu.pojo.Warehouse;

import java.util.List;

public interface WarehouseMapper {
    void addWarehouse(Warehouse warehouse);

    List<Warehouse> selectAllWarehouse();

    Warehouse selectWarehouseById(String id);

    int updateWarehouse(Warehouse warehouse);

    void deleteWarehouse(String id);
}

WarehouseMapper.xml
点击查看代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.QixunQiu.mapper.WarehouseMapper">

    <insert id="addWarehouse" parameterType="com.QixunQiu.pojo.Warehouse">
        INSERT INTO warehouse (
            warehouseCode,
            warehouseName,
            location,
            capacity,
            contactPerson,
            contactPhone
        ) VALUES (
                     #{warehouseCode},
                     #{warehouseName},
                     #{location},
                     #{capacity},
                     #{contactPerson},
                     #{contactPhone}
                 )
    </insert>
    <update id="updateWarehouse" parameterType="com.QixunQiu.pojo.Warehouse">
        UPDATE warehouse
        SET
            warehouseName = #{warehouseName},
            location = #{location},
            capacity = #{capacity},
            contactPerson = #{contactPerson},
            contactPhone = #{contactPhone}
        WHERE warehouseCode = #{warehouseCode}
    </update>
    <delete id="deleteWarehouse">
        delete from warehouse where warehouseCode=#{warehouseCode};
    </delete>

    <select id="selectAllWarehouse" resultType="com.QixunQiu.pojo.Warehouse">
        select *
        from warehouse ;
    </select>
    <select id="selectWarehouseById" resultType="com.QixunQiu.pojo.Warehouse">
        select *
        from warehouse where warehouseCode=#{warehouseCode};
    </select>

</mapper>
MaterialCategory接口
点击查看代码
package com.QixunQiu.mapper;

import com.QixunQiu.pojo.MaterialCategory;

import java.util.List;

public interface MaterialCategoryMapper {
    void addMaterialCategory(MaterialCategory materialCategory);

    List<MaterialCategory> selectAllMaterialCategory();

    MaterialCategory selectMaterialCategoryById(String id);

    int updateMaterialCategory(MaterialCategory materialCategory);

    void deleteMaterialCategory(String id);
}

MaterialCategoryMapper.xml
点击查看代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.QixunQiu.mapper.MaterialCategoryMapper">

    <insert id="addMaterialCategory" parameterType="com.QixunQiu.pojo.MaterialCategory">
        INSERT INTO material_category (
            materialCode,
            materialName,
            specification,
            material,
            remarks
        ) VALUES (
                     #{materialCode},
                     #{materialName},
                     #{specification},
                     #{material},
                     #{remarks}
                 )
    </insert>
    <update id="updateMaterialCategory" parameterType="com.QixunQiu.pojo.MaterialCategory">
        UPDATE material_category
        SET
            materialName = #{materialName},
            specification = #{specification},
            material = #{material},
            remarks = #{remarks}
        WHERE materialCode = #{materialCode}
    </update>
    <delete id="deleteMaterialCategory">
        delete from material_category where materialCode= #{materialCode};
    </delete>
    <select id="selectAllMaterialCategory" resultType="com.QixunQiu.pojo.MaterialCategory">
        select *
        from material_category;
    </select>
    <select id="selectMaterialCategoryById" resultType="com.QixunQiu.pojo.MaterialCategory">
        select *
        from material_category where materialCode=#{materialCode};
    </select>

</mapper>
MaterialLedgerDetail接口
点击查看代码
package com.QixunQiu.mapper;

import com.QixunQiu.pojo.MaterialLedgerDetail;

import java.util.List;

public interface MaterialLedgerDetailMapper {
    void addMaterialLedgerDetail(MaterialLedgerDetail materialLedgerDetail);

    List<MaterialLedgerDetail> getMaterialLedgerDetailById(String id);
}

MaterialLedgerDetailMapper.xml
点击查看代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.QixunQiu.mapper.MaterialLedgerDetailMapper">


    <insert id="addMaterialLedgerDetail" parameterType="com.QixunQiu.pojo.MaterialLedgerDetail">
        INSERT INTO material_ledger_detail (
            ledgerId,
            materialCode,
            operationType,
            quantity,
            unit,
            storageLocation,
            operationDate
        ) VALUES (
                     #{ledgerId},
                     #{materialCode},
                     #{operationType},
                     #{quantity},
                     #{unit},
                     #{storageLocation},
                     #{operationDate, jdbcType=TIMESTAMP}
                 )
    </insert>
    <select id="getMaterialLedgerDetailById" resultType="com.QixunQiu.pojo.MaterialLedgerDetail">
        select * from material_ledger_detail where materialCode=#{materialCode};
    </select>
</mapper>
四、Service UserService
点击查看代码
package com.QixunQiu.service;

import com.QixunQiu.mapper.UserMapper;
import com.QixunQiu.pojo.User;
import com.QixunQiu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public User selectUser(String UserID, String Password) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.select(UserID, Password);
        sqlSession.close();
        return user;
    }

    public User selectUserByID(String UserID) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectByUserID(UserID);
        sqlSession.close();
        return user;
    }

    public List<User> selectAllUser() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users=userMapper.selectALL();
        sqlSession.close();
        return users;
    }

    public void deleteUser(String UserID) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(UserID);
        userMapper.deleteByUserID(UserID);
        sqlSession.commit();
        sqlSession.close();
    }

    public void  updatePassword(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        System.out.println("465652");
        userMapper.updatePassword(user);
        sqlSession.commit();
        sqlSession.close();
    }

    public void addUser(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.addUser(user);
        sqlSession.commit();
        sqlSession.close();
    }
}

WarehouseService
点击查看代码
package com.QixunQiu.service;

import com.QixunQiu.mapper.WarehouseMapper;
import com.QixunQiu.pojo.Warehouse;
import com.QixunQiu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class WarehouseService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public void add(Warehouse warehouse) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        WarehouseMapper warehouseMapper = sqlSession.getMapper(WarehouseMapper.class);
        warehouseMapper.addWarehouse(warehouse);
        sqlSession.commit();
        sqlSession.close();
    }
    public List<Warehouse> getAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        WarehouseMapper warehouseMapper = sqlSession.getMapper(WarehouseMapper.class);
        List<Warehouse> warehouseList=warehouseMapper.selectAllWarehouse();
        sqlSession.close();
        return warehouseList;
    }
    public Warehouse getWarehouseById(String id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        WarehouseMapper warehouseMapper = sqlSession.getMapper(WarehouseMapper.class);
        Warehouse warehouse=warehouseMapper.selectWarehouseById(id);
        sqlSession.close();
        return warehouse;
    }
    public void update(Warehouse warehouse) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        WarehouseMapper warehouseMapper = sqlSession.getMapper(WarehouseMapper.class);
        warehouseMapper.updateWarehouse(warehouse);
        sqlSession.commit();
        sqlSession.close();
    }
    public void delete(String id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        WarehouseMapper warehouseMapper = sqlSession.getMapper(WarehouseMapper.class);
        System.out.println(id);
        warehouseMapper.deleteWarehouse(id);
        sqlSession.commit();
        sqlSession.close();
    }
}

MaterialCategoryService
点击查看代码
package com.QixunQiu.service;

import com.QixunQiu.mapper.MaterialCategoryMapper;
import com.QixunQiu.pojo.MaterialCategory;
import com.QixunQiu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class MaterialCategoryService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public void add(MaterialCategory materialCategory) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MaterialCategoryMapper materialCategoryMapper = sqlSession.getMapper(MaterialCategoryMapper.class);
        materialCategoryMapper.addMaterialCategory(materialCategory);
        sqlSession.commit();
        sqlSession.close();
    }
    public List<MaterialCategory> findAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MaterialCategoryMapper materialCategoryMapper = sqlSession.getMapper(MaterialCategoryMapper.class);
        List<MaterialCategory> materialCategoryList=materialCategoryMapper.selectAllMaterialCategory();
        sqlSession.close();
        return materialCategoryList;
    }
    public MaterialCategory findById(String id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MaterialCategoryMapper materialCategoryMapper = sqlSession.getMapper(MaterialCategoryMapper.class);
        MaterialCategory materialCategory = materialCategoryMapper.selectMaterialCategoryById(id);
        sqlSession.close();
        return materialCategory;
    }
    public void update(MaterialCategory materialCategory) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MaterialCategoryMapper materialCategoryMapper = sqlSession.getMapper(MaterialCategoryMapper.class);
        materialCategoryMapper.updateMaterialCategory(materialCategory);
        sqlSession.commit();
        sqlSession.close();
    }
    public void delete(String id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MaterialCategoryMapper materialCategoryMapper = sqlSession.getMapper(MaterialCategoryMapper.class);
        materialCategoryMapper.deleteMaterialCategory(id);
        sqlSession.commit();
        sqlSession.close();
    }
}

MaterialLedgerDetailService
点击查看代码
package com.QixunQiu.service;

import com.QixunQiu.mapper.MaterialLedgerDetailMapper;
import com.QixunQiu.pojo.MaterialLedgerDetail;
import com.QixunQiu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class MaterialLedgerDetailService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public void add(MaterialLedgerDetail materialLedgerDetail) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MaterialLedgerDetailMapper mapper = sqlSession.getMapper(MaterialLedgerDetailMapper.class);
        mapper.addMaterialLedgerDetail(materialLedgerDetail);
        sqlSession.commit();
        sqlSession.close();
    }
    public List<MaterialLedgerDetail> selectByPrimaryKey(String id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MaterialLedgerDetailMapper mapper = sqlSession.getMapper(MaterialLedgerDetailMapper.class);
        List<MaterialLedgerDetail> materialLedgerDetail=mapper.getMaterialLedgerDetailById(id);
        sqlSession.close();
        return materialLedgerDetail;
    }
}

五、Servlet addMaterialCategoryServlet
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.MaterialCategory;
import com.QixunQiu.service.MaterialCategoryService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/addMaterialCategoryServlet")
public class addMaterialCategoryServlet extends HttpServlet {
    MaterialCategoryService mcs = new MaterialCategoryService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        // 从表单中获取数据
        String materialCode = request.getParameter("materialCode");
        String materialName = request.getParameter("materialName");
        String specification = request.getParameter("specification");
        String material = request.getParameter("material");
        String remarks = request.getParameter("remarks");

        // 创建 MaterialCategory 对象并填充数据
        MaterialCategory category = new MaterialCategory();
        category.setMaterialCode(materialCode);
        category.setMaterialName(materialName);
        category.setSpecification(specification);
        category.setMaterial(material);
        category.setRemarks(remarks);

        mcs.add(category);
        HttpSession session = request.getSession();
        List<MaterialCategory> materialCategoryList=mcs.findAll();
        session.setAttribute("materialCategoryList", materialCategoryList);
        request.getRequestDispatcher("/materialcategory.jsp").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

addMaterialLedgerDetailServlet
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.MaterialLedgerDetail;
import com.QixunQiu.service.MaterialCategoryService;
import com.QixunQiu.service.MaterialLedgerDetailService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.*;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/addMaterialLedgerDetailServlet")
public class addMaterialLedgerDetailServlet extends HttpServlet {
    private MaterialLedgerDetailService mlds = new MaterialLedgerDetailService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        // 获取表单数据
        String ledgerId = generateID();
        String materialCode = request.getParameter("materialCode");
        String operationType = request.getParameter("operationType");
        BigDecimal quantity = new BigDecimal(request.getParameter("quantity"));
        String unit = request.getParameter("unit");
        String storageLocation = request.getParameter("storageLocation");
        String operationDateStr = request.getParameter("operationDate");

        // 将字符串日期转换为 Timestamp(如果用户未选择日期,则使用当前时间)
        Timestamp operationDate = null;
        if (operationDateStr != null && !operationDateStr.isEmpty()) {
            operationDate = Timestamp.valueOf(operationDateStr);
        } else {
            operationDate = new Timestamp(System.currentTimeMillis());
        }

        // 创建 MaterialLedgerDetail 对象
        MaterialLedgerDetail detail = new MaterialLedgerDetail();
        detail.setLedgerId(ledgerId);
        detail.setMaterialCode(materialCode);
        detail.setOperationType(operationType);
        detail.setQuantity(quantity);
        detail.setUnit(unit);
        detail.setStorageLocation(storageLocation);
        detail.setOperationDate(operationDate);

        mlds.add(detail);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
    private int sequenceNumber = 1;

    public synchronized String generateID() {
        // 获取当前日期
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(now);

        // 读取或初始化sequenceNumber
        if (sequenceNumber == 1) {
            File file = new File("MaterialLedgerDetail.txt");
            if (file.exists()) {
                try {
                    BufferedReader reader = new BufferedReader(new FileReader(file));
                    String line = reader.readLine();
                    sequenceNumber = Integer.parseInt(line);
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 组合房产编号
        String sequenceStr = String.format("%04d", sequenceNumber++);
        String houseID = dateStr + sequenceStr;

        // 更新sequenceNumber到文件
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter("MaterialLedgerDetail.txt"));
            writer.write(String.valueOf(sequenceNumber));
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return houseID;
    }
}

addWarehouseServlet
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.Warehouse;
import com.QixunQiu.service.WarehouseService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/addWarehouseServlet")
public class addWarehouseServlet extends HttpServlet {
    private WarehouseService warehouseService = new WarehouseService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求和响应的编码为 UTF-8
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 从表单中获取数据
        String warehouseCode = request.getParameter("warehouseCode");
        String warehouseName = request.getParameter("warehouseName");
        String location = request.getParameter("location");
        String capacityStr = request.getParameter("capacity");
        String contactPerson = request.getParameter("contactPerson");
        String contactPhone = request.getParameter("contactPhone");

        // 创建 Warehouse 对象并填充数据
        Warehouse warehouse = new Warehouse();
        warehouse.setWarehouseCode(warehouseCode);
        warehouse.setWarehouseName(warehouseName);
        warehouse.setLocation(location);
        warehouse.setCapacity(capacityStr != null && !capacityStr.isEmpty() ? new BigDecimal(capacityStr) : null);
        warehouse.setContactPerson(contactPerson);
        warehouse.setContactPhone(contactPhone);

        // 调用服务层方法添加仓库
        warehouseService.add(warehouse);
        HttpSession session = request.getSession();
        List<Warehouse> warehouseList=warehouseService.getAll();
        session.setAttribute("warehouseList", warehouseList);
        request.getRequestDispatcher("/warehouse.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 将 POST 请求转发到 doGet 方法处理
        this.doGet(request, response);
    }
}
DeleteMaterialCategoryServlet
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.MaterialCategory;
import com.QixunQiu.pojo.MaterialLedgerDetail;
import com.QixunQiu.service.MaterialCategoryService;
import com.QixunQiu.service.MaterialLedgerDetailService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/DeleteMaterialCategoryServlet")
public class DeleteMaterialCategoryServlet extends HttpServlet {
    private MaterialCategoryService mcs = new MaterialCategoryService();
    private MaterialLedgerDetailService mlds = new MaterialLedgerDetailService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String id = request.getParameter("id");

        HttpSession session = request.getSession();
        List<MaterialCategory> materialCategoryList=mcs.findAll();
        // 查询物料台账明细信息
        List<MaterialLedgerDetail> materialLedgerDetail = mlds.selectByPrimaryKey(id);

        if (!materialLedgerDetail.isEmpty()) {
            System.out.println("dwdwdwd");
            // 如果物料台账明细存在,说明该物料编码已被使用,不能编辑
            response.getWriter().write("<script>alert('该物料已存在于台账明细中,无法编辑!');window.location.href='" + request.getContextPath() + "/selectAllMaterialCategory';</script>");
        } else {
            // 如果物料台账明细不存在,可以编辑
            mcs.delete(id);
            session.setAttribute("materialCategoryList", materialCategoryList);
            request.getRequestDispatcher("/materialcategory.jsp").forward(request, response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

DeleteWarehouseServlet
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.Warehouse;
import com.QixunQiu.service.WarehouseService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/DeleteWarehouseServlet")
public class DeleteWarehouseServlet extends HttpServlet {
    private WarehouseService warehouseService = new WarehouseService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String id=request.getParameter("id");
        //System.out.println(id);
        warehouseService.delete(id);
        HttpSession session = request.getSession();
        List<Warehouse> warehouseList=warehouseService.getAll();
        session.setAttribute("warehouseList", warehouseList);
        request.getRequestDispatcher("/warehouse.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

LoginServlet
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.User;
import com.QixunQiu.service.UserService;

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.io.PrintWriter;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String UserID = request.getParameter("UserID");
        String Password = request.getParameter("Password");
        User user =userService.selectUser(UserID, Password);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        //3. 判断user释放为null
        if(user != null){
            // 登陆成功
            writer.write("登陆成功");
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            request.getRequestDispatcher("/index.jsp").forward(request,response);
        }else {
            // 登陆失败
            writer.write("登陆失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

selectAllMaterialCategory
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.MaterialCategory;
import com.QixunQiu.service.MaterialCategoryService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectAllMaterialCategory")
public class selectAllMaterialCategory extends HttpServlet {
    MaterialCategoryService mcs = new MaterialCategoryService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        List<MaterialCategory> materialCategoryList=mcs.findAll();
        session.setAttribute("materialCategoryList", materialCategoryList);
        request.getRequestDispatcher("/materialcategory.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

selectAllWarehouse
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.Warehouse;
import com.QixunQiu.service.WarehouseService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectAllWarehouse")
public class selectAllWarehouse extends HttpServlet {
    private WarehouseService warehouseService = new WarehouseService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        List<Warehouse> warehouseList=warehouseService.getAll();
        session.setAttribute("warehouseList", warehouseList);
        request.getRequestDispatcher("/warehouse.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

ToEditMaterialCategoryServlet
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.MaterialCategory;
import com.QixunQiu.pojo.MaterialLedgerDetail;
import com.QixunQiu.service.MaterialCategoryService;
import com.QixunQiu.service.MaterialLedgerDetailService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/ToEditMaterialCategoryServlet")
public class ToEditMaterialCategoryServlet extends HttpServlet {
    private MaterialCategoryService mcs = new MaterialCategoryService();
    private MaterialLedgerDetailService mlds = new MaterialLedgerDetailService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        String ID = request.getParameter("ID");

        // 查询物料分类信息
        MaterialCategory materialCategory = mcs.findById(ID);

        // 查询物料台账明细信息
        List<MaterialLedgerDetail> materialLedgerDetail = mlds.selectByPrimaryKey(ID);

        if (!materialLedgerDetail.isEmpty()) {
            System.out.println("dwdwdwd");
            // 如果物料台账明细存在,说明该物料编码已被使用,不能编辑
            response.getWriter().write("<script>alert('该物料已存在于台账明细中,无法编辑!');window.location.href='" + request.getContextPath() + "/selectAllMaterialCategory';</script>");
        } else {
            // 如果物料台账明细不存在,可以编辑
            session.setAttribute("materialCategory", materialCategory);
            request.getRequestDispatcher("/updateMaterialCategory.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
ToFixWarehouseServlet
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.Warehouse;
import com.QixunQiu.service.WarehouseService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/ToFixWarehouseServlet")
public class ToFixWarehouseServlet extends HttpServlet {
    private WarehouseService warehouseService = new WarehouseService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        String id = request.getParameter("ID");
        System.out.println(id);
        Warehouse warehouse = warehouseService.getWarehouseById(id);
        session.setAttribute("warehouse", warehouse);
        request.getRequestDispatcher("/updateWarehouse.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

updateMaterialCategoryServlet
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.MaterialCategory;
import com.QixunQiu.service.MaterialCategoryService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/updateMaterialCategoryServlet")
public class updateMaterialCategoryServlet extends HttpServlet {
    MaterialCategoryService mcs = new MaterialCategoryService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        // 从表单中获取数据
        String materialCode = request.getParameter("materialCode");
        String materialName = request.getParameter("materialName");
        String specification = request.getParameter("specification");
        String material = request.getParameter("material");
        String remarks = request.getParameter("remarks");

        // 创建 MaterialCategory 对象并填充数据
        MaterialCategory category = new MaterialCategory();
        category.setMaterialCode(materialCode);
        category.setMaterialName(materialName);
        category.setSpecification(specification);
        category.setMaterial(material);
        category.setRemarks(remarks);

        mcs.update(category);
        HttpSession session = request.getSession();
        List<MaterialCategory> materialCategoryList=mcs.findAll();
        session.setAttribute("materialCategoryList", materialCategoryList);
        request.getRequestDispatcher("/materialcategory.jsp").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

updateWarehouseServlet
点击查看代码
package com.QixunQiu.web;

import com.QixunQiu.pojo.Warehouse;
import com.QixunQiu.service.WarehouseService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/updateWarehouseServlet")
public class updateWarehouseServlet extends HttpServlet {
    private WarehouseService warehouseService = new WarehouseService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 从表单中获取数据
        String warehouseCode = request.getParameter("warehouseCode");
        String warehouseName = request.getParameter("warehouseName");
        String location = request.getParameter("location");
        String capacityStr = request.getParameter("capacity");
        String contactPerson = request.getParameter("contactPerson");
        String contactPhone = request.getParameter("contactPhone");

        // 创建 Warehouse 对象并填充数据
        Warehouse warehouse = new Warehouse();
        warehouse.setWarehouseCode(warehouseCode);
        warehouse.setWarehouseName(warehouseName);
        warehouse.setLocation(location);
        warehouse.setCapacity(capacityStr != null && !capacityStr.isEmpty() ? new BigDecimal(capacityStr) : null);
        warehouse.setContactPerson(contactPerson);
        warehouse.setContactPhone(contactPhone);
        //System.out.println(warehouse);
        warehouseService.update(warehouse);
        HttpSession session = request.getSession();
        List<Warehouse> warehouseList=warehouseService.getAll();
        session.setAttribute("warehouseList", warehouseList);
        request.getRequestDispatcher("/warehouse.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

六、前端设计 addMaterialCategory.jsp
点击查看代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加物料分类信息</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 600px;
            margin: auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        h2 {
            text-align: center;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            margin-top: 10px;
        }
        input[type="text"],
        textarea {
            padding: 8px;
            margin-top: 5px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        input[type="submit"] {
            margin-top: 15px;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>添加物料分类信息</h2>
    <form action="${pageContext.request.contextPath}/addMaterialCategoryServlet" method="post">
        <label for="materialCode">物料编码(唯一):</label>
        <input type="text" id="materialCode" name="materialCode" required>

        <label for="materialName">物料名称:</label>
        <input type="text" id="materialName" name="materialName" required>

        <label for="specification">规格:</label>
        <input type="text" id="specification" name="specification">

        <label for="material">物料类型:</label>
        <input type="text" id="material" name="material">

        <label for="remarks">备注:</label>
        <textarea id="remarks" name="remarks" rows="4"></textarea>

        <input type="submit" value="添加物料分类">
    </form>
</div>
</body>
</html>
addMaterialLedgerDetail.jsp
点击查看代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加物料台账明细</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 600px;
            margin: auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        h2 {
            text-align: center;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            margin-top: 10px;
        }
        input[type="text"],
        input[type="number"],
        select {
            padding: 8px;
            margin-top: 5px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        input[type="submit"] {
            margin-top: 15px;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>添加物料台账明细</h2>
    <form action="${pageContext.request.contextPath}/addMaterialLedgerDetailServlet" method="post">

        <label for="materialCode">物资编码:</label>
        <input type="text" id="materialCode" name="materialCode" required>

        <label for="operationType">操作类别:</label>
        <select id="operationType" name="operationType" required>
            <option value="入库">入库</option>
            <option value="出库">出库</option>
        </select>

        <label for="quantity">物资数量:</label>
        <input type="number" id="quantity" name="quantity" step="0.01" required>

        <label for="unit">计量单位:</label>
        <input type="text" id="unit" name="unit" required>

        <label for="storageLocation">存放地点(仓库号):</label>
        <input type="text" id="storageLocation" name="storageLocation" required>

        <input type="submit" value="添加物料台账明细">
    </form>
</div>
</body>
</html>
addWarehouse.jsp
点击查看代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加仓库信息</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 600px;
            margin: auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        h2 {
            text-align: center;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            margin-top: 10px;
        }
        input[type="text"],
        input[type="tel"],
        input[type="number"] {
            padding: 8px;
            margin-top: 5px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        input[type="submit"] {
            margin-top: 15px;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>添加仓库信息</h2>
    <form action="${pageContext.request.contextPath}/addWarehouseServlet" method="post">
        <label for="warehouseCode">仓库编码(唯一):</label>
        <input type="text" id="warehouseCode" name="warehouseCode" required>

        <label for="warehouseName">仓库名称:</label>
        <input type="text" id="warehouseName" name="warehouseName" required>

        <label for="location">仓库地址:</label>
        <input type="text" id="location" name="location">

        <label for="capacity">仓库容量(小数,例如:1000.50):</label>
        <input type="number" id="capacity" name="capacity" step="0.01">

        <label for="contactPerson">联系人:</label>
        <input type="text" id="contactPerson" name="contactPerson">

        <label for="contactPhone">联系电话:</label>
        <input type="tel" id="contactPhone" name="contactPhone">

        <input type="submit" value="添加仓库">
    </form>
</div>
</body>
</html>
index.jsp
点击查看代码
<%@ page import="com.QixunQiu.pojo.User" %>
<%@ page import="java.util.Objects" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<%
    User user = (User) session.getAttribute("user");
    // 使用user对象
%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>仓库管理系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f9;
        }
        header {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            text-align: center;
        }
        .container {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
        }
        .button-group {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<header>
    <h1>仓库管理系统</h1>
</header>
<div class="container">
    <% if (user.getPosition() == 1 || user.getPosition() == 0) { %>
    <div class="button-group">
        <button onclick="addHouse()">添加仓库</button>
        <button onclick="checkHouse()">管理仓库</button>
        <button onclick="addMaterialCategory()">添加物资类</button>
        <button onclick="checkMaterial()">管理物资类</button>
    </div>
    <% } %>
    <% if (user.getPosition() == 2 || user.getPosition() == 0) { %>
    <div class="button-group">
        <button onclick="addMaterialLedgerDetail()">出入库操作</button>
        <button onclick="window.location.href='统计查询.jsp'">统计查询</button>
    </div>
    <% } %>
</div>
<script>
    function addHouse() {
        window.location.href = "addWarehouse.jsp";
    }
    function checkHouse() {
        window.location.href = "/Cang/selectAllWarehouse";
    }
    function addMaterialCategory() {
        window.location.href = "addMaterialCategory.jsp";
    }
    function checkMaterial() {
        window.location.href = "/Cang/selectAllMaterialCategory";
    }
    function addMaterialLedgerDetail() {
        window.location.href = "addMaterialLedgerDetail.jsp";
    }
</script>
</body>
</html>
login.html
点击查看代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        #loginDiv {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        #loginMsg {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #5cb85c;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #4cae4c;
        }

        #subDiv {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        a {
            color: #337ab7;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body>
<div id="loginDiv">
    <form action="/Cang/LoginServlet" method="post" id="form">
        <h1 id="loginMsg">欢迎使用差旅费报销管理信息系统</h1>
        <p>用户名:<input id="UserID" name="UserID" type="text"></p>
        <p>密码:<input id="Password" name="Password" type="password"></p>
        <div id="subDiv">
            <input type="submit" class="button" value="登录">
            <a href="register.html">没有账号?点击注册</a>
        </div>
    </form>
</div>
</body>

</html>
materialcategory.jsp
点击查看代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>物料分类信息管理</title>
    <script>
        function addMaterialCategory() {
            window.location.href = "addMaterialCategory.jsp";
        }
    </script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 800px;
            margin: auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        h2 {
            text-align: center;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: center;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>物料分类信息管理</h2>
    <hr>
    <table>
        <tr>
            <th>序号</th>
            <th>物料编码</th>
            <th>物料名称</th>
            <th>规格</th>
            <th>物料类型</th>
            <th>备注</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${materialCategoryList}" var="category" varStatus="status">
            <tr>
                <td>${status.count}</td>
                <td>${category.materialCode}</td>
                <td>${category.materialName}</td>
                <td>${category.specification}</td>
                <td>${category.material}</td>
                <td>${category.remarks}</td>
                <td>
                    <a href="${pageContext.request.contextPath}/ToEditMaterialCategoryServlet?ID=${category.materialCode}">修改</a>
                    <a href="${pageContext.request.contextPath}/DeleteMaterialCategoryServlet?id=${category.materialCode}">删除</a>
                </td>
            </tr>
        </c:forEach>
    </table>
    <button onclick="addMaterialCategory()">添加物料分类</button>
    <button onclick="window.location.href='${pageContext.request.contextPath}/index.jsp'">返回首页</button>
</div>
</body>
</html>
updateMaterialCategory.jsp
点击查看代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>修改物料分类信息</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 600px;
            margin: auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        h2 {
            text-align: center;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            margin-top: 10px;
        }
        input[type="text"],
        textarea {
            padding: 8px;
            margin-top: 5px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        input[type="submit"] {
            margin-top: 15px;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>修改物料分类信息</h2>
    <form action="${pageContext.request.contextPath}/updateMaterialCategoryServlet" method="post">
        <input type="hidden" name="materialCode" value="${materialCategory.materialCode}">

        <label for="materialName">物料名称:</label>
        <input type="text" id="materialName" name="materialName" value="${materialCategory.materialName}" required>

        <label for="specification">规格:</label>
        <input type="text" id="specification" name="specification" value="${materialCategory.specification}">

        <label for="material">物料类型:</label>
        <input type="text" id="material" name="material" value="${materialCategory.material}">

        <label for="remarks">备注:</label>
        <textarea id="remarks" name="remarks" rows="4">${materialCategory.remarks}</textarea>

        <input type="submit" value="更新物料分类">
    </form>
</div>
</body>
</html>
updateWarehouse.jsp
点击查看代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改仓库信息</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 600px;
            margin: auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        h2 {
            text-align: center;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            margin-top: 10px;
        }
        input[type="text"],
        input[type="tel"],
        input[type="number"] {
            padding: 8px;
            margin-top: 5px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        input[type="submit"] {
            margin-top: 15px;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>修改仓库信息</h2>
    <form action="${pageContext.request.contextPath}/updateWarehouseServlet" method="post">
        <input type="hidden" name="warehouseCode" value="${warehouse.warehouseCode}">

        <label for="warehouseName">仓库名称:</label>
        <input type="text" id="warehouseName" name="warehouseName" value="${warehouse.warehouseName}" required>

        <label for="location">仓库地址:</label>
        <input type="text" id="location" name="location" value="${warehouse.location}">

        <label for="capacity">仓库容量(小数,例如:1000.50):</label>
        <input type="number" id="capacity" name="capacity" value="${warehouse.capacity}" step="0.01">

        <label for="contactPerson">联系人:</label>
        <input type="text" id="contactPerson" name="contactPerson" value="${warehouse.contactPerson}">

        <label for="contactPhone">联系电话:</label>
        <input type="tel" id="contactPhone" name="contactPhone" value="${warehouse.contactPhone}">

        <input type="submit" value="更新仓库信息">
    </form>
</div>
</body>
</html>
warehouse.jsp
点击查看代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>仓库信息打印</title>
    <script>
        function addHouse() {
            window.location.href = "addWarehouse.jsp";
        }
    </script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 800px;
            margin: auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        h2 {
            text-align: center;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: center;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>仓库信息打印</h2>
    <hr>
    <table>
        <tr>
            <th>序号</th>
            <th>仓库编码</th>
            <th>仓库名称</th>
            <th>仓库地址</th>
            <th>仓库容量</th>
            <th>联系人</th>
            <th>联系电话</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${warehouseList}" var="warehouse" varStatus="status">
            <tr>
                <td>${status.count}</td>
                <td>${warehouse.warehouseCode}</td>
                <td>${warehouse.warehouseName}</td>
                <td>${warehouse.location}</td>
                <td>${warehouse.capacity}</td>
                <td>${warehouse.contactPerson}</td>
                <td>${warehouse.contactPhone}</td>
                <td><a href="${pageContext.request.contextPath}/ToFixWarehouseServlet?ID=${warehouse.getWarehouseCode()}">修改</a>
                    <a href="${pageContext.request.contextPath}/DeleteWarehouseServlet?id=${warehouse.getWarehouseCode()}">删除</a>
                </td>
            </tr>
        </c:forEach>
    </table>
    <button onclick="addHouse()">添加仓库</button>
    <button onclick="window.location.href='${pageContext.request.contextPath}/index.jsp'">返回首页</button>
</div>
</body>
</html>
七、总结设计 web.xml
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <welcome-file-list>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>
</web-app>
mybatis-config.xml
点击查看代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <typeAliases>
<!--        记得修改名称-->
        <package name="com.QixunQiu.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!--                数据库名称-->
                <property name="url" value="jdbc:mysql:///cang?useSSL=false&amp;serverTimezone=Hongkong&amp;characterEncoding=utf-8&amp;autoReconnect=true&amp;userServerPreStmts=true"/>
                <property name="username" value="root"/>
                <property name="password" value="1234"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--扫描mapper-->
        <package name="com.QixunQiu.mapper"/>
    </mappers>
</configuration>
SqlSessionFactoryUtils
点击查看代码
package com.QixunQiu.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionFactoryUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        //静态代码块会随着类的加载而自动执行,且执行一次
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }
}

pom.xml
点击查看代码
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>Cang</artifactId>

  <packaging>war</packaging>

  <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
  </properties>

  <version>1.0-SNAPSHOT</version>
  <name>Cang Maven Webapp</name>
  <url>https://maven.apache.org</url>

  <dependencies>
    <!--Servlet-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <!--MyBatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.6</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.32</version>
    </dependency>

    <!--fastjson-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.62</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>javax.servlet.jsp.jstl-api</artifactId>
      <version>1.2.2</version>
    </dependency>


  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <!-- tomcat7的插件, 不同tomcat版本这个也不一样 -->
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <!--        <configuration>-->
        <!--         &lt;!&ndash; 通过maven tomcat7:run运行项目时,访问项目的端口号 &ndash;&gt;-->
        <!--         <port>8080</port>-->
        <!--         &lt;!&ndash; 项目访问路径 本例:localhost:9090, 如果配置的aa, 则访问路径为localhost:9090/aa&ndash;&gt;-->
        <!--         <path>/</path>-->
        <!--        </configuration>-->
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>8</source>
          <target>8</target>
        </configuration>
      </plugin>

    </plugins>

  </build>
</project>

posted @   QixunQiu  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
点击右上角即可分享
微信分享提示