天天看點

mybatis參數映射

mybatis參數映射(?)

規則

  1. @Param

    注解參數時可使用
    1. #{參數位置[0..n-1]}
    或者
    1. #{param[1..n]}
    如果參數類型是自定義對象(bean)時,隻需加上

    .對象屬性

    即可
    1. #{參數位置[0..n-1].對象屬性}
    2. #{param[1..n].對象屬性}
    特别的,如果非注解參數隻有一個時,可使用
    1. #{任意字元}
    來通路參數
  2. @Param

    的作用是設定參數别名。設定後的參數隻能通過
    1. #{注解别名}
    或者
    1. #{param[1..n]}
    如果參數類型是自定義對象(bean)時,隻需加上

    .對象屬性

    即可
    1. #{注解别名.屬性}
    2. #{param[1..n].屬性}
    來通路參數

示例

為了明确上述規則,我們的示例具體細分了各種情況進行展示。

非注解型

一個參數
<span style="font-weight: normal;"><span style="font-family:Microsoft YaHei;font-size:14px;">User getUserById(int id);

select * from <TABLE> where id = #{id}
// or
select * from <TABLE> where id = #{abdc}
// or
select * from <TABLE> where id = #{param1}

User getUser(User user); // user.getName user.getAge 

select * from <TABLE> where name = #{name} and age = #{age}</span></span>
           
多個參數
<span style="font-family:Microsoft YaHei;font-size:14px;">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}</span>
           

注解型

一個參數
<span style="font-family:Microsoft YaHei;font-size:14px;">User getUserById(@Param(value="keyId") int id);  

select * from <TABEL> where id = #{keyId}  
// or  
select * from <TABLE> where id = #{param1}  

User getUser(@Param(value="user") 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}</span>
           
多個參數
<span style="font-family:Microsoft YaHei;font-size:14px;">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}</span>
           

非注解和注解型混合型

當采用部分參數使用

@Param

注解時,參數注釋為将以上兩種情況結合起來即可.

<span style="font-family:Microsoft YaHei;font-size:14px;">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)</span>
           

架構主要映射處理代碼

參數的擷取

<span style="font-family:Microsoft YaHei;font-size:14px;">org.apache.ibatis.binding.MapperMethod
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;  
    }  
}</span>
           

SQL預編譯參數設定

<span style="font-family:Microsoft YaHei;font-size:14px;">org.apache.ibatis.executor.parameter.DefaultParameterHandler
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>
           

繼續閱讀