2 Parameters
2.1 简单参数
Parameters are very powerful elements in MyBatis. For simple situations, probably 90% of the cases, there's not much to them。
Parameters 是 MyBatis 中非常强大的元素。在简单使用中,90%的情况下参数都很少。
select id, username, password
from users
where id = #{id}
The example above demonstrates a very simple named parameter mapping.The parameterType is set to int, so therefore the parameter could be named anything.
上面的例子描述了一个非常简单的命名参数映射。parameterType 被设置为 int,因此参数可以随意命名。
Primitive or simple data types such as Integer and String have no relevant properties, and thus will replace the full value of the parameter entirely. However, if you pass in a complex object, then the behavior is a little different.
原生类型或简单的数据类型,比如 Integer 和 String,是没有关联属性的,因此它会完全用参数值来替代。但是,如果你传入的是一个复杂的对象,那么情况就不太一样。
insert into users (id, username, password)
values (#{id}, #{username}, #{password})
If a parameter object of type User was passed into that statement, the id, username and password property would be looked up and their values passed to a PreparedStatement parameter.
如果一个 User 类型的对象参数被传入这个语句,那么 id、username 和 password 属性会被查