/java-skills:mybatis-patternsfrom java-skills
Guides MyBatis ORM development: XML vs annotation SQL, ResultMap reuse with extends, association/collection join vs nested select (N+1), pagination plugin config, SQL injection prevention with # vs $, dynamic SQL best practices, and columnPrefix for same-table multi-JOIN.
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.
来源:https://mybatis.org/mybatis-3/
GitHub:https://github.com/mybatis/mybatis-3
| # | 错误 | 正确做法 |
|---|---|---|
| 1 | 简单CRUD也用XML写 | @Select("SELECT * FROM user WHERE id=#{id}") 注解即可 |
| 2 | N+1查询:循环查子表 | Join查询(推荐) 或 association/collection + batch select |
| 3 | ${column} 动态排序无白名单 | 先检查 columnName 是否在白名单中,否则 SQL 注入 |
| 4 | 不配置 resultMap 用别名映射 | 定义 <resultMap> + <result column="user_name" property="userName"/> |
| 5 | SELECT * 全部字段 | 只查需要的字段(尤其大字段/TEXT/BLOB) |
| 6 | 模糊查询 LIKE '%${keyword}%' SQL注入 | LIKE CONCAT('%', #{keyword}, '%') 预编译 |
| 7 | 多表 JOIN 不处理列名冲突 | 用别名 + resultMap column 映射 |
| 8 | 不同表 JOIN 同表两次,手动写所有列 | 用 <association columnPrefix="addr_" 复用 resultMap |
<!-- 基础 ResultMap -->
<resultMap id="BaseResultMap" type="com.example.User">
<id column="id" property="id"/>
<result column="user_name" property="userName"/>
<result column="email" property="email"/>
<result column="status" property="status"/>
</resultMap>
<!-- 继承 + 关联(推荐 Join 方式) -->
<resultMap id="UserWithDept" extends="BaseResultMap" type="com.example.UserVO">
<association property="dept" javaType="Department"
resultMap="com.example.DeptMapper.BaseResultMap"
columnPrefix="dept_"/> <!-- ← 同名字段加前缀 -->
<collection property="roles" ofType="Role"
resultMap="com.example.RoleMapper.BaseResultMap"
columnPrefix="role_"/>
</resultMap>
<!-- ✅ 推荐:Join 查询 → 一次SQL,无N+1 -->
<select id="selectUserWithDept" resultMap="UserWithDept">
SELECT u.id, u.user_name, u.email, u.status,
d.id AS dept_id, d.dept_name AS dept_dept_name, <!-- ← 加前缀 -->
r.id AS role_id, r.role_name AS role_role_name
FROM sys_user u
LEFT JOIN sys_dept d ON u.dept_id = d.id
LEFT JOIN sys_user_role ur ON u.id = ur.user_id
LEFT JOIN sys_role r ON ur.role_id = r.id
WHERE u.id = #{id}
</select>
<!-- ❌ 避免:嵌套 select 导致 N+1 -->
<resultMap id="UserWithDeptNested" extends="BaseResultMap" type="com.example.UserVO">
<association property="dept" javaType="Department"
column="dept_id" select="selectDeptById"/> <!-- ← 每次查询一个部门 -->
</resultMap>
<!-- 如果查100个用户 → 1(主查询) + 100(部门查询) = 101次SQL -->
<select id="findByCondition" resultMap="BaseResultMap">
SELECT id, user_name, email, status, create_time
FROM sys_user
<where> <!-- ← 自动处理 AND/OR 前缀 -->
<if test="username != null and username != ''">
AND user_name LIKE CONCAT('%', #{username}, '%') <!-- ✅ 防注入 -->
</if>
<if test="status != null">
AND status = #{status}
</if>
<if test="deptId != null">
AND dept_id = #{deptId}
</if>
</where>
<if test="orderBy != null">
ORDER BY ${orderBy} <!-- ⚠️ 必须白名单校验 -->
</if>
</select>
<!-- 批量插入 -->
<insert id="batchInsert">
INSERT INTO sys_user (user_name, email, status)
VALUES
<foreach collection="list" item="user" separator=",">
(#{user.userName}, #{user.email}, #{user.status})
</foreach>
</insert>
<!-- 动态更新(set 自动去尾逗号) -->
<update id="updateSelective">
UPDATE sys_user
<set>
<if test="userName != null">user_name = #{userName},</if>
<if test="email != null">email = #{email},</if>
<if test="status != null">status = #{status},</if>
</set>
WHERE id = #{id}
</update>
<!-- choose/when/otherwise 多分支 -->
<select id="findByCondition" resultMap="BaseResultMap">
SELECT * FROM sys_user
<where>
<choose>
<when test="queryType == 'name'">AND user_name LIKE CONCAT('%', #{keyword}, '%')</when>
<when test="queryType == 'email'">AND email = #{keyword}</when>
<otherwise>AND status = 'ACTIVE'</otherwise>
</choose>
</where>
</select>
<sql id="BaseColumns">
id, user_name, email, status, create_time, update_time
</sql>
<select id="selectById" resultMap="BaseResultMap">
SELECT <include refid="BaseColumns"/>
FROM sys_user WHERE id = #{id}
</select>
@Select("SELECT * FROM sys_user WHERE id = #{id}")
@Results(id = "userMap", value = {
@Result(column = "user_name", property = "userName"),
@Result(column = "dept_id", property = "deptId")
})
User findById(Long id);
// ✅ 安全:校验排序字段在白名单中
private static final Set<String> SORT_COLUMNS = Set.of(
"create_time", "update_time", "user_name", "status"
);
public List<User> findByCondition(String orderBy, boolean asc) {
if (!SORT_COLUMNS.contains(orderBy)) {
throw new SecurityException("非法排序字段: " + orderBy);
}
// 传入 ${orderBy} 安全使用
}
#{} 会加引号自动转义,${} 直接拼接 — 用户输入必须用 #{}${} 用于动态表名/列名/排序时 — 必须白名单校验,否则 SQL 注入mybatis.configuration.map-underscore-to-camel-case=trueWHERE 1=1 不如 <where> — <where> 自动处理 AND/OR 前缀,更优雅<foreach> 注意 IN 列表为空 — 空列表导致 SQL 语法错误,提前判空本技能不收集、存储或传输任何用户数据。
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
mybatis, orm, sql-mapping, resultmap, dynamic-sql, n-plus-one
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 包名。/