انتقل إلى المحتوى
أمين أبوزيد
JNR-026

Spring JDBC

  • 5 درس
  • 2.5 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • JdbcTemplate
  • ربط المعاملات (Parameter binding)
  • تحويل نتائج الاستعلام

تعلّم أولًا

2.5 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Configure a DataSource through Spring Boot properties.
  2. Query and update with JdbcClient/JdbcTemplate and named parameters.
  3. Map rows to records with RowMapper or automatic mapping.
  4. Explain how Spring translates SQLExceptions into its DataAccessException hierarchy.

الدروس

  1. 01إعداد DataSource في Boot
  2. 02JdbcClient وJdbcTemplate
  3. 03تحويل الصفوف (Mapping rows)
  4. 04ترجمة الاستثناءات
  5. 05عرض عملي: استبدال المستودع في الذاكرة بـ Spring JDBC

المختبر والتمارين

Lab

JNR-026-LAB1 · Products on PostgreSQL

Outcomes: 1–4 · Time: ~2.5 h

  1. Add the JDBC starter and driver; configure the datasource in application.yml with credentials from environment variables.
  2. Implement ProductRepository (find by id, list, save, update, delete) with JdbcClient and named parameters, behind the existing interface.
  3. Use one RowMapper shared by all queries.
  4. Translate a duplicate SKU to a domain exception in the repository.
  5. Write an integration test against a real PostgreSQL database.

Acceptance: no controller or service changes needed; integration test passes against a real database.

Exercises (assessment: implement a reporting query repository method)

IDOutcomeTask
JNR-026-EX011Write the application.yml datasource block without a hard-coded password.
JNR-026-EX022Write update and delete methods with named parameters and return whether a row was affected.
JNR-026-EX033Map a product row to a record with a RowMapper, then do it again with query(Product.class).
JNR-026-EX044Name the exception Spring throws for a unique violation and for .single() with no row.
JNR-026-EX052, 3Reporting method salesByCategory() returning record CategoryTotal(String category, BigDecimal total) from a group by query.
JNR-026-EX064Explain why catching DataAccessException in every repository method is a mistake.
أظهر الحلول
  • EX01 url: jdbc:postgresql://localhost:5432/shop, username: ${DB_USER}, password: ${DB_PASSWORD}.
  • EX02 int n = jdbc.sql("update product set price = :p where id = :id").param("p", p).param("id", id).update(); return n == 1;
  • EX03 (rs, i) -> new Product(rs.getLong("id"), rs.getString("name"), rs.getBigDecimal("price")) versus jdbc.sql(sql).query(Product.class).list().
  • EX04 DuplicateKeyException (a DataIntegrityViolationException); EmptyResultDataAccessException.
  • EX05 select c.name as category, sum(p.price) as total from product p join category c on c.id = p.category_id group by c.name mapped with .query(CategoryTotal.class).list().
  • EX06 It hides real failures, repeats code, and leaves nobody able to decide; catch a specific subclass where you can act and let the rest reach the global handler.

الاختبار

Outcomes: O1 DataSource configuration · O2 JdbcClient/JdbcTemplate · O3 row mapping · O4 exception translation.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Boot's default connection pool is:O1
  2. 2Where should the database password come from?O1
  3. 3Named parameters look like:O2
  4. 4JdbcClient.update() returns:O2
  5. 5Spring's JDBC helpers take care of:O2
  6. 6A RowMapper maps:O3
  7. 7Where should ResultSet handling live?O3
  8. 8A unique-key violation becomes:O4
  9. 9DataAccessException is:O4
  10. 10Best practice for handling it:O4

الجزء العملي

Six exercises JNR-026-EX01 to EX06 plus the lab; the assessment is implementing a reporting query repository method (EX05). Pass: at least five exercises correct and the lab integration test green.

أخطاء شائعة

Leaking ResultSet handling into services; catching DataAccessException everywhere.

التقييم

Implement a reporting query repository method.

الصلة بالمسار الوظيفي

JDBC remains the right choice for reporting and bulk work (MID-022).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.