mybatis-patterns
Skill này làm gì
Công cụ này giúp tổ chức và tối ưu code truy cập cơ sở dữ liệu bằng công nghệ MyBatis. Nó đảm bảo các truy vấn của bạn an toàn, hiệu quả và dễ hiểu.
Khi nào nên dùng
- ✓Khi viết hoặc xem lại cách chương trình truy cập dữ liệu.
- ✓Khi quyết định giữa các cách viết lệnh cơ sở dữ liệu khác nhau.
- ✓Khi muốn chương trình lấy dữ liệu nhanh hơn.
Liên kết skill
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/mybatis-patterns/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Làm sao để đảm bảo các truy vấn MyBatis của tôi an toàn trước các cuộc tấn công?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Nó phân tích code MyBatis của bạn để tìm các mẫu phổ biến và đề xuất cải tiến. Điều này bao gồm kiểm tra cách dữ liệu được gửi và nhận, và cách giao dịch được quản lý.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
MyBatis Patterns
Use MyBatis when SQL control and explicit mappings matter. Keep SQL readable,
make mapper contracts small, and put business transactions in the service layer.
Pair this skill with springboot-patterns for application structure,
database-migrations for schema changes, postgres-patterns or
mysql-patterns for engine-specific SQL, and security-review for untrusted
input and access-control concerns.
When to Activate
- Adding or reviewing MyBatis mapper interfaces or XML files
- Choosing between XML mappers, annotation mappers, and generated SQL
- Designing
resultMapmappings, joins, nested collections, or projections - Reviewing dynamic SQL, parameter binding, pagination, or batch writes
- Debugging N+1 queries, slow statements, connection use, or transaction bugs
- Integrating MyBatis-Spring into Spring Boot or an XML-first legacy application
How It Works
Use a thin mapper for persistence operations and keep business rules in a service. A typical flow is:
Controller / batch job -> Service (@Transactional) -> Mapper -> Database
- Register mappers once, either with
@Mapperon each interface or with@MapperScanfor a package. Do not mix registration approaches casually. - Keep the XML
namespaceequal to the mapper interface's fully qualified name. - Give each statement a stable, descriptive
id; treat mapper methods as a persistence API that can be tested independently. - Return a domain DTO or projection for read paths instead of exposing a large mutable map when the shape is known.
- Keep schema changes in migrations. Do not make mapper startup silently create or alter production tables.
Examples
@MapperScan("com.example.user.persistence")
@Configuration
class MyBatisConfig {
}
public interface UserMapper {
UserSummary findSummaryById(long userId);
}
<mapper namespace="com.example.user.persistence.UserMapper">
<select id="findSummaryById"
parameterType="long"
resultType="com.example.user.persistence.UserSummary">
SELECT id, display_name AS displayName, status
FROM users
WHERE id = #{userId}
</select>
</mapper>
Parameter Binding and SQL Safety
Use #{...} for values. MyBatis binds it as a prepared-statement parameter.
Treat ${...} as raw SQL text: it is only appropriate for a value selected
from a closed, application-owned allowlist such as a sort-column map.
private static final Map<String, String> SORT_COLUMNS = createSortColumns();
private static Map<String, String> createSortColumns() {
Map<String, String> columns = new HashMap<>();
columns.put("name", "display_name");
columns.put("created", "created_at");
return Collections.unmodifiableMap(columns);
}
String sortColumn = SORT_COLUMNS.getOrDefault(request.sort(), "created_at");
String sortDirection = request.descending() ? "DESC" : "ASC";
// The mapper receives only an allowlisted identifier; user input is never
// passed directly to ${sortColumn} or ${sortDirection}.
<select id="findPage" resultType="com.example.user.persistence.UserSummary">
SELECT id AS id,
display_name AS displayName,
status AS status,
created_at AS createdAt
FROM users
WHERE tenant_id = #{tenantId}
ORDER BY ${sortColumn} ${sortDirection}
LIMIT #{limit}
</select>
Derive tenantId from the authenticated principal or a server-side tenant
context, never from a request field. Authenticate the principal and verify
that it is authorized for the tenant before invoking the mapper; keep the
tenant_id predicate in SQL as defense in depth.
If possible, avoid ${} entirely by selecting among fixed statements or by
using a database-specific query builder. Never interpolate user input into a
table name, column name, ORDER BY, WHERE fragment, or SQL expression.
For multiple parameters, use a request object or explicit @Param names:
List<UserSummary> findActive(
@Param("tenantId") long tenantId,
@Param("statuses") Set<UserStatus> statuses
);
Do not use the deprecated external parameterMap element. Prefer inline
parameter mappings and explicit jdbcType for nullable values when the JDBC
driver needs it.
XML, Result Types, and Result Maps
- Use
resultTypefor a simple one-to-one mapping whose column labels already match the target properties. - Use
resultMapwhen aliases, type handlers, nested objects, or collections need explicit mapping.
…
Câu hỏi thường gặp
MyBatis là gì?
MyBatis là công cụ giúp các chương trình Java giao tiếp với cơ sở dữ liệu, cho phép bạn viết lệnh SQL một cách có tổ chức hơn.
'Mapper' là gì?
Mapper giống như người phiên dịch giữa code của chương trình và lệnh SQL. Chúng xác định dữ liệu nào cần lấy và cách trình bày dữ liệu đó.
Vì sao bảo mật truy vấn quan trọng?
Truy vấn không an toàn có thể cho phép kẻ xấu truy cập hoặc sửa đổi dữ liệu của bạn. Công cụ này giúp ngăn điều đó.
Chi tiết
Mô tả gốc của tác giả
MyBatis and MyBatis-Spring patterns for mapper design, XML and annotation SQL, result mapping, dynamic SQL safety, transactions, batching, pagination, and query performance. Use when building or reviewing Java persistence code with MyBatis, Spring Boot, MyBatis-Spring, or MyBatis-based legacy applications.
repo: affaan-m/ECC
Độ phổ biến
- Sao GitHub
- 280k
- Vị trí xếp hạng
- #214
- Giấy phép
- MIT
- Cập nhật
- 15/10/2026
Bình luận
- …
Thêm skill từ kho mã này
accessibility
280kGiúp agent của bạn tạo giao diện mà ai cũng dùng được, đảm bảo website và app hoạt động tốt với người khuyết tật như người mù hoặc khó vận động.
agent-architecture-audit
280kGiúp agent của bạn kiểm tra xem mọi thứ có hoạt động đúng không, đồng thời tìm và sửa các lỗi trong hệ thống AI để chúng chạy như mong đợi.
agent-eval
280kGiúp agent của bạn so sánh các công cụ lập trình AI khác nhau, đo xem công cụ nào nhanh hơn, rẻ hơn và đáng tin cậy hơn cho từng tác vụ.
agent-harness-construction
280kGiúp agent của bạn hiểu rõ hơn cách thực hiện tác vụ, tối ưu các công cụ và chỉ dẫn mà agent dùng để tăng khả năng thành công.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.