When using annotations to simplify XML configuration, the role of @Param annotation is to name the parameters, and after naming the parameters, you can get the parameter value according to the name, and correctly pass the parameters into the SQL statement
Let's first look at the @Select method in the Mapper interface
Here's an explanation
1.@Select(....) The function of the annotation is to tell the mybatis framework to execute the SQL statement in parentheses
2.s_id id, s_name name, class_id classid format is field name + attribute name, for example s_id is the field name in the database, id is the attribute name in the class
The function of this code is to realize the one-to-one mapping of database field names and entity class attributes, otherwise the database will not know how to match
3.where s_name= #{aaaa} and class_id = #{bbbb} means that the sql statement should accept 2 parameters, one parameter name is aaaa, one parameter name is bbbb, if you want to pass the correct parameters, then you need to name the parameters, because you don't need to use the xml configuration file, then we have to use other ways to name the parameters, this way is @Param annotations
4. Write @Param ("parameter name") in front of the method parameter to indicate the name of the parameter, and the name is the content in parentheses
public Student select(@Param("aaaa") String name,@Param("bbbb")int class_id); Give the parameter String name to aaaa, and then sql statement.... where s_name= #{aaaa} to get the parameter value based on aaaa
|