来源:http://blog.csdn.net/oxcow/article/details/8024717/
请查看最新整理地址: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 参数为自定义对象
参数注释为: #{对象属性}
示例:
- <span mce_style="font-size: medium;" style="font-size:14px; "> User getUserById(int id);
-
-
-
- select * from <TABLE> where id = #{id}
-
- User getUser(User user);
-
- select * from <TABLE> where name = #{name} and age = #{age}</span>
1.2 使用`@Param`注解
1.2.1 参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{注解名称} | #{param1}
1.2.2 参数为自定义对象
参数注释为: #{注解名称.对象属性} | #{param1.对象属性}
示例:
- <span mce_style="font-size: medium;" style="font-size:14px; "> User getUserById(@Param(value="keyId") int id);
-
- select * from <TABEL> where id = #{keyId}
-
- select * from <TABLE> where id = #{param1}
-
- User getUser(@Param(value="usr") User user);
- select * from <TABLE> where name = #{user.name} and age = #{user.age}
-
- select * from <TABLE> where name = #{param1.name} and age = #{param1.age}</span>
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].对象属性}
示例:
- <span mce_style="font-size: medium;" style="font-size:14px; "> User getUser(String name, int age);
- select * from <TABLE> where name = #{0} and age = #{1}
-
- 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}
-
- select * from <TABLE> where name = #{param1.name} and age = {param1.age} and flag = #{param2}</span>
2.2 使用`@Param`注解
2.2.1 参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{注解名称} | #{param[1..n]}
2.2.2 参数为自定义对象
参数注释为: #{注解名称.对象属性} | #{param[1..n].对象属性}
示例:
- <span mce_style="font-size: medium;" style="font-size:14px; "> User getUser(@Param(value="xm") String name, @Param(value="nl") int age);
- select * from <TABLE> where name = #{xm} and age = #{nl}
-
- select * from <TABLE> where name = #{param1} and age = #{param2}
-
- 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}
-
- select * from <TABLE> where name = #{param1.name} and age = #{param1.age} and flag = #{param2}
-
- select * from <TABLE> where name = #{usr.name} and age = #{param1.age} and flag = #{param2}</span>
2.2.3 部分参数使用`@Param`注解
当采用部分参数使用`@Param`注解时,参数注释为将以上两种情况结合起来即可。
示例:
- <span mce_style="font-size: medium;" style="font-size:14px; ">User getUser(String name, @Param(value="nl") age, int gendar);
-
-
- select * from <TABLE> where name = #{0} and age = #{nl} and gendar = #{param3)</span>
总结下
- `@Param`的作用是设置参数别名。设置后的参数只能通过`#{param[1..n]`或者`#{注解别名}`来访问
- 多个参数情况下,均可使用 `#{参数位置[0..n-1]}` | `#{param[1..n]}`来访问参数
最后给出几个源代码中关于参数设置的源代码供大家参考:
参数获取操作:org.apache.ibatis.binding.MapperMethod.getParam(Object[] args)
- <span mce_style="font-size: medium;" style="font-size:14px; "> private Object getParam(Object[] args) {
- final int paramCount = paramPositions.size();
-
- if (args == null || paramCount == 0) {
- return null;
-
- } 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)]);
- }
-
-
- 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;
- }
- }</span>
SQL预编译参数设置:org.apache.ibatis.executor.parameter.DefaultParameterHandler.setParameters(PreparedStatement ps) throws SQLException
- <span mce_style="font-size: medium;" style="font-size:14px; ">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);
- }
- }
- }
- }</span>