请查看最新整理地址:http://leeyee.github.io/blog/2012/09/27/mybatis-param-mapping-rules
最近在*Mybatis*的学习中对于映射语句中的参数注释设置有点犯迷糊,于是在*debug*下跟踪了下源代码,发现*Mybatis*在接口方法映射语句中会做如下处理:
1. 接口方法只有一个参数
1.1 不使用`@Param`注解
1.1.1 参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{任意字符}
1.1.2 参数为自定义对象
参数注释为: #{对象属性}
示例:
User getUserById(int id);
// 注:该中情况下'任意字符'不能为空,否则报错
// select * from <TABEL> where id = #{任意字符}
select * from <TABLE> where id = #{id}
User getUser(User user); // user.getName user.getAge
select * from <TABLE> where name = #{name} and age = #{age}
1.2 使用`@Param`注解
1.2.1 参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{注解名称} | #{param1}
1.2.2 参数为自定义对象
参数注释为: #{注解名称.对象属性} | #{param1.对象属性}
示例:
User getUserById(@Param(value="keyId") int id);
select * from <TABEL> where id = #{keyId}
// or
select * from <TABLE> where id = #{param1}
User getUser(@Param(value="usr") User user); // user.getName user.getAge
select * from <TABLE> where name = #{user.name} and age = #{user.age}
// or
select * from <TABLE> where name = #{param1.name} and age = #{param1.age}
2. 接口方法有两个及两个以上参数
2.1 不使用`@Param`注解
2.1.1 参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{参数位置[0..n-1]} | #{param[1..n]}
2.1.2 参数为自定义对象
参数注释为: #{参数位置[0..n-1].对象属性} | #{param[1..n].对象属性}
示例:
User getUser(String name, int age);
select * from <TABLE> where name = #{0} and age = #{1}
// or
select * from <TABLE> where name = #{param1} and age = #{param2}
User getUser(User usr, int flag);
select * from <TABLE> where name = #{0.name} and age = {0.age} and flag = #{1}
// or
select * from <TABLE> where name = #{param1.name} and age = {param1.age} and flag = #{param2}
2.2 使用`@Param`注解
2.2.1 参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{注解名称} | #{param[1..n]}
2.2.2 参数为自定义对象
参数注释为: #{注解名称.对象属性} | #{param[1..n].对象属性}
示例:
User getUser(@Param(value="xm") String name, @Param(value="nl") int age);
select * from <TABLE> where name = #{xm} and age = #{nl}
// or
select * from <TABLE> where name = #{param1} and age = #{param2}
// or
select * from <TABLE> where name = #{xm} and age = #{param2}
User getUser(@Param(value="usr") User user, @Param(value="tag") int flag);
select * from <TABLE> where name = #{usr.name} and age = #{usr.age} and flag = #{tag}
// or
select * from <TABLE> where name = #{param1.name} and age = #{param1.age} and flag = #{param2}
// or
select * from <TABLE> where name = #{usr.name} and age = #{param1.age} and flag = #{param2}
2.2.3 部分参数使用`@Param`注解
当采用部分参数使用`@Param`注解时,参数注释为将以上两种情况结合起来即可。
示例:
User getUser(String name, @Param(value="nl") age, int gendar);
// 对于age的访问不能是 #{1} 只能是 #{param2} | #{nl}
select * from <TABLE> where name = #{0} and age = #{nl} and gendar = #{param3)
总结下
- `@Param`的作用是设置参数别名。设置后的参数只能通过`#{param[1..n]`或者`#{注解别名}`来访问
- 多个参数情况下,均可使用 `#{参数位置[0..n-1]}` | `#{param[1..n]}`来访问参数
最后给出几个源代码中关于参数设置的源代码供大家参考:
参数获取操作:org.apache.ibatis.binding.MapperMethod.getParam(Object[] args)
private Object getParam(Object[] args) {
final int paramCount = paramPositions.size();
// 无参数
if (args == null || paramCount == 0) {
return null;
// 无注解并参数个数为1
} else if (!hasNamedParameters && paramCount == 1) {
return args[paramPositions.get(0)];
} else {
Map<String, Object> param = new MapperParamMap<Object>();
for (int i = 0; i < paramCount; i++) {
param.put(paramNames.get(i), args[paramPositions.get(i)]);
}
// issue #71, add param names as param1, param2...but ensure backward compatibility
// 这就是 #{param[1..n]} 的来源
for (int i = 0; i < paramCount; i++) {
String genericParamName = "param" + String.valueOf(i + 1);
if (!param.containsKey(genericParamName)) {
param.put(genericParamName, args[paramPositions.get(i)]);
}
}
return param;
}
}
SQL预编译参数设置:org.apache.ibatis.executor.parameter.DefaultParameterHandler.setParameters(PreparedStatement ps) throws SQLException
org.apache.ibatis.executor.parameter.DefaultParameterHandler.setParameters(PreparedStatement ps) throws SQLException
public void setParameters(PreparedStatement ps)
throws SQLException {
ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterMappings != null) {
MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject);
for (int i = 0; i < parameterMappings.size(); i++) {
ParameterMapping parameterMapping = parameterMappings.get(i);
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
PropertyTokenizer prop = new PropertyTokenizer(propertyName);
if (parameterObject == null) {
value = null;
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
value = parameterObject;
} else if (boundSql.hasAdditionalParameter(propertyName)) {
value = boundSql.getAdditionalParameter(propertyName);
} else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)
&& boundSql.hasAdditionalParameter(prop.getName())) {
value = boundSql.getAdditionalParameter(prop.getName());
if (value != null) {
value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length()));
}
} else {
value = metaObject == null ? null : metaObject.getValue(propertyName);
}
TypeHandler typeHandler = parameterMapping.getTypeHandler();
if (typeHandler == null) {
throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
}
JdbcType jdbcType = parameterMapping.getJdbcType();
if (value == null && jdbcType == null) jdbcType = configuration.getJdbcTypeForNull();
typeHandler.setParameter(ps, i + 1, value, jdbcType);
}
}
}
}