1.使用@Param注解:在Mapper接口的方法中使用@Param注解,可以未参数去一个名字,方便在SQL语句中引用。
@Select("SELECT * FROM user WHERE id = #{id}")
User getUserById(@Param("id") int userId);
2.使用Map类型参数:在Mapper接口的方法参数列表中使用Map类型参数,可以将所有参数封装到Map中,通过键值对的方式传递参数。
@Select("SELECT * FROM user WHERE id = #{id} AND name = #{name}")
User getUserByIdAndName(Map<String,Object> paramMap);
3.使用POJO类型参数:在Mapper接口的方法参数列表中使用自定义的POJO类型参数,可以将参数封装到一个对象中,通过对象的属性名来引用参数。
@Insert("INSERT INTO user (id,name,age) VALUES (#{id},#{name},#{age})")
int addUser(User user);
4.使用数组类型参数:在Mapper接口的方法参数列表中使用数组类型参数,可以将多个参数封装到一个数组中,通过数组下标的方式来引用参数。
@Select("SELECT * FROM user WHERE id IN (#{ida})")
List<User> getUsersByIda(@Param("ida") int[] ida);
5.使用List类型参数:在Mapper接口的方法参数列表中使用List类型参数,可以将多个参数封装到一个List中,通过List中元来引用参数。
@Select("SELECT * FROM user WHERE id IN (#{ida})")
List<User> getUsersByIda(List<Integer> ida);