肝了一個半月的 Java 項目快速開發腳手架:Chewing

前言

閒來無事,整一個 Java 項目快速開發腳手架。前端

正文

1、簡介

Chewing 是一個簡單的 Java 項目快速開發腳手架。既適合須要開發小型項目的小夥伴使用,也適合剛入門的新手用來學習一些經常使用的技術。java

2、源碼

源碼地址:https://github.com/jingqueyimu/chewinggit

3、核心技術

一、後端

  • SpringBoot:Web 應用框架。
  • MyBatis:持久層框架。
  • MySQL:關係型數據庫。
  • Redis:緩存數據庫。
  • RabbitMQ:消息隊列中間件。

二、前端

  • Thymeleaf:模板引擎。
  • Bootstrap:UI 框架。

4、環境部署

一、準備工做

  • JDK 1.8
  • MySQL 5.7
  • Maven 3.x
  • Redis
  • RabbitMQ

二、必要配置

  • application.properties 配置文件:數據庫、Redis、RabbitMQ、郵箱等配置。
  • config/myconfig.properties 配置文件:系統相關的自定義配置。

5、項目介紹

一、文件結構

src/main/java
    |— com.jingqueyimu
        |— annotation        // 註解
        |— aspect        // 切面
        |— config        // 配置
        |— constant        // 常量
        |— context        // 上下文
        |— controller        // 控制層
        |— exception        // 異常
        |— factory        // 工廠
        |— filter        // 過濾器
        |— handler        // 處理器
        |— init        // 初始化
        |— interceptor        // 攔截器
        |— mapper        // 持久層
        |— model        // 數據模型
        |— mq        // 消息隊列
        |— schedule        // 調度
        |— service        // 服務層
        |— util        // 工具
        MyAppcation.java        // 應用啓動類
src/main/resources
    |— config        // 配置文件
    |— mapper        // 映射文件
    |— static        // 靜態文件
    |— templates        // 頁面文件
    application.properties        // 應用配置文件
    quartz.properties        // 調度配置文件

二、代碼說明

(1)路由

  • /api/xxx:須要用戶登陸。
  • /console/xxx:須要管理員登陸,登陸、登出等部分接口除外。

(2)接口規範

  • 頁面請求:GET、URL 參數。
  • 接口請求:POST、JSON 參數。

(3)數據庫初始化

  • 初始化配置文件:config/dbinit.json。
  • 配置初始化標識及 SQL 語句。

初始化示例:github

[
    {
        "initKey": "site_config_20210110",
        "sqls": [
            "INSERT INTO t_site_config (id, code, name, content, description, public_flag, gmt_create) VALUES(NULL, 'site_record_no', '網站備案號', '<a href=\"https://beian.miit.gov.cn\" class=\"ml-2\" target=\"_blank\">備案號</a>', '網站備案號', true, NOW());"
        ]
    }
]

(4)文件上傳下載

  • 上傳單個文件:/file/upload。
  • 上傳多個文件:/file/uploads。
  • 下載文件:/file/download。

(5)Excel 導入導出

  • Excel 導入導出方法:ExcelUtil.importExcel()、ExcelUtil.exportExcel()。
  • Excel 導入導出處理器接口:IExcelImportHandler、IExcelExportHandler。

導入示例:sql

String msg = null;
try {
    String[] keys = new String[] {"username", "realName", "mobile"};
    msg = ExcelUtil.importExcel(file.getInputStream(), keys, new IExcelImportHandler() {
        
        @Override
        public void handle(JSONObject data) {
            if (StringUtils.isBlank(data.getString("username"))) {
                throw new RuntimeException("用戶名不能爲空");
            }
            if (StringUtils.isBlank(data.getString("mobile"))) {
                throw new RuntimeException("手機號不能爲空");
            }
            // 業務處理
        }
    });
    log.info(msg);
} catch (IOException e) {
    e.printStackTrace();
}

導出示例:數據庫

ServletOutputStream os = null;
try {
    String fileName = "用戶列表";
    ...
    // 用戶列表數據
    List<User> list = userService.list(params);
    // 表頭
    String[] headers = new String[] {"編號 ", "用戶名", "姓名", "手機號", "郵箱", "註冊方式", "註冊時間", "上次登陸時間", "是否VIP"};
    os = response.getOutputStream();
    // 導出
    ExcelUtil.exportExcel(fileName, list, headers, os, new IExcelExportHandler<User>() {
        
        @Override
        public List<Object> handle(User user) {
            List<Object> rowDatas = new ArrayList<>();
            rowDatas.add(user.getId());
            rowDatas.add(user.getUsername());
            rowDatas.add(user.getRealName());
            rowDatas.add(user.getMobile());
            rowDatas.add(user.getEmail());
            rowDatas.add(RegisterType.getEnum(user.getRegisterType()).getValue());
            rowDatas.add(DateUtil.format(user.getRegisterTime(), "yyyy-MM-dd HH:mm:ss"));
            rowDatas.add(user.getLastLoginTime() == null ? "" : DateUtil.format(user.getLastLoginTime(), "yyyy-MM-dd HH:mm:ss"));
            rowDatas.add(Boolean.TRUE.equals(user.getVipFlag()) ? "是" : "否");
            return rowDatas;
        }
    });
} catch (IOException e) {
    e.printStackTrace();
} finally {
    ...
}

(6)獲取字典

  • 獲取單個字典:/common/dict。
  • 獲取多個字典:/common/dicts。

(7)上下文信息

  • UserContext:用戶上下文信息。
  • AdminContext:管理員上下文信息。

(8)調度

  • 新增調度任務:繼承 BaseJob。
  • 調度接口
    • 執行調度任務:/console/schedule_job/run。
    • 修改調度狀態:/console/schedule_job/update_status。
    • 修改調度頻率:/console/schedule_job/update_cron。

(9)註解

  • @Lock:分佈式鎖。
  • @Perm:標註須要管理員權限的接口。

(10)配置文件

  • 因爲我的習慣,項目中使用的是 properties 配置文件,對於習慣使用 yml 配置文件的,請手動修改。
  • 若是要加載自定義的 yml 配置文件,能夠使用項目中提供的 YAML 屬性源工廠類:YamlPropertySourceFactory。

代碼示例:json

@Component
@ConfigurationProperties(prefix="test")
@PropertySource(value="classpath:config/test.yml", encoding="UTF-8", factory=YamlPropertySourceFactory.class)
public class TestYmlConfig {
    ...
}

(11)屬性名後綴匹配查詢條件

BaseService 中以 JSON 對象爲參數的方法,可經過在屬性名後面添加後綴,來匹配查詢條件。後端

  • xxx_begin:大於等於。
  • xxx_end:小於等於。
  • xxx_in:IN 查詢。
  • xxx_like:模糊查詢。
  • xxx_llike:左模糊查詢。
  • xxx_rlike:右模糊查詢。
  • 其餘:等於。
  • 屬性值爲空:不參與查詢。

代碼示例:api

@Test
public void test() {
    JSONObject params = new JSONObject();
    params.put("username_like", "test");
    List<User> user = userService.list(params);
    System.out.println(user);
}

@Test
public void test2() {
    JSONObject params = new JSONObject();
    params.put("username_in", Arrays.asList("test"));
    List<User> user = userService.list(params);
    System.out.println(user);
}

(12)...

6、演示圖

一、前臺

二、後臺

結語

目前,Chewing 還只是提供了一些較爲經常使用的功能(實在是肝不動了)。可是,後續會不斷完善、新增功能。有時間的話,也會整一個微服務版的。緩存

敬請期待~

交流區


微信公衆號:驚卻一目
我的博客:驚卻一目

相關文章
相關標籤/搜索