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;
}
}
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;
}
}
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()));
}
}
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);
}
}
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;
}
Aggregations