Search in sources :

Example 26 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping in project qiuyj-code by qiuyuanjun.

the class MysqlBatchInsertParameterObjectResolver method resolveParameterObject.

@Override
public List<ParameterMapping> resolveParameterObject(Configuration config, SqlInfo sqlInfo, Object paramObj, SqlNode sqlNode) {
    List list = (List) ParameterResolver.resolveParameter(paramObj).getParameterValues()[0];
    if (list.isEmpty()) {
        throw new IllegalArgumentException("Method batchInsert parameter can not be an empty list");
    } else {
        this.sqlInfo = sqlInfo;
        this.config = config;
        StringJoiner grammaJoiner = new StringJoiner(",");
        StringJoiner valueJoiner = new StringJoiner(",", "(", ")");
        List<ParameterMapping> singleInstanceParameterMapping = new ArrayList<>(sqlInfo.getFieldCount());
        ParameterMapping canonic = new ParameterMapping.Builder(config, "list", batchInsertTypeHandler).build();
        for (PropertyColumnMapping propertyColumnMapping : sqlInfo.getPropertyColumnMappings()) {
            valueJoiner.add(SqlProvider.PREPARE_FLAG);
            singleInstanceParameterMapping.add(canonic);
        }
        List<ParameterMapping> allParameterMappings = new ArrayList<>(sqlInfo.getFieldCount() * list.size());
        String value = valueJoiner.toString();
        for (int i = 0; i < list.size(); i++) {
            grammaJoiner.add(value);
            allParameterMappings.addAll(singleInstanceParameterMapping);
        }
        // 重新设置StaticTextSqlNode的text属性的值
        resetStaticSqlNode((StaticTextSqlNode) sqlNode, grammaJoiner.toString());
        return allParameterMappings;
    }
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StringJoiner(java.util.StringJoiner)

Example 27 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping in project qiuyj-code by qiuyuanjun.

the class OracleBatchInsertParameterObjectResolver method resolveParameterObject.

@Override
public List<ParameterMapping> resolveParameterObject(Configuration config, SqlInfo sqlInfo, Object paramObj, SqlNode sqlNode) {
    List list = (List) ParameterResolver.resolveParameter(paramObj).getParameterValues()[0];
    if (list.isEmpty()) {
        throw new IllegalArgumentException("Method batchInsert parameter can not be an empty list");
    } else {
        this.sqlInfo = sqlInfo;
        this.config = config;
        String join = " INTO " + sqlInfo.getTableName();
        StringBuilder eachBuilder = new StringBuilder();
        StringJoiner joiner = new StringJoiner(",", "(", ")");
        List<ParameterMapping> instanceParameterMappings = new ArrayList<>(sqlInfo.getFieldCount());
        ParameterMapping canonic = new ParameterMapping.Builder(config, "list", batchInsertTypeHandler).build();
        for (String column : sqlInfo.getAllColumnsWithoutAlias()) {
            joiner.add(column);
            instanceParameterMappings.add(canonic);
        }
        eachBuilder.append(joiner.toString());
        eachBuilder.append(" VALUES ");
        joiner = new StringJoiner(",", "(", ")");
        List<ParameterMapping> parameterMappings = new ArrayList<>(sqlInfo.getFieldCount() * list.size());
        for (int idx = 0; idx < sqlInfo.getFieldCount(); idx++) {
            joiner.add(SqlProvider.PREPARE_FLAG);
        }
        eachBuilder.append(joiner.toString());
        joiner = new StringJoiner(join);
        for (int idx = 0; idx < list.size(); idx++) {
            joiner.add(eachBuilder.toString());
            parameterMappings.addAll(instanceParameterMappings);
        }
        String sql = join + joiner.toString() + " SELECT 1 FROM DUAL";
        resetStaticSqlNode((StaticTextSqlNode) sqlNode, sql);
        return parameterMappings;
    }
}
Also used : ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StringJoiner(java.util.StringJoiner)

Example 28 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping in project yyl_example by Relucent.

the class MybatisBoundSqlExample method main.

public static void main(String[] args) throws IOException {
    // 解决一个不同包引起的冲突
    System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
    Configuration configuration = new Configuration();
    MapperBuilderAssistant builderAssistant = new MapperBuilderAssistant(configuration, null);
    // 定义 NameSpace
    builderAssistant.setCurrentNamespace("sample");
    try (InputStream input = MybatisBoundSqlExample.class.getResourceAsStream("sample-mapper.xml")) {
        // XML XPath解析器
        XPathParser parser = new XPathParser(input);
        // 获得select节点
        for (XNode node : parser.evalNodes("/mapper/select")) {
            // 解析节点
            XMLStatementBuilder statementParser = new XMLStatementBuilder(configuration, builderAssistant, node, null);
            // 这个操作会创建 MappedStatement ,并保存到configuration中
            statementParser.parseStatementNode();
        }
    }
    // 获得 getById
    MappedStatement ms = configuration.getMappedStatement("sample.selectList");
    // 定义参数
    Map<String, String> parameters = new HashMap<>();
    parameters.put("id", "1");
    // 根据参数获得 BoundSql
    BoundSql boundSql = ms.getBoundSql(parameters);
    // 打印最终SQL 和 参数
    System.out.println("[SQL]");
    System.out.println(boundSql.getSql());
    System.out.println("[PARAMETER]");
    for (ParameterMapping pm : boundSql.getParameterMappings()) {
        System.out.println(pm.getProperty() + "=>" + parameters.get(pm.getProperty()));
    }
}
Also used : XPathParser(org.apache.ibatis.parsing.XPathParser) Configuration(org.apache.ibatis.session.Configuration) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) HashMap(java.util.HashMap) BoundSql(org.apache.ibatis.mapping.BoundSql) InputStream(java.io.InputStream) XNode(org.apache.ibatis.parsing.XNode) MappedStatement(org.apache.ibatis.mapping.MappedStatement) MapperBuilderAssistant(org.apache.ibatis.builder.MapperBuilderAssistant) XMLStatementBuilder(org.apache.ibatis.builder.xml.XMLStatementBuilder)

Example 29 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping in project Mybatis-PageHelper by pagehelper.

the class AbstractHelperDialect method handleParameter.

protected void handleParameter(BoundSql boundSql, MappedStatement ms, Class<?> firstClass, Class<?> secondClass) {
    if (boundSql.getParameterMappings() != null) {
        List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(boundSql.getParameterMappings());
        newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, firstClass).build());
        newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, secondClass).build());
        MetaObject metaObject = MetaObjectUtil.forObject(boundSql);
        metaObject.setValue("parameterMappings", newParameterMappings);
    }
}
Also used : ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 30 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping in project Mybatis-PageHelper by pagehelper.

the class InformixDialect method processPageParameter.

@Override
public Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) {
    paramMap.put(PAGEPARAMETER_FIRST, page.getStartRow());
    paramMap.put(PAGEPARAMETER_SECOND, page.getPageSize());
    // 处理pageKey
    pageKey.update(page.getStartRow());
    pageKey.update(page.getPageSize());
    // 处理参数配置
    if (boundSql.getParameterMappings() != null) {
        List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>();
        if (page.getStartRow() > 0) {
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, long.class).build());
        }
        if (page.getPageSize() > 0) {
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, int.class).build());
        }
        newParameterMappings.addAll(boundSql.getParameterMappings());
        MetaObject metaObject = MetaObjectUtil.forObject(boundSql);
        metaObject.setValue("parameterMappings", newParameterMappings);
    }
    return paramMap;
}
Also used : ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) ArrayList(java.util.ArrayList)

Aggregations

ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)40 MetaObject (org.apache.ibatis.reflection.MetaObject)18 ArrayList (java.util.ArrayList)17 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)15 MappedStatement (org.apache.ibatis.mapping.MappedStatement)9 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)8 Configuration (org.apache.ibatis.session.Configuration)5 PropertyColumnMapping (com.qiuyj.mybatis.PropertyColumnMapping)4 StaticTextSqlNode (org.apache.ibatis.scripting.xmltags.StaticTextSqlNode)4 JdbcType (org.apache.ibatis.type.JdbcType)4 TypeHandler (org.apache.ibatis.type.TypeHandler)4 StringJoiner (java.util.StringJoiner)3 Section (org.apache.ibatis.domain.blog.Section)3 SQL (org.apache.ibatis.jdbc.SQL)3 BoundSql (org.apache.ibatis.mapping.BoundSql)3 BeanExampleResolver (com.qiuyj.mybatis.BeanExampleResolver)2 Conditionable (indi.mybatis.flying.models.Conditionable)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 List (java.util.List)2