Appearance
导入规范
📦 包管理:统一的包导入规范和常用导入清单
📋 目录
导入顺序规范
标准导入顺序
java
// 1. Java标准库 (java.*)
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import java.util.Calendar;
import java.sql.Timestamp;
import java.math.BigDecimal;
// 2. javax包
import javax.validation.Valid;
// 3. 第三方框架库 (org.*, com.*)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
import com.mybatisflex.annotation.UseDataSource;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import lombok.Data;
// 4. 项目内部包 (jpwise.*)
import jpwise.base.ActionResult;
import jpwise.base.controller.BasicDemoController;
import jpwise.base.service.impl.BasicDemoServiceImpl;
import jpwise.base.dao.BasicMapper;
import jpwise.model.base.BaseEntity;
// 5. 工具类包
import jpwise.util.JsonUtil;
import jpwise.util.StringUtil;
import jpwise.util.DateUtil;
import jpwise.util.RandomUtil;
import jpwise.util.UserProvider;
import jpwise.util.file.FileUtil;
import jpwise.util.file.UploadUtil;
// 6. 常量和枚举
import jpwise.constant.MsgCode;
import jpwise.database.constant.DbNameConst;
// 7. 异常类
import jpwise.exception.BusinessException;
import jpwise.exception.DataException;
// 8. 静态导入 (import static)
import static jpwise.constant.Constants.*;
// 9. 功能相关导入(钩子函数、工作流等)
import jpwise.engine.model.flowengine.FlowModel;
import jpwise.model.flow.FlowTaskEntity;
import jpwise.base.enums.FlowRecordEnum;
import jpwise.message.service.MessageService;
导入原则
1. 避免通配符导入
java
// ❌ 错误:使用通配符
import java.util.*;
import org.springframework.web.bind.annotation.*;
import jpwise.util.*;
// ✅ 正确:明确导入
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import jpwise.util.JsonUtil;
import jpwise.util.StringUtil;
2. 只导入实际使用的类
java
// ✅ 正确:只导入需要的类
import jpwise.util.JsonUtil; // 实际使用
import jpwise.util.DateUtil; // 实际使用
// ❌ 错误:导入未使用的类
import jpwise.util.RandomUtil; // 如果代码中没有使用
3. 使用完整包路径
java
// ✅ 正确:使用完整路径避免歧义
import java.util.Date;
import java.sql.Date; // 如果同时需要两个Date类
// 在代码中明确使用
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
常用导入清单
Controller层标准导入
java
// Spring Web相关
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
// 验证相关
import javax.validation.Valid;
// 项目基础
import jpwise.base.ActionResult;
import jpwise.annotation.BuildQuery;
import jpwise.base.query.mode.QueryBuilder;
import jpwise.model.base.PaginationVO;
import jpwise.model.base.PageListVO;
// 工具类
import jpwise.util.JsonUtil;
import jpwise.constant.MsgCode;
// 实体类
import jpwise.base.entity.YourEntity; // 替换为具体实体类
Service层标准导入
java
// Spring相关
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
// MyBatis-Flex相关
import com.mybatisflex.annotation.UseDataSource;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.paginate.Page;
// 数据源
import jpwise.database.constant.DbNameConst;
// 工具类
import jpwise.util.UserProvider;
import jpwise.util.DateUtil;
import jpwise.util.RandomUtil;
import jpwise.util.StringUtil;
import jpwise.util.JsonUtil;
// 异常处理
import jpwise.exception.BusinessException;
import jpwise.exception.DataException;
// 日志
import lombok.extern.slf4j.Slf4j;
// 集合操作(如需要)
import java.util.function.Consumer;
Mapper层标准导入
java
// MyBatis相关
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;
// 项目基础
import jpwise.base.dao.BasicMapper;
// MyBatis-Flex
import com.mybatisflex.core.BaseMapper;
// 实体类
import jpwise.base.entity.YourEntity; // 替换为具体实体类
模块特定导入
Basic继承体系开发导入
java
// Basic继承相关
import jpwise.base.controller.BasicDemoController;
import jpwise.base.service.BasicDemoService;
import jpwise.base.service.impl.BasicDemoServiceImpl;
import jpwise.base.dao.BasicMapper;
// 钩子方法相关(按需导入)
import jpwise.engine.model.flowengine.FlowModel;
import jpwise.model.flow.FlowTaskEntity;
import jpwise.base.enums.FlowRecordEnum;
import jpwise.message.service.MessageService;
// 子表处理相关
import jpwise.annotation.SubEntity;
// JSON处理
import com.fasterxml.jackson.annotation.JsonProperty;
纯手工开发导入
java
// 完整的手工开发导入
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.paginate.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Service;
import jpwise.base.ActionResult;
import jpwise.annotation.BuildQuery;
import jpwise.base.query.mode.QueryBuilder;
避免的导入
错误的包路径
java
// ❌ 常见错误的导入路径
// 错误:MessageService位置错误
import jpwise.system.service.MessageService;
// 正确:
import jpwise.message.service.MessageService;
// 错误:FileUtil位置错误
import jpwise.util.FileUtil;
// 正确:
import jpwise.util.file.FileUtil;
// 错误:TreeUtil位置错误
import jpwise.base.TreeUtil;
// 正确:
import jpwise.util.treeutil.TreeUtil;
// 正确:
import jpwise.engine.model.flowengine.FlowModel;
import jpwise.model.flow.FlowTaskEntity;
import jpwise.base.enums.FlowRecordEnum;
不推荐的导入方式
java
// ❌ 不推荐:使用@Resource
import javax.annotation.Resource;
// ✅ 推荐:统一使用@Autowired
import org.springframework.beans.factory.annotation.Autowired;
// ❌ 不推荐:导入具体的日志实现
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// ✅ 推荐:使用Lombok日志注解
import lombok.extern.slf4j.Slf4j;
IDE配置
IDEA导入设置
java
// File → Settings → Editor → Code Style → Java → Imports
// 导入设置:
Class count to use import with '*': 999 // 避免通配符导入
Names count to use static import with '*': 999 // 避免静态通配符导入
// 导入顺序设置:
java.*
javax.*
org.*
com.*
jpwise.*
static imports
自动导入配置
java
// Settings → Editor → General → Auto Import
☑ Add unambiguous imports on the fly // 自动导入无歧义的类
☑ Optimize imports on the fly // 自动优化导入
☐ Show import popup // 显示导入弹窗(可选)
导入优化快捷键
java
// IDEA快捷键
Ctrl + Alt + O // 优化导入(移除未使用的导入)
Alt + Enter // 快速导入缺失的类
Ctrl + Shift + F12 // 最大化编辑器(隐藏导入区域)
特殊场景导入
工具类子包导入
java
// 文件相关工具类(注意子包路径)
import jpwise.util.file.FileUtil;
import jpwise.util.file.UploadUtil;
import jpwise.util.file.base.StorageType;
// 树形结构工具类
import jpwise.util.treeutil.TreeUtil;
// 数据库工具类
import jpwise.database.util.JdbcUtil;
import jpwise.database.util.ConnUtil;
枚举和常量导入
java
// 文件类型枚举
import jpwise.util.enums.FileTypeEnum;
// 数据库常量
import jpwise.database.constant.DbNameConst;
// 消息码
import jpwise.constant.MsgCode;
// 基础字段常量
import jpwise.constant.BaseFieldConstant;
MyBatis-Flex查询导入
java
// 基础查询
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.paginate.Page;
// 高级查询功能
import java.util.function.Consumer; // 用于OR条件查询
// 数据库操作
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.mybatisflex.core.BaseMapper;
导入检查清单
Controller层检查
- [ ] Spring Web注解导入
- [ ] 项目基础类导入 (ActionResult, BuildQuery)
- [ ] 实体类导入
- [ ] 验证注解导入 (@Valid)
Service层检查
- [ ] Spring注解导入 (@Service, @Autowired)
- [ ] 数据源注解导入 (@UseDataSource)
- [ ] MyBatis-Flex相关导入
- [ ] 工具类导入
- [ ] 异常类导入
通用检查
- [ ] 没有通配符导入 (import xxx.*)
- [ ] 没有未使用的导入
- [ ] 包路径正确无误
- [ ] 导入顺序符合规范
常见导入错误和解决方案
1. 类名冲突
java
// 问题:Date类冲突
import java.util.Date;
import java.sql.Date; // 冲突
// 解决方案1:使用完整路径
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
// 解决方案2:重命名导入(不推荐)
import java.sql.Date as SqlDate; // Java不支持,只是示例
2. 工具类路径错误
java
// 查看工具类的实际位置
// 方法:在IDE中Ctrl+Click进入工具类源码,查看package声明
// FileUtil实际路径
package jpwise.util.file; // 所以导入应该是:
import jpwise.util.file.FileUtil;
3. 自动导入错误
java
// 问题:IDE自动导入了错误的类
import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
// 解决:删除错误导入,手动添加正确导入
import jpwise.util.MessageFormatter; // 假设有这个类
💡 提示:良好的导入规范是代码整洁的第一步。使用IDE的自动导入功能,但要注意检查导入的正确性。定期使用"优化导入"功能清理未使用的导入。