一、需求描述:

1、要求桌面能在window、Linux和macos系统上运行

2、用户自定义数据筛选策略,策略可通过excel导入导出

3、选择多个excel文件通过策略过滤生成新的excel

二、技术选型及集成环境配置:

1、PC端跨平台直接选用javafx来作为桌面开发

2、动态数据规则使用drools以及内存数据库H2

3、excel操作选择阿里的easyexcel

4、基础环境:win10专业版、idea2023.1.3、springboot3.0.8、jdk17

三、基础环境搭建

1、下载jdk17下载地址:Java Downloads | Oracle

2、javafx-sdk和jmode(打exe程序的时候用)下载地址:JavaFX - Gluon

 

3、将jar打包成其他系统可执行文件的工具(wix311)下载 安装:Release WiX Toolset v3.11.2 · wixtoolset/wix3 · GitHub

4、环境变量参考:

 

四、开发项目配置:

1、新建springboot项目

2、pom文件参考

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.8</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.stats.excel</groupId>
    <artifactId>stats-excel</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>stats-excel</name>
    <description>stats-excel</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>

        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 桌面库-->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>17</version>
        </dependency>
        <!-- JavaFX -->
<!--        <dependency>-->
<!--            <groupId>org.openjfx</groupId>-->
<!--            <artifactId>javafx-controls</artifactId>-->
<!--            <version>17</version>-->
<!--        </dependency>-->
        <!-- 表格出来工具 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!-- 数据框架-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.13.5</version>
        </dependency>
        <!-- Drools -->
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>7.61.0.Final</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3、application启动文件参考

package com.stats.excel.statsexcel;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.net.URL;

@SpringBootApplication
public class StatsExcelApplication extends Application {

    // 任何地方都可以通过这个applicationContext获取springboot的上下文
    public static ConfigurableApplicationContext applicationContext;
    private static String[] args;

    @Override
    public void start(Stage primaryStage) throws Exception {
        URL resource = getClass().getResource("/fxml/login.fxml");
        if (resource == null) {
            throw new Exception();
        }
        // 加载 fxml 下面的逻辑可以单独封装
        FXMLLoader loader = new FXMLLoader(resource);
        loader.setControllerFactory(new Callback<Class<?>, Object>() {
            @Override
            public Object call(Class<?> param) {
                // 控制器工厂提供bean注入,此处的缺点是不能根据bean名字注入,只能通过class类型注入bean
                // 解决方案:
                // 1、SpringbootJavafxDemoApplication.applicationContext.getBean("Bean Name", Bean.class);
                // 2、@Autowired private ApplicationContext applicationContext;
                // Object bean_name = applicationContext.getBean("bean Name", Bean.class);
                return applicationContext.getBean(param);
            }
        });
        // 加载
        VBox root = loader.load();

        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


    public static void main(String[] args) {
        StatsExcelApplication.args = args;
        launch(args);
    }

    @Override
    public void init() throws Exception {
        // 启动springboot
        applicationContext = SpringApplication.run(StatsExcelApplication.class, args);
    }

    @Override
    public void stop() throws Exception {
        // 关闭springboot
        applicationContext.stop();
    }

}

4、其他service层、controller层以及数据库配置请参考项目:非生物语言学家 / DesktopAPP桌面应用开发 · GitCode

5、启动类配置:

5.1、新增javafx-sdk启动参数配置

--module-path
"D:\java\install\openjfx-17.0.7_windows-x64_bin-sdk\javafx-sdk-17.0.7\lib"
--add-modules
javafx.controls,javafx.fxml

5.2、 Artifacts依赖(javafx-sdk-17.0.7\bin下的所有文件)导入:

 6、jar转win可执行exe文件命令参考:

jpackage --type exe --input . --dest . --main-jar .\[你打的jar包] --main-class [你工程的包名.启动类]p --module-path "[你的javafx-jmods路径]" --add-modules javafx.controls,javafx.fxml --win-shortcut --win-menu

五、效果展示略

Logo

长江两岸老火锅,共聚山城开发者!We Want You!

更多推荐