JDBC Mapping Without Reflection: Up to 117 Faster Than Spring JDBC
SQL execution isn't the only cost of a database query. Turning a ResultSet into Java objects costs time and memory, too. What if we generated the mapping code at compile time instead? That's what SJF4J does with @CompiledJdbcMapper . A simple example Define a Java record: public record User ( long id , String userName , int age ) { } Declare a mapper: @CompiledJdbcMapper public interface…
Mapping database results to Java objects can be costly both in terms of time and memory. SJF4J offers a solution by generating mapping code at compile time with the @CompiledJdbcMapper annotation. To use it, define a Java record and declare a mapper interface. The mapper can be accessed via the INSTANCE field, and the users method takes a ResultSet as input.
Consider a benchmark comparing SJF4J against Spring JDBC and MyBatis using a 12-column result set with 1,000 rows. The results show that SJF4J is up to 117 times faster than Spring JDBC and nearly 118 times faster than MyBatis when mapping Java records. SJF4J also allocates 50 times less memory than Spring's DataClassRowMapper.
SJF4J generates specialized Java code at compile time, eliminating the need for runtime object mapping. This results in faster column index resolution, typed JDBC getters, and direct object creation. The generated code closely resembles handwritten JDBC mapping, outperforming general-purpose reflective mappers.
For those who prefer to control the row loop, SJF4J also supports prepared row mapping. An interface with a prepare method takes a ResultSetMetaData and can be used to map individual rows as needed. SJF4J does not replace SQL frameworks or manage database connections; it simply accelerates the ResultSet-to-object conversion process, allowing developers to keep their SQL and JDBC code intact.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to