MyBatis Generator 生成的 Mapper 介面通常包含以下標準方法(以 User 為例):
| 方法 | 用途 | 對應 SQL 示例 |
|---|---|---|
countByExample(example) | 條件計數 | SELECT COUNT(*) FROM user WHERE username='joe' |
deleteByExample(example) | 條件批量刪除 | DELETE FROM user WHERE username='joe' |
deleteByPrimaryKey(id) | 主鍵單條刪除 | DELETE FROM user WHERE id=101 |
insert(record) | 全字段插入 | INSERT INTO user(id,username,password,email) VALUES(...) |
insertSelective(record) | 非空字段插入 | 僅插入非 null 字段 |
selectByExample(example) | 條件查詢列表 | SELECT * FROM user WHERE username='joe' ORDER BY username |
selectByPrimaryKey(id) | 主鍵查詢單條 | SELECT * FROM user WHERE id=101 |
updateByExampleSelective(record, example) | 條件更新非空字段 | UPDATE user SET password='123' WHERE username='joe' |
updateByExample(record, example) | 條件更新全字段 | 全字段覆蓋 |
updateByPrimaryKeySelective(record) | 主鍵更新非空字段 | UPDATE user SET password='joe' WHERE id=101 |
updateByPrimaryKey(record) | 主鍵更新全字段 | UPDATE user SET username='joe',password='joe' WHERE id=101 |
注意:Example 類內部的 Criteria 用於構建 WHERE 條件,支援 andXxxEqualTo、andXxxIsNull、orXxxLike 等豐富條件組合。
<mapper namespace="com.example.mapper.UserMapper">
<!-- 緩存配置 -->
<cache eviction="LRU" flushInterval="60000" size="512" readOnly="true"/>
<!-- 結果映射:將查詢列映射到屬性 -->
<resultMap id="userResultMap" type="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="email" column="email"/>
<!-- 關聯對象映射 -->
<association property="profile" javaType="Profile">
<id property="id" column="profile_id"/>
<result property="nickname" column="nickname"/>
</association>
<!-- 集合映射 (一對多) -->
<collection property="articles" ofType="Article">
<id property="id" column="article_id"/>
<result property="title" column="title"/>
</collection>
</resultMap>
<!-- 可重用 SQL 片段 -->
<sql id="userColumns">
id, username, password, email, create_time
</sql>
<select id="selectById" parameterType="int" resultMap="userResultMap">
SELECT <include refid="userColumns"/> FROM user WHERE id = #{id}
</select>
<insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user(username, password, email) VALUES(#{username}, #{password}, #{email})
</insert>
<update id="updateByPrimaryKeySelective" parameterType="User">
UPDATE user
<set>
<if test="username != null">username = #{username},</if>
<if test="password != null">password = #{password},</if>
</set>
WHERE id = #{id}
</update>
<delete id="deleteByPrimaryKey" parameterType="int">
DELETE FROM user WHERE id = #{id}
</delete>
</mapper>
mapperLocations 表達式匹配的所有 XML 資源。XMLMapperBuilder,調用 parse()。parse() → configurationElement() 依次解析子節點:
cache-ref / cache → 緩存配置parameterMap / resultMap → 參數/結果映射sql → SQL 片段註冊select|insert|update|delete → 交由 XMLStatementBuilder 解析為 MappedStatementMappedStatement 被註冊到 Configuration 的 mappedStatements Map 中,Key 為 namespace + "." + id。SqlSession 根據 Key 獲取 MappedStatement,由 Executor 執行。XMLMapperBuilder.configurationElement(XNode context):總解析入口。XMLStatementBuilder.parseStatementNode():單條 SQL 節點解析,構建 MappedStatement.Builder。MapperBuilderAssistant.addMappedStatement(...):最終註冊到 Configuration。resultMap 而非 resultType,支援嵌套映射、列名與屬性名不一致、類型轉換。<if>、<where>、<set>、<foreach>)構建靈活查詢/更新。useGeneratedKeys="true" keyProperty="id" 獲取自增主鍵回填。fetchSize、resultSetType="FORWARD_ONLY"、cursor 流式處理。深入理解 Mapper XML 解析流程與 MappedStatement 註冊機制,有助於排查映射錯誤、優化複雜查詢、擴展自定義語句處理。掌握內置方法語義與動態 SQL 技巧,是高效使用 MyBatis 的關鍵。