閒來無事,整一個 Java 項目快速開發腳手架。前端
Chewing 是一個簡單的 Java 項目快速開發腳手架。既適合須要開發小型項目的小夥伴使用,也適合剛入門的新手用來學習一些經常使用的技術。java
源碼地址:https://github.com/jingqueyimu/chewing。git
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 // 調度配置文件
初始化示例: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());" ] } ]
導入示例: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 { ... }
代碼示例:json
@Component @ConfigurationProperties(prefix="test") @PropertySource(value="classpath:config/test.yml", encoding="UTF-8", factory=YamlPropertySourceFactory.class) public class TestYmlConfig { ... }
BaseService 中以 JSON 對象爲參數的方法,可經過在屬性名後面添加後綴,來匹配查詢條件。後端
代碼示例: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); }
目前,Chewing 還只是提供了一些較爲經常使用的功能(實在是肝不動了)。可是,後續會不斷完善、新增功能。有時間的話,也會整一個微服務版的。緩存
敬請期待~
微信公衆號:驚卻一目
我的博客:驚卻一目