6.6 KiB
6.6 KiB
Database Guidelines
MySQL data and DAO conventions for the library-management system.
Overview
MySQL is the project data layer. DAO classes perform CRUD and query operations
against MySQL. The initial scaffold schema exists at
src/main/resources/db/schema.sql; future module tables should follow the same
DDL style and DAO boundaries.
Core Tables
Use primary keys for every table and foreign keys for cross-entity integrity.
Implemented scaffold tables:
roles: administrator, librarian, reader, and future role definitions.permissions: permission definitions for protected actions.role_permissions: role-to-permission mapping.users: login accounts for administrator, librarian, and reader roles.system_logs: key operation logs, backup events, and exception traces.
Planned module tables:
books: book information, inventory count/status, category reference.book_categories: category names and descriptions.readers: reader profiles, borrowing eligibility, contact information.borrow_records: book-reader borrowing, return, renew, and overdue data.system_logs: key operation logs, backup events, and exception traces.
Record new schema changes in src/main/resources/db/schema.sql and update this
spec with exact table names, key columns, and DAO/service contracts.
DAO Responsibilities
- DAOs own database CRUD and query details.
- Use parameterized SQL or prepared-statement style access; never concatenate raw request parameters into SQL.
- Keep transaction boundaries in the service layer for workflows that span multiple DAO calls, such as borrow/return operations that also update inventory status.
- Return entities or small query result objects to services, not HTML or servlet response objects.
- Keep MySQL connection details in
src/main/resources/db.propertiesloaded byJdbcUtil. The required keys aredb.driver,db.url,db.username, anddb.password.
Query Guidance
- Book search must support combined lookup by title, author, category, and ID.
- Statistics queries should cover borrowing rankings, inventory reports, and overdue reports.
- Borrowing records should preserve enough dates/status fields for borrow, return, renew, overdue calculation, and automatic collection status updates.
- Permission queries should support role-based checks for administrator, librarian, and reader workflows.
Integrity Constraints
books.category_idshould referencebook_categories.borrow_records.book_idshould referencebooks.borrow_records.reader_idshould referencereaders.- Administrator-role and role-permission mapping tables should use foreign keys to preserve authorization integrity.
users.role_codemust referenceroles.code.role_permissions.role_codemust referenceroles.code.role_permissions.permission_codemust referencepermissions.code.- Prefer explicit status columns/enums for inventory and borrowing states, then document the chosen values once code exists.
Scenario: Login And Permission Scaffold Schema
1. Scope / Trigger
- Trigger: the initial Java Web scaffold introduced a concrete MySQL schema and login contract.
- Schema path:
src/main/resources/db/schema.sql. - Example configuration path:
src/main/resources/db.properties.example.
2. Signatures
- DAO signature:
UserDao.findActiveByUsername(String username). - Service signature:
AuthService.authenticate(String username, String password). - Permission signature:
AuthService.hasPermission(AuthenticatedUser user, Permission permission). - Servlet route:
POST /loginwithusername,password, and optional same-applicationredirect. - Protected routes:
/dashboard,/admin/home,/librarian/home, and/reader/home. - Session keys:
authenticatedUser,userRole, anduserPermissions. - DB config keys:
db.driver,db.url,db.username, anddb.password. - Login tables:
roles,permissions,role_permissions,users, andsystem_logs.
3. Contracts
users.username: unique login identifier submitted byLoginServlet.users.password_hash: PBKDF2 hash inpbkdf2_sha256$iterations$saltBase64$hashBase64format.users.role_code: foreign key toroles.code; supported scaffold values areadministrator,librarian, andreader.users.active: only rows withactive = 1can authenticate.roles.code,permissions.code, androle_permissionsmust match the JavaRoleandPermissionenum codes exactly.db.propertiesmust be local configuration. Commitdb.properties.example, but do not commit real credentials.- Session state stores an
AuthenticatedUsersnapshot, role code, and permission-code set. It must not store raw passwords or DAO result objects with password hashes. - Login redirects must stay inside the current application context. Reject
values that do not start with a single
/or that contain CR/LF characters.
4. Validation & Error Matrix
- Missing username or password -> request returns to login JSP with
Username and password are required. - Unknown user, inactive user, or hash mismatch -> request returns to login JSP
with
Invalid username or password. - Unsafe or blank redirect -> ignore and route to
/dashboardafter success. - Missing
db.properties, JDBC failure, or unsupported role code -> request returns a generic service-unavailable message and logs server-side details. - Authenticated user missing a required permission -> HTTP 403 and
WEB-INF/jsp/auth/unauthorized.jsp.
5. Good/Base/Bad Cases
- Good:
adminresolves toadministrator, receives all scaffold permissions, and can access/admin/home. - Base:
readerresolves toreader, can access/reader/home, and cannot access/admin/home. - Bad: a JSP reads SQL or password hashes directly from the database. Keep that logic in DAO/service code.
6. Tests Required
- Compile service/DAO/entity/util classes with
javacwhen Maven is unavailable. - Run
PermissionPolicyCheckor equivalent assertions for administrator, librarian, and reader permissions. - Run
AuthServiceCheckor equivalent assertions for required-field failures, invalid credentials, success, permission checks, and DAO failure fallback. - When Maven/Tomcat dependencies are installed, run
mvn testormvn clean packageto compile Servlet and JSP integration.
7. Wrong vs Correct
Wrong
// JSP, Servlet, or session code opens JDBC and stores password_hash.
Correct
login.jsp -> LoginServlet -> AuthService -> UserDao -> users/roles tables
session -> AuthenticatedUser snapshot + role/permission codes only