Java GUI,mybatis实现资产管理系统

Java GUI——资产管理系统

前言:为了做java课设,学了一手Java GUI。感觉蛮有意思的,写写文章,做个视频记录一下。欢迎大家友善指出我的不足

资产管理系统录制视频,从头敲到尾

模块划分

  • 资产信息管理
    • 资产信息查询 【各种条件查询】
    • 资产信息修改(不能对资产进行领用、归还、报废
    • 资产信息增加
    • 资产信息删除
  • 人员信息管理
    • 人员信息查询 【各种条件查询】
    • 人员信息修改
    • 人员信息增加
    • 人员信息删除
  • 资产设备领用、归还、报废
    • 资产设备操作(仅限于领用、归还、报废
    • 资产设备查询 【各种条件】

数据库创建

资产表

DROP TABLE IF EXISTS `asset_information`;
CREATE TABLE `asset_information`  (
  `asset_number` int NOT NULL AUTO_INCREMENT COMMENT '资产编号',
  `asset_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '资产名称',
  `price` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '价格',
  `purchase_date` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '购买日期',
  `status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '状态 0-默认 1-领用 2-归还 3-报废',
  `remarks` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`asset_number`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1007 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_croatian_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of asset_information
-- ----------------------------
INSERT INTO `asset_information` VALUES (1000, '潇洒哥', '1000', '', '1', '');
INSERT INTO `asset_information` VALUES (1004, '潇洒哥', '10000', '2020-09-07 14:52:54', '0', '是个蛋');

资产操作流水表

DROP TABLE IF EXISTS `asset_operation_log`;
CREATE TABLE `asset_operation_log`  (
  `operation_number` int NOT NULL AUTO_INCREMENT COMMENT '操作编号',
  `operation_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '操作类型 0-默认 1-领用 2-归还 3-报废',
  `asset_number` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '资产编号',
  `operation_time` datetime NULL DEFAULT NULL COMMENT '操作时间',
  `recipient` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '操作人',
  `remarks` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`operation_number`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_croatian_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of asset_operation_log
-- ----------------------------
INSERT INTO `asset_operation_log` VALUES (5, '1', '1000', NULL, '1', '无');

人员信息表

DROP TABLE IF EXISTS `personnel_information`;
CREATE TABLE `personnel_information`  (
  `personnel_number` int NOT NULL AUTO_INCREMENT COMMENT '人员编号',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '姓名',
  `gender` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '性别',
  `department` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '部门',
  `position` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '职位',
  `other` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci NULL DEFAULT NULL COMMENT '其他信息',
  PRIMARY KEY (`personnel_number`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_croatian_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of personnel_information
-- ----------------------------
INSERT INTO `personnel_information` VALUES (1, '潇洒哥', '男', '羊村', '大哥', '是个蛋,白色的蛋蛋');
INSERT INTO `personnel_information` VALUES (2, '飞鸽', '男', '鸽子部门', '二弟', '飞哥不鸽');
INSERT INTO `personnel_information` VALUES (3, '老王', '女', '军乐团', '团长', '牛逼啊');

业务流程

在这里插入图片描述

资产被人员操作后,操作记录写入资产记录表。同时根据人员的操作修改资产本身的信息。

代码编写

maven坐标

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

        <!-- Lombok 依赖 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>
        <!--MyBatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!--MySQL-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.0</version>
            </plugin>
        </plugins>
    </build>

本项目采用模块划编写。不同模块划分为不同的包,每个包下维护若干类,每个类表示一个UI界面。

模块一:主界面

1.1)创建主界面UI

用代码构建如下界面, 但不具备界面跳转等操作
在这里插入图片描述

在这里插入图片描述

package com.xhf.keshe;

import com.xhf.keshe.asset.AssetQuery;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame {
    /**
     * 窗体宽
     */
    public static final int width = 800;
    /**
     * 窗体高
     */
    public static final int height = 600;
    /**
     * 基本字体
     */
    public static final Font font = new Font("仿宋", Font.BOLD, 20);
    /**
     * 创建基本界面
     */
    private JPanel workspace;

    public Main() {
        // 创建窗体
        setTitle("资产管理系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(width, height);

        // 创建基本界面
        initBasePanel();

        // 居中
        setLocationRelativeTo(null);

        // 初始化界面
        initUI();
    }

    /**
     * 创建基本界面
     */
    private void initBasePanel() {
        this.workspace = new JPanel();
        workspace.setLayout(null);
        JLabel jLabel = new JLabel("欢迎使用资产管理系统", JLabel.CENTER);    //创建一个标签
        jLabel.setBounds(140, 100, 400, 60);
        jLabel.setFont(Main.font);
        workspace.add(jLabel, BorderLayout.NORTH);
        add(workspace);
    }

    /**
     * 初始化界面
     */
    private void initUI() {
        // 添加菜单栏组件
        JMenuBar jMenuBar = new JMenuBar();

        // 在菜单栏(bar)中添加若干菜单(menu)
        String[] jMenuNameList = {"资产信息管理", "人员信息管理", "资产设备管理"};
        for (int i = 0; i < 3; ++i) {
            // jMenu的名称
            String jMenuName = jMenuNameList[i];
            JMenu jMenu = new JMenu(jMenuName);
            // 为menu添加item
            addItemForJMenu(jMenu, jMenuName);
            // 设置字体
            jMenu.setFont(font);
            // 将jMenu添加到bar中
            jMenuBar.add(jMenu);
        }

        // 添加jMenuBar
        setJMenuBar(jMenuBar);
    }

    /**
     * 为不同名称的jMenu添加item
     * @param jMenu
     * @param jMenuName
     */
    private void addItemForJMenu(JMenu jMenu, String jMenuName) {
        String[] item1NameList = {"资产信息查询", "资产信息修改", "资产信息增加", "资产信息删除"};
        String[] item2NameList = {"人员信息查询", "人员信息修改", "人员信息增加", "人员信息删除"};
        String[] item3NameList = {"资产设备查询", "资产设备操作"};

        // 判断jMenu名称, 然后添加item
        switch (jMenuName) {
            case "资产信息管理":
                for (String s : item1NameList) {
                    addItem(s, jMenu);
                }
                break;
            case "人员信息管理":
                for (String s : item2NameList) {
                    addItem(s, jMenu);
                }
                break;
            case "资产设备管理":
                for (String s : item3NameList) {
                    addItem(s, jMenu);
                }
                break;
        }
    }

    /**
     * 创建jMenuItem, 同时取名name, 将item添加到menu中
     * @param name
     * @param jMenu
     */
    private void addItem(String name, JMenu jMenu) {
        JMenuItem item = new JMenuItem(name);
        item.setFont(font);
        jMenu.add(item);
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.setVisible(true);
    }
}

1.2)增加主界面的界面跳转逻辑

所有的界面跳转,都可以理解为以下几个操作

  1. 触发相应操作
  2. 清除当前界面
  3. 添加需要界面
  4. 界面重新渲染

分析可知,通过点击item,才触发界面跳转的操作。因此我们需要给不同的item上添加相应的监听器,用于执行界面跳转的逻辑

    /**
     * 创建jMenuItem, 同时取名name, 将item添加到menu中
     * @param name
     * @param jMenu
     */
    private void addItem(String name, JMenu jMenu) {
        JMenuItem item = new JMenuItem(name);
        // 为不同的item添加监听操作
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 为不同的item添加不同的界面
                switch (name) {
                    case "资产信息查询":
                        // 清空当前界面
                        remove(workspace);
                        // 添加想要的界面
                        workspace = new AssetQuery();
                        add(workspace);
                        // 重新绘制界面
                        validate();
                        break;
                }
            }
        });
        item.setFont(font);
        jMenu.add(item);
    }

模块二: 资产界面

2.1)mybatis整合

学习资料

(44条消息) Mybatis基本使用教程(小白向)_敲代码它不香嘛的博客-CSDN博客

入门_MyBatis中文网

2.2.1) 编写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>
    <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://localhost:3306/keshe1"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--后续编写的mapper.xml都需要添加到这里-->
        <mapper resource="mapper/assetInformationMapper.xml"/>
        <mapper resource="mapper/personnelInformationMapper.xml"/>
        <mapper resource="mapper/operateMapper.xml"/>
    </mappers>
</configuration>

如下图,编写mybatis-config.xml【核心配置文件】文件,放置在resources根目录

image-20230710155452363
2.2.2) 获取SqlSession

sqlsession是mybatis中的核心工具,有了它才有后续操作。sqlsession封装jdbc的数据库连接操作

package com.xhf.keshe.utils;

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

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

public class GetSqlsession {
    private static InputStream inputStream;

	//这个方法生成一个生产Sqlsession的工厂,即SqlSessionFactory
    public static SqlSessionFactory createfactory() {
		
        {
            try {
                inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //通过把这个工厂return出来,以便后续通过这个工厂获得SqlSession对象
        return sqlSessionFactory;
    }

	//这个方法获得SqlSession对象
    public static SqlSession getsqlsession(){
        return createfactory().openSession();
    }
}
2.2.3)编写实体类
package com.xhf.keshe.asset.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class AssetInformation implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 资产编号
     */
    private Integer assetNumber;

    /**
     * 资产名称
     */
    private String assetName;

    /**
     * 价格
     */
    private String price;

    /**
     * 购买日期
     */
    private String purchaseDate;

    /**
     * 状态 0-默认 1-领用 2-归还 3-报废
     */
    private String status;

    /**
     * 备注
     */
    private String remarks;
}
2.2.5) 编写mapper接口
package com.xhf.keshe.asset.mapper;

import com.xhf.keshe.asset.entity.AssetInformation;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface AssetInformationMapper {
    /**
     * 查询全部信息
     * @return
     */
    List<AssetInformation> list();
    /**
     * 根据id查询数据
     */
    AssetInformation listById(@Param("id") String id);
    /**
     * 修改
     */
    void update(@Param("entity") AssetInformation entity);
    /**
     * 新增数据
     */
    void insert(@Param("entity") AssetInformation entity);
    /**
     * 根据id删除数据
     */
    void deleteById(@Param("id") Integer id);
}

2.2.6)编写mapper的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.xhf.keshe.asset.mapper.AssetInformationMapper">

    <resultMap id="BaseResultMap" type="com.xhf.keshe.asset.entity.AssetInformation" >
        <result column="asset_number" property="assetNumber" />
        <result column="asset_name" property="assetName" />
        <result column="price" property="price" />
        <result column="purchase_date" property="purchaseDate" />
        <result column="status" property="status" />
        <result column="remarks" property="remarks" />
    </resultMap>

    <insert id="insert" useGeneratedKeys="true" keyProperty="assetNumber">
        INSERT INTO
            asset_information
        VALUES (
            null,
            #{entity.assetName},
            #{entity.price},
            #{entity.purchaseDate},
            #{entity.status},
            #{entity.remarks}
            );
    </insert>
    <update id="update">
        UPDATE asset_information
        <set>
            <if test="entity.assetName != null"> asset_name = #{entity.assetName}, </if>
            <if test="entity.price != null"> price = #{entity.price}, </if>
            <if test="entity.purchaseDate != null"> purchase_date = #{entity.purchaseDate}, </if>
            <if test="entity.status"> status = #{entity.status}, </if>
            <if test="entity.remarks"> remarks = #{entity.remarks} </if>
        </set>
        WHERE
            asset_information.asset_number = #{entity.assetNumber}
    </update>
    <delete id="deleteById">
        DELETE FROM asset_information WHERE asset_information.asset_number = #{id};
    </delete>

    <select id="list" resultMap="BaseResultMap">
        SELECT * FROM asset_information;
    </select>
    <select id="listById" resultMap="BaseResultMap">
        SELECT * FROM asset_information WHERE asset_number = #{id};
    </select>

</mapper>

2.2)查询界面UI

package com.xhf.keshe.asset.page;

import com.xhf.keshe.Main;
import com.xhf.keshe.asset.entity.AssetInformation;
import com.xhf.keshe.asset.mapper.AssetInformationMapper;
import com.xhf.keshe.utils.GetSqlsession;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.*;
import java.util.List;


/**
 * 维护资产信息查询界面
 */
public class AssetQuery extends JPanel {
    // 表格列名
    public static final String[] columnNames = {"资产编号", "资产名称", "价格", "购买日期", "状态", "备注"};
    /**
     * sqlsession
     */
    private SqlSession sqlSession;
    /**
     * mapper
     */
    private AssetInformationMapper mapper;

    {
        sqlSession = GetSqlsession.getsqlsession();
        mapper = sqlSession.getMapper(AssetInformationMapper.class);
    }

    public AssetQuery() {
        // 设置界面大小
        setSize(Main.width, Main.height);
        // 设置布局
        setLayout(new BorderLayout());
        // 设置标题
        JLabel title = new JLabel("资产查询");
        title.setFont(Main.TitleFont);
        // 设置标题为居中对其
        title.setHorizontalAlignment(SwingConstants.CENTER);
        this.add(title, BorderLayout.NORTH);

        // 创建表格数据
        Object[][] data = getData();

        // 创建 JTable
        JTable table = new JTable(data, columnNames);
        // 设置头的字体
        table.getTableHeader().setFont(Main.font);
        // 设置每行的高度
        table.setRowHeight(25);
        // 设置表内容字体
        table.setFont(Main.font);

        // 将表格添加到滚动面板,并将滚动面板添加到窗口
        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane);
    }

    /**
     * 返回资产表格数据
     * @return
     */
    private Object[][] getData() {
        // 返回list数据
        List<AssetInformation> list = mapper.list();
        Object[][] data = new Object[list.size()][columnNames.length];
        // 将list处理为二维数组, 赋值给data
        for (int i = 0; i < list.size(); i++) {
            // 将list[i] -> assetInformation 变为数组
            Object[] res = new Object[columnNames.length];
            AssetInformation assetInformation = list.get(i);
            res[0] = assetInformation.getAssetNumber();
            res[1] = assetInformation.getAssetName();
            res[2] = assetInformation.getPrice();
            res[3] = assetInformation.getPurchaseDate();
            res[4] = assetInformation.getStatus();
            res[5] = assetInformation.getRemarks();
            data[i] = res;
        }
        return data;
    }
}

2.3)添加界面UI

package com.xhf.keshe.asset.page;

import com.xhf.keshe.Main;
import com.xhf.keshe.asset.entity.AssetInformation;
import com.xhf.keshe.asset.mapper.AssetInformationMapper;
import com.xhf.keshe.utils.GetSqlsession;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class AssetAdd extends JPanel {
    /**
     * title宽度
     */
    public static final int widthTitle = 200, heightTitle = 40;
    /**
     * 标题x坐标
     */
    public static final int xTitle = Main.width / 2 - widthTitle / 2;
    /**
     * 标题y坐标
     */
    public static final int yTitle = Main.height / 10;
    /**
     * y轴间距
     */
    public static final int yGap = 40;
    /**
     * 存储所有的JTextField
     */
    private final List<JTextField> jTextFieldList = new ArrayList<JTextField>();
    /**
     * 获取mapper
     */
    private static AssetInformationMapper mapper;
    /**
     * 获取sqlSession
     */
    private static SqlSession sqlSession;

    {
        sqlSession = GetSqlsession.getsqlsession();
        mapper = sqlSession.getMapper(AssetInformationMapper.class);
    }

    /**
     * 提交button
     */
    private final JButton jButton = new JButton("添加");

    public AssetAdd() {
        setLayout(null);
        // 设置title坐标
        JLabel title = new JLabel("添加资产信息");
        title.setFont(Main.TitleFont);
        title.setBounds(xTitle, yTitle, widthTitle, heightTitle);

        // 通过title坐标, 计算剩余组件坐标, 并添加
        int widthLabel = 100, heightLabel = 30, xLabel = xTitle / 2, yLabel = yTitle + yGap + heightLabel, xGap = 80;
        int xText = xLabel + widthLabel + xGap, yText = yLabel, widthText = 300, heightText = 30;

        for (int i = 0; i < AssetQuery.columnNames.length - 1; ++i) {
            // 创建JLabel
            JLabel jLabel = new JLabel(AssetQuery.columnNames[i + 1]);
            jLabel.setFont(Main.font);
            jLabel.setBounds(xLabel, yLabel, widthLabel, heightLabel);
            // 创建JTextField
            JTextField jTextField = new JTextField();
            jTextField.setFont(Main.font);
            jTextField.setBounds(xText, yText, widthText, heightText);
            // 添加
            add(jLabel);
            add(jTextField);
            // 将所有jTextField存储
            jTextFieldList.add(jTextField);
            // 更新位置
            yLabel += yGap;
            yText += yGap;
        }
        // 添加修改button
        jButton.setFont(Main.font);
        jButton.setBounds(xTitle, yText + yGap, widthTitle, heightTitle);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 提交数据
                save();
            }
        });

        add(title);
        add(jButton);
    }

    /**
     * 提交数据
     */
    private void save() {
        System.out.println("保存数据");
        AssetInformation entity = new AssetInformation();
        entity.setAssetName(jTextFieldList.get(0).getText());
        entity.setPrice(jTextFieldList.get(1).getText());
        entity.setPurchaseDate(jTextFieldList.get(2).getText());
        entity.setStatus(jTextFieldList.get(3).getText());
        entity.setRemarks(jTextFieldList.get(4).getText());
        // 保存数据
        mapper.insert(entity);
        // 事务提交
        sqlSession.commit();
        JOptionPane.showMessageDialog(null, "添加成功");
    }
}

2.4)修改界面UI

package com.xhf.keshe.asset.page;

import com.xhf.keshe.Main;
import com.xhf.keshe.asset.entity.AssetInformation;
import com.xhf.keshe.asset.mapper.AssetInformationMapper;
import com.xhf.keshe.utils.GetSqlsession;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class AssetUpdate extends JPanel {
    /**
     * title宽度
     */
    private static final int widthTitle = 200, heightTitle = 40;
    /**
     * 标题x坐标
     */
    private static final int xTitle = Main.width / 2 - widthTitle / 2;
    /**
     * 标题y坐标
     */
    private static final int yTitle = Main.height / 10;
    /**
     * y轴间距
     */
    private static final int yGap = 40;
    /**
     * 存储所有的JTextField
     */
    private final List<JTextField> jTextFieldList = new ArrayList<JTextField>();
    /**
     * sqlsession
     */
    private SqlSession sqlSession;
    /**
     * 获取mapper
     */
    private AssetInformationMapper mapper;

    {
        sqlSession = GetSqlsession.getsqlsession();
        mapper = sqlSession.getMapper(AssetInformationMapper.class);
    }

    /**
     * 提交button
     */
    private final JButton jButton = new JButton("修改");

    public AssetUpdate() {
        setLayout(null);
        // 设置title坐标
        JLabel title = new JLabel("修改资产信息");
        title.setFont(Main.TitleFont);
        title.setBounds(xTitle, yTitle, widthTitle, heightTitle);

        // 通过title坐标, 计算剩余组件坐标, 并添加
        int widthLabel = 100, heightLabel = 30, xLabel = xTitle / 2, yLabel = yTitle + yGap + heightLabel, xGap = 80;
        int xText = xLabel + widthLabel + xGap, yText = yLabel, widthText = 300, heightText = 30;

        for (int i = 0; i < AssetQuery.columnNames.length; ++i) {
            // 创建JLabel
            JLabel jLabel = new JLabel(AssetQuery.columnNames[i]);
            jLabel.setFont(Main.font);
            jLabel.setBounds(xLabel, yLabel, widthLabel, heightLabel);
            // 创建JTextField
            JTextField jTextField = new JTextField();
            jTextField.setFont(Main.font);
            jTextField.setBounds(xText, yText, widthText, heightText);
            // 添加
            add(jLabel);
            add(jTextField);
            // 将所有jTextField存储
            jTextFieldList.add(jTextField);

            if (i == 0) {
                System.out.println("添加button");
                // 添加查询按钮
                JButton jButton = new JButton("查询");
                jButton.setFont(Main.font);
                jButton.setBounds(xText + widthText + 10, yText, 80, heightText);
                jButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // 查询数据
                        getData();
                        // 重置文本框读写状态
                        setField();
                    }
                });
                add(jButton);
            } else {
                // 设置所有的都无法修改
                jTextField.setEnabled(false);
            }
            // 更新位置
            yLabel += yGap;
            yText += yGap;
        }
        // 添加修改button
        jButton.setEnabled(false);
        jButton.setFont(Main.font);
        jButton.setBounds(xTitle, yText + yGap, widthTitle, heightTitle);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 提交数据
                upload();
            }
        });

        add(title);
        add(jButton);
    }

    /**
     * 重置textField的写状态
     */
    private void setField() {
        // 除了状态, 其它都可修改
        for (int i1 = 0; i1 < jTextFieldList.size(); i1++) {
            if (i1 != 4) {
                jTextFieldList.get(i1).setEnabled(true);
            }
        }
    }

    /**
     * 根据id查询数据信息, 并进行回显
     */
    private void getData() {
        // 根据jTextField中的id查询数据, 并进行数据回显
        String id = jTextFieldList.get(0).getText();
        AssetInformation assetInformation = mapper.listById(id);
        // 查不到
        if (assetInformation == null) {
            JOptionPane.showMessageDialog(null, "查询失败");
            return;
        }
        // 修改按钮功能重新开启
        jButton.setEnabled(true);
        // 数据回显
        jTextFieldList.get(0).setText(String.valueOf(assetInformation.getAssetNumber()));
        jTextFieldList.get(1).setText(assetInformation.getAssetName());
        jTextFieldList.get(2).setText(assetInformation.getPrice());
        jTextFieldList.get(3).setText(assetInformation.getPurchaseDate());
        jTextFieldList.get(4).setText(assetInformation.getStatus());
        jTextFieldList.get(5).setText(assetInformation.getRemarks());
    }

    /**
     * 提交数据
     */
    private void upload() {
        AssetInformation entity = new AssetInformation();
        entity.setAssetNumber(Integer.valueOf(jTextFieldList.get(0).getText()));
        entity.setAssetName(jTextFieldList.get(1).getText());
        entity.setPrice(jTextFieldList.get(2).getText());
        entity.setPurchaseDate(jTextFieldList.get(3).getText());
        entity.setStatus(jTextFieldList.get(4).getText());
        entity.setRemarks(jTextFieldList.get(5).getText());
        // 保存数据
        mapper.update(entity);
        sqlSession.commit();
        JOptionPane.showMessageDialog(null, "修改成功");
    }
}

2.5)删除界面UI

package com.xhf.keshe.asset.page;

import com.xhf.keshe.Main;
import com.xhf.keshe.asset.mapper.AssetInformationMapper;
import com.xhf.keshe.utils.GetSqlsession;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AssetDelete extends JPanel {
    /**
     * sqlsession
     */
    private static SqlSession sqlSession;
    /**
     * mapper
     */
    private static AssetInformationMapper assetInformationMapper;

    {
        sqlSession = GetSqlsession.getsqlsession();
        assetInformationMapper = sqlSession.getMapper(AssetInformationMapper.class);
    }

    public AssetDelete() {
        setLayout(null);
        // 设置标题
        JLabel title = new JLabel("资产信息删除");
        title.setFont(Main.TitleFont);
        title.setBounds(AssetAdd.xTitle, AssetAdd.yTitle, AssetAdd.widthTitle, AssetAdd.heightTitle);
        add(title);

        // jLabel设置坐标
        int widthLabel = 100, heightLabel = 30, xLabel = AssetAdd.xTitle / 2, yLabel = AssetAdd.yTitle + AssetAdd.yGap + heightLabel, xGap = 80;
        JLabel jLabel = new JLabel("资产编号");
        jLabel.setFont(Main.font);
        jLabel.setBounds(xLabel, yLabel, widthLabel, heightLabel);

        // 设置JCombox
        int xText = xLabel + widthLabel + xGap, yText = yLabel, widthText = 300, heightText = 30;
        JComboBox jComboBox = new JComboBox();
        jComboBox.setFont(Main.font);
        jComboBox.setBounds(xText, yText, widthText, heightText);
        jComboBox.addItem("请选择");
        // 查询所有的id, 并添加到JComboBox中
        assetInformationMapper.list().forEach(e -> {
            jComboBox.addItem(e.getAssetNumber());
        });

        add(jLabel);
        add(jComboBox);

        // 删除按钮
        JButton jButton = new JButton("删除");
        jButton.setFont(Main.font);
        jButton.setBounds(AssetAdd.xTitle, AssetAdd.yTitle + 120, AssetAdd.widthTitle, AssetAdd.heightTitle);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object id = jComboBox.getSelectedItem();
                assetInformationMapper.deleteById((Integer) id);
                sqlSession.commit();
                JOptionPane.showMessageDialog(null, "删除成功");
            }
        });
        add(jButton);
    }
}

模块三:人员界面

3.1)mybatis整合

3.1.1)编写实体类
package com.xhf.keshe.person.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PersonnelInformation implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
    * 人员编号
    */
    private Integer personnelNumber;

    /**
    * 姓名
    */
    private String name;

    /**
    * 性别
    */
    private String gender;

    /**
    * 部门
    */
    private String department;

    /**
    * 职位
    */
    private String position;

    /**
    * 其他信息
    */
    private String other;
}
3.1.2)编写mapper接口
package com.xhf.keshe.person.mapper;

import com.xhf.keshe.person.entity.PersonnelInformation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface PersonnelInformationMapper {
    /**
     * 查询全部信息
     * @return
     */
    List<PersonnelInformation> list();
    /**
     * 根据id查询数据
     */
    PersonnelInformation listById(@Param("id") String id);
    /**
     * 修改
     */
    void update(@Param("entity") PersonnelInformation entity);
    /**
     * 新增数据
     */
    void insert(@Param("entity") PersonnelInformation entity);
    /**
     * 根据id删除数据
     */
    void deleteById(@Param("id") Integer id);
}

3.1.3)编写mapper的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.xhf.keshe.person.mapper.PersonnelInformationMapper">
    <resultMap id="BaseResultMap" type="com.xhf.keshe.person.entity.PersonnelInformation" >
        <result column="personnel_number" property="personnelNumber" />
        <result column="name" property="name" />
        <result column="gender" property="gender" />
        <result column="department" property="department" />
        <result column="position" property="position" />
        <result column="other" property="other" />
    </resultMap>
    <insert id="insert">
        INSERT INTO
            personnel_information
        VALUES (
            null,
            #{entity.name},
            #{entity.gender},
            #{entity.department},
            #{entity.position},
            #{entity.other}
        )
    </insert>
    <update id="update">
        UPDATE personnel_information
        SET
            `name` = #{entity.name},
            gender = #{entity.gender},
            department = #{entity.department},
            `position` = #{entity.position},
            other = #{entity.other}
        WHERE
            personnel_number = #{entity.personnelNumber};
    </update>
    <delete id="deleteById">
        DELETE FROM personnel_information WHERE personnel_number = #{id};
    </delete>
    <select id="list" resultMap="BaseResultMap">
        SELECT * FROM personnel_information;
    </select>
    <select id="listById" resultMap="BaseResultMap">
        SELECT * FROM personnel_information WHERE personnel_number = #{id};
    </select>

</mapper>

3.2)查询界面UI

package com.xhf.keshe.person.page;

import com.xhf.keshe.Main;
import com.xhf.keshe.person.entity.PersonnelInformation;
import com.xhf.keshe.person.mapper.PersonnelInformationMapper;
import com.xhf.keshe.utils.GetSqlsession;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.*;
import java.util.List;


/**
 * 维护资产信息查询界面
 */
public class PersonQuery extends JPanel {
    /**
     * 获取mapper
     */
    private static PersonnelInformationMapper mapper;
    /**
     * 获取sqlSession
     */
    private static SqlSession sqlSession;

    {
        sqlSession = GetSqlsession.getsqlsession();
        mapper = sqlSession.getMapper(PersonnelInformationMapper.class);
    }

    // 表格列名
    public static final String[] columnNames = {"人员编号", "姓名", "性别", "部门", "职位", "其它信息"};

    public PersonQuery() {
        // 设置界面大小
        setSize(Main.width, Main.height);
        // 设置布局
        setLayout(new BorderLayout());
        // 设置标题
        JLabel title = new JLabel("人员查询");
        title.setFont(Main.TitleFont);
        // 设置标题为居中对其
        title.setHorizontalAlignment(SwingConstants.CENTER);
        this.add(title, BorderLayout.NORTH);

        // 创建表格数据
        Object[][] data = getData();

        // 创建 JTable
        JTable table = new JTable(data, columnNames);
        // 设置头的字体
        table.getTableHeader().setFont(Main.font);
        // 设置每行的高度
        table.setRowHeight(25);
        // 设置表内容字体
        table.setFont(Main.font);

        // 将表格添加到滚动面板,并将滚动面板添加到窗口
        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane);
    }

    /**
     * 返回资产表格数据
     * @return
     */
    private Object[][] getData() {
        // 返回list数据
        List<PersonnelInformation> list = mapper.list();
        System.out.println(list.toString());

        Object[][] data = new Object[list.size()][columnNames.length];
        // 将list处理为二维数组, 赋值给data
        for (int i = 0; i < list.size(); i++) {
            // 将list[i] -> assetInformation 变为数组
            Object[] res = new Object[columnNames.length];
            PersonnelInformation personnelInformation = list.get(i);
            res[0] = personnelInformation.getPersonnelNumber();
            res[1] = personnelInformation.getName();
            res[2] = personnelInformation.getGender();
            res[3] = personnelInformation.getDepartment();
            res[4] = personnelInformation.getPosition();
            res[5] = personnelInformation.getOther();
            data[i] = res;
        }
        return data;
    }
}

3.3)添加界面UI

package com.xhf.keshe.person.page;

import com.xhf.keshe.Main;
import com.xhf.keshe.person.entity.PersonnelInformation;
import com.xhf.keshe.person.mapper.PersonnelInformationMapper;
import com.xhf.keshe.utils.GetSqlsession;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class PersonAdd extends JPanel {
    /**
     * title宽度
     */
    public static final int widthTitle = 200, heightTitle = 40;
    /**
     * 标题x坐标
     */
    public static final int xTitle = Main.width / 2 - widthTitle / 2;
    /**
     * 标题y坐标
     */
    public static final int yTitle = Main.height / 10;
    /**
     * y轴间距
     */
    public static final int yGap = 40;
    /**
     * 存储所有的JTextField
     */
    private final List<JTextField> jTextFieldList = new ArrayList<JTextField>();
    /**
     * 获取mapper
     */
    private static PersonnelInformationMapper mapper;
    /**
     * 获取sqlSession
     */
    private static SqlSession sqlSession;

    {
        sqlSession = GetSqlsession.getsqlsession();
        mapper = sqlSession.getMapper(PersonnelInformationMapper.class);
    }

    /**
     * 提交button
     */
    private final JButton jButton = new JButton("添加");

    public PersonAdd() {
        setLayout(null);
        // 设置title坐标
        JLabel title = new JLabel("添加人员信息");
        title.setFont(Main.TitleFont);
        title.setBounds(xTitle, yTitle, widthTitle, heightTitle);

        // 通过title坐标, 计算剩余组件坐标, 并添加
        int widthLabel = 100, heightLabel = 30, xLabel = xTitle / 2, yLabel = yTitle + yGap + heightLabel, xGap = 80;
        int xText = xLabel + widthLabel + xGap, yText = yLabel, widthText = 300, heightText = 30;

        for (int i = 0; i < PersonQuery.columnNames.length - 1; ++i) {
            // 创建JLabel
            JLabel jLabel = new JLabel(PersonQuery.columnNames[i + 1]);
            jLabel.setFont(Main.font);
            jLabel.setBounds(xLabel, yLabel, widthLabel, heightLabel);
            // 创建JTextField
            JTextField jTextField = new JTextField();
            jTextField.setFont(Main.font);
            jTextField.setBounds(xText, yText, widthText, heightText);
            // 添加
            add(jLabel);
            add(jTextField);
            // 将所有jTextField存储
            jTextFieldList.add(jTextField);
            // 更新位置
            yLabel += yGap;
            yText += yGap;
        }
        // 添加修改button
        jButton.setFont(Main.font);
        jButton.setBounds(xTitle, yText + yGap, widthTitle, heightTitle);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 提交数据
                save();
            }
        });

        add(title);
        add(jButton);
    }

    /**
     * 提交数据
     */
    private void save() {
        System.out.println("保存数据");
        PersonnelInformation entity = new PersonnelInformation();
        entity.setName(jTextFieldList.get(0).getText());
        entity.setGender(jTextFieldList.get(1).getText());
        entity.setDepartment(jTextFieldList.get(2).getText());
        entity.setPosition(jTextFieldList.get(3).getText());
        entity.setOther(jTextFieldList.get(4).getText());
        // 保存数据
        mapper.insert(entity);
        // 事务提交
        sqlSession.commit();
        JOptionPane.showMessageDialog(null, "添加成功");
    }
}

3.4)修改界面UI

package com.xhf.keshe.person.page;

import com.xhf.keshe.Main;
import com.xhf.keshe.person.entity.PersonnelInformation;
import com.xhf.keshe.person.mapper.PersonnelInformationMapper;
import com.xhf.keshe.utils.GetSqlsession;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class PersonUpdate extends JPanel {
    /**
     * title宽度
     */
    private static final int widthTitle = 200, heightTitle = 40;
    /**
     * 标题x坐标
     */
    private static final int xTitle = Main.width / 2 - widthTitle / 2;
    /**
     * 标题y坐标
     */
    private static final int yTitle = Main.height / 10;
    /**
     * y轴间距
     */
    private static final int yGap = 40;
    /**
     * 存储所有的JTextField
     */
    private final List<JTextField> jTextFieldList = new ArrayList<JTextField>();
    /**
     * sqlsession
     */
    private SqlSession sqlSession;
    /**
     * 获取mapper
     */
    private PersonnelInformationMapper mapper;

    {
        sqlSession = GetSqlsession.getsqlsession();
        mapper = sqlSession.getMapper(PersonnelInformationMapper.class);
    }

    /**
     * 提交button
     */
    private final JButton jButton = new JButton("修改");

    public PersonUpdate() {
        setLayout(null);
        // 设置title坐标
        JLabel title = new JLabel("修改人员信息");
        title.setFont(Main.TitleFont);
        title.setBounds(xTitle, yTitle, widthTitle, heightTitle);

        // 通过title坐标, 计算剩余组件坐标, 并添加
        int widthLabel = 100, heightLabel = 30, xLabel = xTitle / 2, yLabel = yTitle + yGap + heightLabel, xGap = 80;
        int xText = xLabel + widthLabel + xGap, yText = yLabel, widthText = 300, heightText = 30;

        for (int i = 0; i < PersonQuery.columnNames.length; ++i) {
            // 创建JLabel
            JLabel jLabel = new JLabel(PersonQuery.columnNames[i]);
            jLabel.setFont(Main.font);
            jLabel.setBounds(xLabel, yLabel, widthLabel, heightLabel);
            // 创建JTextField
            JTextField jTextField = new JTextField();
            jTextField.setFont(Main.font);
            jTextField.setBounds(xText, yText, widthText, heightText);
            // 添加
            add(jLabel);
            add(jTextField);
            // 将所有jTextField存储
            jTextFieldList.add(jTextField);

            if (i == 0) {
                System.out.println("添加button");
                // 添加查询按钮
                JButton jButton = new JButton("查询");
                jButton.setFont(Main.font);
                jButton.setBounds(xText + widthText + 10, yText, 80, heightText);
                jButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // 查询数据
                        getData();
                        // 重置文本框读写状态
                        setField();
                    }
                });
                add(jButton);
            } else {
                // 设置所有的都无法修改
                jTextField.setEnabled(false);
            }
            // 更新位置
            yLabel += yGap;
            yText += yGap;
        }
        // 添加修改button
        jButton.setEnabled(false);
        jButton.setFont(Main.font);
        jButton.setBounds(xTitle, yText + yGap, widthTitle, heightTitle);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 提交数据
                upload();
            }
        });

        add(title);
        add(jButton);
    }

    /**
     * 重置textField的写状态
     */
    private void setField() {
        // 除了状态, 其它都可修改
        for (int i1 = 0; i1 < jTextFieldList.size(); i1++) {
            jTextFieldList.get(i1).setEnabled(true);
        }
    }

    /**
     * 根据id查询数据信息, 并进行回显
     */
    private void getData() {
        // 根据jTextField中的id查询数据, 并进行数据回显
        String id = jTextFieldList.get(0).getText();
        PersonnelInformation personnelInformation = mapper.listById(id);
        // 查不到
        if (personnelInformation == null) {
            JOptionPane.showMessageDialog(null, "查询失败");
            return;
        }
        // 修改按钮功能重新开启
        jButton.setEnabled(true);
        // 数据回显
        jTextFieldList.get(0).setText(String.valueOf(personnelInformation.getPersonnelNumber()));
        jTextFieldList.get(1).setText(personnelInformation.getName());
        jTextFieldList.get(2).setText(personnelInformation.getGender());
        jTextFieldList.get(3).setText(personnelInformation.getDepartment());
        jTextFieldList.get(4).setText(personnelInformation.getPosition());
        jTextFieldList.get(5).setText(personnelInformation.getOther());
    }

    /**
     * 提交数据
     */
    private void upload() {
        PersonnelInformation entity = new PersonnelInformation();
        entity.setPersonnelNumber(Integer.valueOf(jTextFieldList.get(0).getText()));
        entity.setName(jTextFieldList.get(1).getText());
        entity.setGender(jTextFieldList.get(2).getText());
        entity.setDepartment(jTextFieldList.get(3).getText());
        entity.setPosition(jTextFieldList.get(4).getText());
        entity.setOther(jTextFieldList.get(5).getText());
        // 保存数据
        mapper.update(entity);
        sqlSession.commit();
        JOptionPane.showMessageDialog(null, "修改成功");
    }
}

3.5)删除界面UI

package com.xhf.keshe.person.page;

import com.xhf.keshe.Main;
import com.xhf.keshe.asset.page.AssetAdd;
import com.xhf.keshe.person.mapper.PersonnelInformationMapper;
import com.xhf.keshe.utils.GetSqlsession;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PersonDelete extends JPanel {
    /**
     * sqlsession
     */
    private static SqlSession sqlSession;
    /**
     * mapper
     */
    private static PersonnelInformationMapper mapper;

    {
        sqlSession = GetSqlsession.getsqlsession();
        mapper = sqlSession.getMapper(PersonnelInformationMapper.class);
    }

    public PersonDelete() {
        setLayout(null);
        // 设置标题
        JLabel title = new JLabel("人员信息删除");
        title.setFont(Main.TitleFont);
        title.setBounds(AssetAdd.xTitle, AssetAdd.yTitle, AssetAdd.widthTitle, AssetAdd.heightTitle);
        add(title);

        // jLabel设置坐标
        int widthLabel = 100, heightLabel = 30, xLabel = AssetAdd.xTitle / 2, yLabel = AssetAdd.yTitle + AssetAdd.yGap + heightLabel, xGap = 80;
        JLabel jLabel = new JLabel("人员id");
        jLabel.setFont(Main.font);
        jLabel.setBounds(xLabel, yLabel, widthLabel, heightLabel);

        // 设置JCombox
        int xText = xLabel + widthLabel + xGap, yText = yLabel, widthText = 300, heightText = 30;
        JComboBox jComboBox = new JComboBox();
        jComboBox.setFont(Main.font);
        jComboBox.setBounds(xText, yText, widthText, heightText);
        jComboBox.addItem("请选择");
        // 查询所有的id, 并添加到JComboBox中
        mapper.list().forEach(e -> {
            jComboBox.addItem(e.getPersonnelNumber());
        });

        add(jLabel);
        add(jComboBox);

        // 删除按钮
        JButton jButton = new JButton("删除");
        jButton.setFont(Main.font);
        jButton.setBounds(AssetAdd.xTitle, AssetAdd.yTitle + 120, AssetAdd.widthTitle, AssetAdd.heightTitle);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object id = jComboBox.getSelectedItem();
                mapper.deleteById((Integer) id);
                sqlSession.commit();
                JOptionPane.showMessageDialog(null, "删除成功");
            }
        });
        add(jButton);
    }
}

模块四:资产操作

4.1)mybatis整合

4.1.1)编写实体类
package com.xhf.keshe.operate.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class AssetOperationLog implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 操作编号
     */
    private Integer operationNumber;

    /**
     * 操作类型
     */
    private String operationType;

    /**
     * 资产编号
     */
    private String assetNumber;

    /**
     * 操作时间
     */
    private Date operationTime;

    /**
     * 领用人
     */
    private String recipient;
    /**
     * 备注
     */
    private String remarks;
}
4.1.2)编写mapper接口
package com.xhf.keshe.operate.mapper;

import com.xhf.keshe.operate.entity.AssetOperationLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface OperateMapper {
    /**
     * 查询所有数据
     * @return
     */
    List<AssetOperationLog> list();
    /**
     * 保存
     */
    void insert(@Param("entity") AssetOperationLog entity);
}

4.1.3)编写mapper的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.xhf.keshe.operate.mapper.OperateMapper">

    <resultMap id="BaseResultMap" type="com.xhf.keshe.operate.entity.AssetOperationLog" >
        <result column="operation_number" property="operationNumber" />
        <result column="operation_type" property="operationType" />
        <result column="asset_number" property="assetNumber" />
        <result column="operation_time" property="operationTime" />
        <result column="recipient" property="recipient" />
        <result column="remarks" property="remarks" />
    </resultMap>
    <insert id="insert">
        INSERT INTO
            asset_operation_log
        VALUES (
            null,
            #{entity.operationType},
            #{entity.assetNumber},
            #{entity.operationTime},
            #{entity.recipient},
            #{entity.remarks}
        );
    </insert>

    <select id="list" resultMap="BaseResultMap">
        SELECT * FROM asset_operation_log;
    </select>

</mapper>

4.2)资产查询界面

package com.xhf.keshe.operate.page;

import com.xhf.keshe.Main;
import com.xhf.keshe.operate.entity.AssetOperationLog;
import com.xhf.keshe.operate.mapper.OperateMapper;
import com.xhf.keshe.utils.GetSqlsession;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class OperateQuery extends JPanel {
    /**
     * 列名
     */
    public static final String[] columnNames = {"操作编号", "操作类型", "资产编号", "操作时间", "操作人", "备注"};
    /**
     * sqlsession
     */
    private SqlSession sqlSession;
    /**
     * mapper
     */
    private OperateMapper mapper;

    {
        sqlSession = GetSqlsession.getsqlsession();
        mapper = sqlSession.getMapper(OperateMapper.class);
    }

    public OperateQuery() {
        // 设置界面大小
        setSize(Main.width, Main.height);
        // 设置布局
        setLayout(new BorderLayout());
        // 设置标题
        JLabel title = new JLabel("操作查询");
        title.setFont(Main.TitleFont);
        // 设置标题为居中对其
        title.setHorizontalAlignment(SwingConstants.CENTER);
        this.add(title, BorderLayout.NORTH);

        // 创建表格数据
        Object[][] data = getData();

        // 创建 JTable
        JTable table = new JTable(data, columnNames);
        // 设置头的字体
        table.getTableHeader().setFont(Main.font);
        // 设置每行的高度
        table.setRowHeight(25);
        // 设置表内容字体
        table.setFont(Main.font);

        // 将表格添加到滚动面板,并将滚动面板添加到窗口
        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane);
    }

    /**
     * 返回资产表格数据
     * @return
     */
    private Object[][] getData() {
        // 返回list数据
        List<AssetOperationLog> list = mapper.list();
        Object[][] data = new Object[list.size()][columnNames.length];
        // 将list处理为二维数组, 赋值给data
        for (int i = 0; i < list.size(); i++) {
            // 将list[i] -> assetInformation 变为数组
            Object[] res = new Object[columnNames.length];
            AssetOperationLog operationLog = list.get(i);
            res[0] = operationLog.getOperationNumber();
            res[1] = operationLog.getOperationType();
            res[2] = operationLog.getAssetNumber();
            res[3] = operationLog.getOperationTime();
            res[4] = operationLog.getRecipient();
            res[5] = operationLog.getRemarks();
            data[i] = res;
        }
        return data;
    }
}

4.3)资产操作界面

package com.xhf.keshe.operate.page;

import com.xhf.keshe.Main;
import com.xhf.keshe.asset.entity.AssetInformation;
import com.xhf.keshe.asset.mapper.AssetInformationMapper;
import com.xhf.keshe.asset.page.AssetQuery;
import com.xhf.keshe.operate.entity.AssetOperationLog;
import com.xhf.keshe.operate.mapper.OperateMapper;
import com.xhf.keshe.utils.GetSqlsession;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class Operate extends JPanel {
    /**
     * sqlsession
     */
    private SqlSession sqlsession;
    /**
     * mapper
     */
    private OperateMapper mapper;
    /**
     * mapper
     */
    private AssetInformationMapper mapper2;
    /**
     * 资产编号
     */
    private JTextField jTextField1;
    /**
     * 操作人员编号
     */
    private JTextField jTextField2;
    /**
     * 备注
     */
    private JTextField jTextField3;
    /**
     * 操作类型
     */
    private JComboBox comboBox;
    {
        sqlsession = GetSqlsession.getsqlsession();
        mapper = sqlsession.getMapper(OperateMapper.class);
        mapper2 = sqlsession.getMapper(AssetInformationMapper.class);
    }

    public Operate() {
        // 设置布局
        setLayout(new BorderLayout());
        JLabel title = new JLabel("资产设备操作");
        title.setFont(Main.TitleFont);
        title.setHorizontalAlignment(SwingConstants.CENTER);
        add(title, BorderLayout.NORTH);

        // 设置table
        Object[][] data = getData();
        JTable table = new JTable(data, AssetQuery.columnNames);
        table.setFont(Main.font);
        table.setRowHeight(30);
        table.getTableHeader().setFont(Main.font);

        // 添加到滚轴
        JScrollPane jScrollPane = new JScrollPane(table);
        add(jScrollPane, BorderLayout.CENTER);

        // 添加底栏
        int hgap = 30, vgap = 5;
        JPanel bottomBar = new JPanel();
        bottomBar.setLayout(new GridLayout(3, 1, hgap, vgap));

        // 上栏
        JPanel upper = new JPanel();
        upper.setLayout(new GridLayout(1, 4, hgap, vgap));
        initUpper(upper);
        // 中栏
        JPanel middle = new JPanel();
        middle.setLayout(new GridLayout(1, 4, hgap, vgap));
        initMiddle(middle);

        // 下栏
        JPanel down = new JPanel();
        down.setLayout(new GridLayout(1, 1));
        JButton jButton = new JButton("确定");
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 保存日志
                saveLog();
                // 修改资产
                updateAsset();
                JOptionPane.showMessageDialog(null, "操作成功");
            }
        });
        jButton.setFont(Main.font);
        down.add(jButton);

        bottomBar.add(upper);
        bottomBar.add(middle);
        bottomBar.add(down);

        add(bottomBar, BorderLayout.SOUTH);
    }

    /**
     * 修改资产
     */
    private void updateAsset() {
        AssetInformation assetInformation = new AssetInformation();
        assetInformation.setAssetNumber(Integer.valueOf(jTextField1.getText()));
        assetInformation.setStatus(getNumber((String) comboBox.getSelectedItem()));
        mapper2.update(assetInformation);
        sqlsession.commit();
    }

    /**
     * 修改日志
     */
    private void saveLog() {
        AssetOperationLog operationLog = new AssetOperationLog();
        // 设置资产id
        operationLog.setAssetNumber(String.valueOf(jTextField1.getText()));
        // 设置操作人
        operationLog.setRecipient(jTextField2.getText());
        // 设置操作
        operationLog.setOperationType(getNumber((String) comboBox.getSelectedItem()));
        // 设置备注
        operationLog.setRemarks(jTextField3.getText());
        // 保存操作日志
        mapper.insert(operationLog);
        sqlsession.commit();
    }

    /**
     * 根据操作目的, 返回对应数值
     * @param selectedItem
     * @return
     */
    private String getNumber(String selectedItem) {
        if (selectedItem.equals("领用")) {
            return "1";
        }else if (selectedItem.equals("归还")) {
            return "2";
        }else {
            return "3";
        }
    }

    /**
     * 初始化middle栏界面
     * @param middle
     */
    private void initMiddle(JPanel middle) {
        JLabel jLabel = new JLabel("操作");
        jLabel.setFont(Main.font);
        middle.add(jLabel);

        comboBox = new JComboBox();
        comboBox.setFont(Main.font);
        comboBox.addItem("领用");
        comboBox.addItem("归还");
        comboBox.addItem("报废");
        middle.add(comboBox);

        JLabel jLabel1 = new JLabel("备注");
        jLabel1.setFont(Main.font);
        middle.add(jLabel1);
        jTextField3 = new JTextField();
        middle.add(jTextField3);
    }

    /**
     * 初始化upper栏界面
     * @param upper
     */
    private void initUpper(JPanel upper) {
        JLabel jLabel = new JLabel("资产编号");
        jLabel.setFont(Main.font);
        upper.add(jLabel);
        jTextField1 = new JTextField();
        upper.add(jTextField1);

        JLabel jLabel1 = new JLabel("操作人员编号");
        jLabel1.setFont(Main.font);
        upper.add(jLabel1);
        jTextField2 = new JTextField();
        upper.add(jTextField2);
    }

    /**
     * 获取数据
     * @return
     */
    private Object[][] getData() {
        // 返回list数据
        List<AssetInformation> list = mapper2.list();
        Object[][] data = new Object[list.size()][AssetQuery.columnNames.length];
        // 将list处理为二维数组, 赋值给data
        for (int i = 0; i < list.size(); i++) {
            // 将list[i] -> assetInformation 变为数组
            Object[] res = new Object[AssetQuery.columnNames.length];
            AssetInformation assetInformation = list.get(i);
            res[0] = assetInformation.getAssetNumber();
            res[1] = assetInformation.getAssetName();
            res[2] = assetInformation.getPrice();
            res[3] = assetInformation.getPurchaseDate();
            res[4] = assetInformation.getStatus();
            res[5] = assetInformation.getRemarks();
            data[i] = res;
        }
        return data;
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/71235.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

SpringBoot在线失物招领系统

一个基于SpringBootSemanticUI的pc Web在线失物招领系统 http://localhost:8080/swzl/index 主页 http://localhost:8080/swzl/login 登录页 用户表user admin字段为true是管理员 false用户 springboot2.3 springmvc mybatis html ajax idea 或eclipse maven mys…

【Linux进行时】进程概念

进程的概念 什么是进程呢&#xff1f; ❓首先我们需要认识一下什么叫进程呢&#xff1f; 课本概念&#xff1a;程序的一个执行实例&#xff0c;正在执行的程序等 &#x1f525;内核观点&#xff1a;担当分配系统资源&#xff08;CPU时间&#xff0c;内存&#xff09;的实体。…

SpringBoot06---前端路由VueRouter

单页面应用&#xff0c;意思是只有一个html&#xff0c;变化的内容是不同组件进行切换&#xff0c;每个组件加载网络请求&#xff0c;渲染对应的数据&#xff0c;这个内容就是学习怎么完成组件切换 以网易云音乐为例&#xff1a; 网易云音乐 (163.com) 现在无需注册&#xf…

springBoot的日志文件

日志是程序的重要组成部分&#xff0c;主要可以用来定位和排查问题。除此之外&#xff0c;还可以用来&#xff1a; 1. 记录用户的登录日志&#xff0c;方便分析用户是正常登录还是恶意破解&#xff1b; 2. 记录系统的操作日志&#xff0c;方便数据恢复和定位操作人&#xff1b;…

docker 安装elasticsearch、kibana

下载es镜像 docker pull elasticsearch 启动es容器 docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.typesingle-node" -e ES_JAVA_OPTS"-Xms512m -Xmx512m" -d elasticsearch 验证es界面访问 ​​​​​http://节点ip:9200/ ​…

leetcode 416. 分割等和子集

2023.8.12 太难了ToT.... 一题做了一个下午。 本题是动规题目的0-1背包问题系列。将分割子集转化为 在nums数组中任取任意元素使其和等于总元素和的一半&#xff0c;即可满足题目条件。 1、使用一个bool型二维dp数组&#xff0c;dp[i][j] 的含义是&#xff1a;任取nums数组在索…

已有公司将ChatGPT集成到客服中心以增强用户体验

Ozonetel正在利用ChatGPT来改善客户体验。该公司表示&#xff0c;他们通过使用ChatGPT收集与客户互动过程收集的“语料”能够更有针对性地提高服务效率&#xff0c;提供个性化的用户体验&#xff0c;并实现更高的客户满意度。[1] 通过这套解决方案&#xff0c;客服中心将拥有一…

27.Netty源码之FastThreadLocal

highlight: arduino-light FastThreadLocal FastThreadLocal 的实现与 ThreadLocal 非常类似&#xff0c;Netty 为 FastThreadLocal 量身打造了 FastThreadLocalThread 和 InternalThreadLocalMap 两个重要的类。下面我们看下这两个类是如何实现的。 FastThreadLocalThread 是对…

c++文件流详细笔记

c++流 IO :向设备输入数据和输出数据 C++的IO流 设备: 文件控制台特定的数据类型(stringstream)c++中,必须通过特定的已经定义好的类, 来处理IO(输入输出) 文件流 文件流: 对文件进行读写操作 头文件: 类库: ifstream 对文件输入(读文件) ofstream 对文件输出(写…

idea中如何处理飘红提示

idea中如何处理飘红提示 在写sql时&#xff0c;总是会提示各种错误 查找资料&#xff0c;大部分都是说关提示&#xff0c;这里把错误提示选择为None即可 关掉以后&#xff0c;也确实不显示任何提示了&#xff0c;但总有一种掩耳盗铃的感觉 这个sms表明明存在&#xff0c;但是还…

深度学习常用的python库学习笔记

文章目录 数据分析四剑客Numpyndarray数组和标量之间的运算基本的索引和切片数学和统计方法线性代数 PandasMatplotlibPIL 数据分析四剑客 Numpy Numpy中文网 ndarray 数组和标量之间的运算 基本的索引和切片 数学和统计方法 线性代数 Pandas Pandas中文网 Matplotlib Mat…

MuMu模拟器运行一段时间后Device.Present耗时突然上升

1&#xff09;MuMu模拟器运行一段时间后Device.Present耗时突然上升 2&#xff09;​如何在运行过程中获得温度信息 3&#xff09;Input System鼠标更换主按键的Bug 4&#xff09;如何禁止Unity向https://config.uca.cloud.unity3d.com发送设备信息 这是第347篇UWA技术知识分享…

Leetcode-每日一题【剑指 Offer 26. 树的子结构】

题目 输入两棵二叉树A和B&#xff0c;判断B是不是A的子结构。(约定空树不是任意一个树的子结构) B是A的子结构&#xff0c; 即 A中有出现和B相同的结构和节点值。 例如: 给定的树 A: 3 / \ 4 5 / \ 1 2 给定的树 B&#xff1a; 4 / 1 返回 true&#xff0…

C++笔记之静态成员函数的使用场景

C笔记之静态成员函数的使用场景 C静态成员函数的核心特点是不与特定类实例相关&#xff0c;可通过类名直接调用&#xff0c;用于执行与类相关的操作而无需创建类对象。其主要用途是在类级别上共享功能&#xff0c;管理全局状态或提供工具函数。 code review! 文章目录 C笔记之…

悬崖传感器调试问题总结

悬崖传感器原理 使用ADC采样电路&#xff0c;周期的进行开/关灯&#xff0c;获取ADC采样值。根据预先设置好ADC门限&#xff0c;判断是否为悬崖。ADC的精度是12位&#xff0c;对应电路的电压是3.3伏&#xff0c;悬崖传感器通过开灯和关灯&#xff0c;接收的不同灯光强度&#x…

[数据集][目标检测]道路坑洼目标检测数据集VOC格式1510张2类别

数据集格式&#xff1a;Pascal VOC格式(不包含分割路径的txt文件和yolo格式的txt文件&#xff0c;仅仅包含jpg图片和对应的xml) 图片数量(jpg文件个数)&#xff1a;1510 标注数量(xml文件个数)&#xff1a;1510 标注类别数&#xff1a;2 标注类别名称:["keng","…

Redis缓存设计

缓存能够有效地加速应用的读写速度&#xff0c;同时也可以降低后端负载&#xff0c;对日常应用的开发至关重要。但是将缓存加入应用架构后也会带来一些问题&#xff0c;本文将针对这些问题介绍缓存使用技巧和设计方案。 1缓存的收益和成本 下图左侧为客户端直接调用存储层的架…

vector【1】介绍与使用(超详解哦)

vector 引言vector介绍接口使用默认成员函数迭代器容量元素访问数据修改 总结 引言 在string部分&#xff0c;我们详细的介绍了各个接口的使用&#xff0c;虽然其不属于STL的一部分&#xff0c;但是接口与STL中的容器相似&#xff0c;所以我们在学习使用vector等STL容器的使用…

JavaScript之BOM+window对象+定时器+location,navigator,history对象

一.BOM概述 BOM即浏览器对象模型,它提供了独立于内容而与窗口进行交互的对象 BOM的顶级对象是window 二.window对象的常见事件 1.窗口加载事件window.onload window.onload function(){} 或者 window.addEventListener("onload" , function(){}); window.onlo…

websocket知识点

http协议 http协议特点&#xff1a; 无状态协议每个请求是独立的单双工通信&#xff0c;且服务器无法主动给客户端发信息http协议受浏览器同源策略影响 http实现双向通信方法: 轮询长轮询iframe流sse EventSource websocket协议 websocket协议: 全双工协议支持跨域支持多…