/java-skills:mapstruct-patternsfrom java-skills
Provides MapStruct best practices from the official Reference Guide, covering mapper definition styles, Spring injection strategies, nested mapping patterns, @Qualifier vs @Named, Lombok integration, and common LLM mistakes.
Ships in java-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
来源: MapStruct Reference Guide | GitHub: mapstruct/mapstruct
// ✅ 推荐:CONSTRUCTOR 注入(测试友好,不依赖Spring容器)
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.CONSTRUCTOR)
public interface UserMapper { UserVO toVO(User e); }
// ✅ 循环依赖:SETTER注入
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.SETTER,
uses = { OrderMapper.class })
public interface UserMapper { ... }
// ✅ 全局默认配置(避免每个接口重复)
@MapperConfig(componentModel = "spring", injectionStrategy = InjectionStrategy.CONSTRUCTOR)
public interface CentralConfig {}
@Mapper(config = CentralConfig.class) public interface UserMapper { ... }
// ❌ 反模式:全写在顶层(重复配置,难以维护)
@Mapping(target = "address.street", source = "home.street")
@Mapping(target = "address.city", source = "home.city")
UserDTO toDTO(User e);
// ✅ 模式:显式子方法(单一配置点,可复用)
@Mapper
public interface UserMapper {
UserDTO toDTO(User e); // MapStruct 自动检测嵌套类型
AddressDTO toAddressDTO(Address e); // 子映射方法 — 唯一配置处
}
// ✅ dot notation + 子方法组合(局部覆盖)
@Mapping(target = "address.zip", constant = "00000") // 常量覆盖
@Mapping(target = "address", source = "homeAddr") // 其余走子方法
UserDTO toDTO(User e);
// ✅ 推荐:自定义 @Qualifier(IDE重构安全)
@Qualifier @Target(ElementType.METHOD) @Retention(RetentionPolicy.CLASS)
public @interface ToUpperCase {}
@ToUpperCase default String toUpper(String s) { return s.toUpperCase(); }
@Mapping(target = "name", qualifiedBy = ToUpperCase.class)
UserDTO toDTO(User e);
// ⚠️ 可用但不推荐:@Named(字符串依赖,IDE不能自动重命名)
@Named("toUpper") default String toUpper(String s) { return s.toUpperCase(); }
@Mapping(target = "name", qualifiedByName = "toUpper") // 字符串引用
// 场景: UserMapper → OrderMapper → UserMapper
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.SETTER,
uses = { OrderMapper.class }) // ← SETTER是关键
public interface UserMapper {
@Mapping(target = "orders", source = "orderList")
UserDTO toDTO(User entity);
}
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.SETTER,
uses = { UserMapper.class })
public interface OrderMapper { ... }
@Mapper
public abstract class UserMapper {
@Autowired protected PasswordEncoder encoder; // 可注入字段
@Mapping(target = "encodedPwd", expression = "java(encoder.encode(dto.getPassword()))")
public abstract UserVO toVO(UserDTO dto);
protected String formatDate(LocalDateTime dt) { return dt.format(FORMATTER); } // 自定义方法
}
本技能不收集、存储或传输任何用户数据。
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Generated from the plugin's indexed repository (full-stack-skills/java-skills) — no native marketplace selected
npx claudepluginhub p/full-statck-skills-java-skillsOther plugins' skills for the same job
39 skills in this plugin
/caffeine-patternsCaffeine JVM 本地缓存技能。覆盖本地缓存 vs 远程缓存(Redis)选择决策树、expireAfterWrite vs expireAfterAccess vs refreshAfterWrite 三种过期策略对比与组合规则(refresh < expire)、CacheLoader加载与reload异步刷新模式、Spring Cache @Cacheable集成、maximumSize驱逐策略、recordStats缓存统计。 纠正 LLM:用 ConcurrentHashMap+手动过期、所有缓存走Redis、refreshAfterWrite > expireAfterWrite导致永远不刷新、CacheLoader返回null导致缓存穿透。/commons-patternsApache Commons 最佳实践模式。从官方文档提炼,覆盖 Commons Lang3(StringUtils千面判空/isEmpty vs isBlank/Validate抛NullPointerException迁移)、Commons IO(IOUtils.toString大文件陷阱/FileUtils.lineIterator逐行读模式/FilenameUtils.normalize路径规范化)、Commons Collections4(CollectionUtils.union/intersection/subtract集合运算)。 纠正 LLM 误用:混淆 StringUtils.isBlank vs isEmpty、IOUtils.toString 读大文件OOM、不知道 LineIterator 逐行读模式、用 lang 而非 lang3 包名。/