Hibernate / MyBatis Interview questions
What is parameter binding in MyBatis?
Parameter binding is the process of substituting Java method arguments into placeholders within a SQL statement, using the #{} syntax to reference a parameter by name (or by property, for object parameters), which MyBatis translates into a safely parameterized JDBC PreparedStatement.
<insert id="insertUser" parameterType="com.example.model.User"> INSERT INTO users (name, email, status) VALUES (#{name}, #{email}, #{status}) </insert>
When the parameter is a single simple value (like an int or String), #{} can use any name inside the braces as a convention; when the parameter is a Java object (as in the example above), the name inside #{} must match a property (getter/setter pair) on that object, which MyBatis uses reflection to read at execution time.
For methods taking multiple separate parameters, either @Param annotations on the interface method or a Map parameter are used to give each value a name that the corresponding #{} placeholders in the SQL can reference unambiguously, since Java doesn't preserve parameter names in bytecode by default without additional compiler configuration.
More Related questions...