org.apache.ibatis.mapping.SqlSource Java Examples
The following examples show how to use
org.apache.ibatis.mapping.SqlSource.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RecordDelete.java From Aooms with Apache License 2.0 | 6 votes |
@Override public void process() { MappedStatement mappedStatement = MetaObjectAssistant.getMappedStatement(metaObject); Object parameterObject = MetaObjectAssistant.getParameterObject(metaObject); Record record = (Record) parameterObject; String tableName = record.getGeneral(MyBatisConst.TABLE_NAME_PLACEHOLDER); String pkName = record.getGeneralOrDefault(MyBatisConst.TABLE_PK_NAME_PLACEHOLDER, AoomsVar.ID); Object pkValue = record.get(pkName); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(" delete from "); stringBuilder.append(tableName); // tableName stringBuilder.append(" where "+ pkName +" = #{"+ pkName +"} "); String sql = stringBuilder.toString(); // SqlSource sqlSource = new XMLLanguageDriver().createSqlSource(mappedStatement.getConfiguration(), sql, Map.class); Configuration configuration = MetaObjectAssistant.getConfiguration(metaObject); SqlSource sqlSource = configuration.getLanguageRegistry().getDefaultDriver().createSqlSource(mappedStatement.getConfiguration(), sql, Map.class); BoundSql boundSql = sqlSource.getBoundSql(parameterObject); MetaObjectAssistant.setDelegateBoundSql(metaObject,boundSql); MetaObjectAssistant.setDelegateParameterHandlerBoundSql(metaObject,boundSql); }
Example #2
Source File: MybatisSqlInterceptor.java From taoshop with Apache License 2.0 | 6 votes |
private MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource) { MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType()); builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) { StringBuilder keyProperties = new StringBuilder(); for (String keyProperty : ms.getKeyProperties()) { keyProperties.append(keyProperty).append(","); } keyProperties.delete(keyProperties.length() - 1, keyProperties.length()); builder.keyProperty(keyProperties.toString()); } builder.timeout(ms.getTimeout()); builder.parameterMap(ms.getParameterMap()); builder.resultMaps(ms.getResultMaps()); builder.resultSetType(ms.getResultSetType()); builder.cache(ms.getCache()); builder.flushCacheRequired(ms.isFlushCacheRequired()); builder.useCache(ms.isUseCache()); return builder.build(); }
Example #3
Source File: MySqlPagingPlugin.java From mybatis-test with Apache License 2.0 | 6 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); RowBounds rb = (RowBounds) args[ROW_BOUNDS_INDEX]; if (rb == RowBounds.DEFAULT) { return invocation.proceed(); } args[ROW_BOUNDS_INDEX] = RowBounds.DEFAULT; MappedStatement ms = (MappedStatement) args[MAPPED_STATEMENT_INDEX]; BoundSql boundSql = ms.getBoundSql(args[PARAMETER_INDEX]); System.out.println(); String sql = boundSql.getSql(); String limit = String.format("LIMIT %d,%d", rb.getOffset(), rb.getLimit()); sql = sql + " " + limit; SqlSource sqlSource = new StaticSqlSource(ms.getConfiguration(), sql, boundSql.getParameterMappings()); Field field = MappedStatement.class.getDeclaredField("sqlSource"); field.setAccessible(true); field.set(ms, sqlSource); return invocation.proceed(); }
Example #4
Source File: MapperAnnotationBuilder.java From mybatis with Apache License 2.0 | 6 votes |
private SqlSource getSqlSourceFromAnnotations(Method method, Class<?> parameterType, LanguageDriver languageDriver) { try { Class<? extends Annotation> sqlAnnotationType = getSqlAnnotationType(method); Class<? extends Annotation> sqlProviderAnnotationType = getSqlProviderAnnotationType(method); if (sqlAnnotationType != null) { if (sqlProviderAnnotationType != null) { throw new BindingException("You cannot supply both a static SQL and SqlProvider to method named " + method.getName()); } Annotation sqlAnnotation = method.getAnnotation(sqlAnnotationType); final String[] strings = (String[]) sqlAnnotation.getClass().getMethod("value").invoke(sqlAnnotation); return buildSqlSourceFromStrings(strings, parameterType, languageDriver); } else if (sqlProviderAnnotationType != null) { Annotation sqlProviderAnnotation = method.getAnnotation(sqlProviderAnnotationType); return new ProviderSqlSource(assistant.getConfiguration(), sqlProviderAnnotation); } return null; } catch (Exception e) { throw new BuilderException("Could not find value method on SQL annotation. Cause: " + e, e); } }
Example #5
Source File: DynamicSqlSource.java From mybatis with Apache License 2.0 | 6 votes |
@Override public BoundSql getBoundSql(Object parameterObject) { //生成一个动态上下文 DynamicContext context = new DynamicContext(configuration, parameterObject); //这里SqlNode.apply只是将${}这种参数替换掉,并没有替换#{}这种参数 rootSqlNode.apply(context); //调用SqlSourceBuilder SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration); Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass(); //SqlSourceBuilder.parse,注意这里返回的是StaticSqlSource,解析完了就把那些参数都替换成?了,也就是最基本的JDBC的SQL写法 SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings()); //看似是又去递归调用SqlSource.getBoundSql,其实因为是StaticSqlSource,所以没问题,不是递归调用 BoundSql boundSql = sqlSource.getBoundSql(parameterObject); for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) { boundSql.setAdditionalParameter(entry.getKey(), entry.getValue()); } return boundSql; }
Example #6
Source File: XMLLanguageDriver.java From mybatis with Apache License 2.0 | 6 votes |
@Override public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) { // issue #3 if (script.startsWith("<script>")) { XPathParser parser = new XPathParser(script, false, configuration.getVariables(), new XMLMapperEntityResolver()); return createSqlSource(configuration, parser.evalNode("/script"), parameterType); } else { // issue #127 script = PropertyParser.parse(script, configuration.getVariables()); TextSqlNode textSqlNode = new TextSqlNode(script); //一种是动态,一种是原始 if (textSqlNode.isDynamic()) { return new DynamicSqlSource(configuration, textSqlNode); } else { return new RawSqlSource(configuration, script, parameterType); } } }
Example #7
Source File: ProviderSqlSource.java From mybatis with Apache License 2.0 | 6 votes |
private SqlSource createSqlSource(Object parameterObject) { try { String sql; if (providerTakesParameterObject) { sql = (String) providerMethod.invoke(providerType.newInstance(), parameterObject); } else { sql = (String) providerMethod.invoke(providerType.newInstance()); } Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass(); return sqlSourceParser.parse(sql, parameterType, new HashMap<String, Object>()); } catch (Exception e) { throw new BuilderException("Error invoking SqlProvider method (" + providerType.getName() + "." + providerMethod.getName() + "). Cause: " + e, e); } }
Example #8
Source File: XMLLanguageDriver.java From mybaties with Apache License 2.0 | 6 votes |
@Override public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) { // issue #3 if (script.startsWith("<script>")) { XPathParser parser = new XPathParser(script, false, configuration.getVariables(), new XMLMapperEntityResolver()); return createSqlSource(configuration, parser.evalNode("/script"), parameterType); } else { // issue #127 script = PropertyParser.parse(script, configuration.getVariables()); TextSqlNode textSqlNode = new TextSqlNode(script); //一种是动态,一种是原始 if (textSqlNode.isDynamic()) { return new DynamicSqlSource(configuration, textSqlNode); } else { return new RawSqlSource(configuration, script, parameterType); } } }
Example #9
Source File: DynamicSqlSource.java From mybaties with Apache License 2.0 | 6 votes |
@Override public BoundSql getBoundSql(Object parameterObject) { //生成一个动态上下文 DynamicContext context = new DynamicContext(configuration, parameterObject); //这里SqlNode.apply只是将${}这种参数替换掉,并没有替换#{}这种参数 rootSqlNode.apply(context); //调用SqlSourceBuilder SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration); Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass(); //SqlSourceBuilder.parse,注意这里返回的是StaticSqlSource,解析完了就把那些参数都替换成?了,也就是最基本的JDBC的SQL写法 SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings()); //看似是又去递归调用SqlSource.getBoundSql,其实因为是StaticSqlSource,所以没问题,不是递归调用 BoundSql boundSql = sqlSource.getBoundSql(parameterObject); for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) { boundSql.setAdditionalParameter(entry.getKey(), entry.getValue()); } return boundSql; }
Example #10
Source File: MyBatisUtils.java From platform with Apache License 2.0 | 6 votes |
/** * 复制MappedStatement * * @param ms {@link MappedStatement} * @param newSqlSource {@link SqlSource} * @return {@link MappedStatement} */ public static MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) { MappedStatement.Builder builder = new MappedStatement.Builder( ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType() ); builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); String[] keyProperties = ms.getKeyProperties(); builder.keyProperty(keyProperties == null ? null : keyProperties[0]); builder.timeout(ms.getTimeout()); builder.parameterMap(ms.getParameterMap()); builder.resultMaps(ms.getResultMaps()); builder.resultSetType(ms.getResultSetType()); builder.cache(ms.getCache()); builder.flushCacheRequired(ms.isFlushCacheRequired()); builder.useCache(ms.isUseCache()); return builder.build(); }
Example #11
Source File: MapperEnhancer.java From tsharding with MIT License | 6 votes |
protected MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource, String newMsId) { MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), newMsId, newSqlSource, ms.getSqlCommandType()); builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); // setStatementTimeout() builder.timeout(ms.getTimeout()); // setParameterMap() builder.parameterMap(ms.getParameterMap()); // setStatementResultMap() List<ResultMap> resultMaps = ms.getResultMaps(); builder.resultMaps(resultMaps); builder.resultSetType(ms.getResultSetType()); // setStatementCache() builder.cache(ms.getCache()); builder.flushCacheRequired(ms.isFlushCacheRequired()); builder.useCache(ms.isUseCache()); return builder.build(); }
Example #12
Source File: PagePluging.java From aaden-pay with Apache License 2.0 | 6 votes |
private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) { Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType()); builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); // builder.keyProperty((ms.getKeyProperty())); builder.keyProperty(arrayToStr(ms.getKeyProperties())); // setStatementTimeout() builder.timeout(ms.getTimeout()); // setStatementResultMap() builder.parameterMap(ms.getParameterMap()); // setStatementResultMap() builder.resultMaps(ms.getResultMaps()); builder.resultSetType(ms.getResultSetType()); // setStatementCache() builder.cache(ms.getCache()); builder.flushCacheRequired(ms.isFlushCacheRequired()); builder.useCache(ms.isUseCache()); return builder.build(); }
Example #13
Source File: DeleteBuilder.java From azeroth with Apache License 2.0 | 6 votes |
/** * @param configuration * @param entity */ public static void build(Configuration configuration, LanguageDriver languageDriver, EntityInfo entity) { String msId = entity.getMapperClass().getName() + "." + GeneralSqlGenerator.methodDefines.deleteName(); // 从参数对象里提取注解信息 EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass()); // 生成sql String sql = buildDeleteSql(entityMapper); SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, entity.getEntityClass()); MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.DELETE); MappedStatement statement = statementBuilder.build(); configuration.addMappedStatement(statement); }
Example #14
Source File: UpdateBuilder.java From azeroth with Apache License 2.0 | 6 votes |
/** * @param configuration * @param entity */ public static void build(Configuration configuration, LanguageDriver languageDriver, EntityInfo entity) { String[] names = GeneralSqlGenerator.methodDefines.updateName().split(","); for (String name : names) { String msId = entity.getMapperClass().getName() + "." + name; EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass()); String sql = buildUpdateSql(entityMapper, name.endsWith("Selective")); SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, entity.getEntityClass()); MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.UPDATE); MappedStatement statement = statementBuilder.build(); configuration.addMappedStatement(statement); } }
Example #15
Source File: ExecutorTestHelper.java From mybatis with Apache License 2.0 | 5 votes |
public static MappedStatement prepareComplexSelectBlogMappedStatement(final Configuration config) { final TypeHandlerRegistry registry = config.getTypeHandlerRegistry(); final SqlSource sqlSource = new StaticSqlSource(config, "SELECT b.id, b.author_id, b.title, a.username, a.password, a.email, a.bio" + " FROM blog b" + " INNER JOIN author a ON b.author_id = a.id" + " WHERE b.id = ?"); final ParameterMap parameterMap = new ParameterMap.Builder(config, "defaultParameterMap", int.class, new ArrayList<ParameterMapping>() { { add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build()); } }).build(); final ResultMap resultMap = new ResultMap.Builder(config, "defaultResultMap", Blog.class, new ArrayList<ResultMapping>() { { add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)) .flags(new ArrayList<ResultFlag>() { { add(ResultFlag.ID); } }).build()); add(new ResultMapping.Builder(config, "title", "title", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.id", "author_id", registry.getTypeHandler(int.class)).build()); add(new ResultMapping.Builder(config, "author.username", "username", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.password", "password", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.email", "email", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.bio", "bio", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "posts", "id", registry.getTypeHandler(int.class)).javaType(List.class).nestedQueryId("selectPostsForBlog").build()); } }).build(); return new MappedStatement.Builder(config, "selectBlogById", sqlSource, SqlCommandType.SELECT) .parameterMap(parameterMap) .resultMaps(new ArrayList<ResultMap>() { { add(resultMap); } }).build(); }
Example #16
Source File: ExecutorTestHelper.java From mybaties with Apache License 2.0 | 5 votes |
public static MappedStatement prepareSelectBlogByIdAndAuthor(final Configuration config) { final TypeHandlerRegistry registry = config.getTypeHandlerRegistry(); final SqlSource sqlSource = new StaticSqlSource(config,"SELECT b.id, b.author_id, b.title, a.username, a.password, a.email, a.bio" + " FROM blog b" + " INNER JOIN author a ON b.author_id = a.id" + " WHERE b.id = ? and a.id = ?"); final ParameterMap parameterMap = new ParameterMap.Builder(config, "defaultParameterMap", Map.class, new ArrayList<ParameterMapping>() { { add(new ParameterMapping.Builder(config, "blogId", registry.getTypeHandler(int.class)).build()); add(new ParameterMapping.Builder(config, "authorId", registry.getTypeHandler(int.class)).build()); } }).build(); final ResultMap resultMap = new ResultMap.Builder(config, "defaultResultMap", Blog.class, new ArrayList<ResultMapping>() { { add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)) .flags(new ArrayList<ResultFlag>() { { add(ResultFlag.ID); } }).build()); add(new ResultMapping.Builder(config, "title", "title", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.id", "author_id", registry.getTypeHandler(int.class)).build()); add(new ResultMapping.Builder(config, "author.username", "username", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.password", "password", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.email", "email", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.bio", "bio", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "posts", "id", registry.getTypeHandler(int.class)).javaType(List.class).nestedQueryId("selectPostsForBlog").build()); } }).build(); return new MappedStatement.Builder(config, "selectBlogByIdAndAuthor", sqlSource, SqlCommandType.SELECT) .parameterMap(parameterMap) .resultMaps(new ArrayList<ResultMap>() { { add(resultMap); } }).build(); }
Example #17
Source File: MapperAnnotationBuilder.java From mybaties with Apache License 2.0 | 5 votes |
private SqlSource buildSqlSourceFromStrings(String[] strings, Class<?> parameterTypeClass, LanguageDriver languageDriver) { final StringBuilder sql = new StringBuilder(); for (String fragment : strings) { sql.append(fragment); sql.append(" "); } return languageDriver.createSqlSource(configuration, sql.toString(), parameterTypeClass); }
Example #18
Source File: MapperBuilderAssistant.java From mybaties with Apache License 2.0 | 5 votes |
/** Backward compatibility signature */ //向后兼容方法 public MappedStatement addMappedStatement( String id, SqlSource sqlSource, StatementType statementType, SqlCommandType sqlCommandType, Integer fetchSize, Integer timeout, String parameterMap, Class<?> parameterType, String resultMap, Class<?> resultType, ResultSetType resultSetType, boolean flushCache, boolean useCache, boolean resultOrdered, KeyGenerator keyGenerator, String keyProperty, String keyColumn, String databaseId, LanguageDriver lang) { return addMappedStatement( id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterType, resultMap, resultType, resultSetType, flushCache, useCache, resultOrdered, keyGenerator, keyProperty, keyColumn, databaseId, lang, null); }
Example #19
Source File: SqlSourceBuilder.java From mybaties with Apache License 2.0 | 5 votes |
public SqlSource parse(String originalSql, Class<?> parameterType, Map<String, Object> additionalParameters) { ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(configuration, parameterType, additionalParameters); //替换#{}中间的部分,如何替换,逻辑在ParameterMappingTokenHandler GenericTokenParser parser = new GenericTokenParser("#{", "}", handler); String sql = parser.parse(originalSql); //返回静态SQL源码 return new StaticSqlSource(configuration, sql, handler.getParameterMappings()); }
Example #20
Source File: MapperAnnotationBuilder.java From mybatis with Apache License 2.0 | 5 votes |
private KeyGenerator handleSelectKeyAnnotation(SelectKey selectKeyAnnotation, String baseStatementId, Class<?> parameterTypeClass, LanguageDriver languageDriver) { String id = baseStatementId + SelectKeyGenerator.SELECT_KEY_SUFFIX; Class<?> resultTypeClass = selectKeyAnnotation.resultType(); StatementType statementType = selectKeyAnnotation.statementType(); String keyProperty = selectKeyAnnotation.keyProperty(); String keyColumn = selectKeyAnnotation.keyColumn(); boolean executeBefore = selectKeyAnnotation.before(); // defaults boolean useCache = false; KeyGenerator keyGenerator = new NoKeyGenerator(); Integer fetchSize = null; Integer timeout = null; boolean flushCache = false; String parameterMap = null; String resultMap = null; ResultSetType resultSetTypeEnum = null; SqlSource sqlSource = buildSqlSourceFromStrings(selectKeyAnnotation.statement(), parameterTypeClass, languageDriver); SqlCommandType sqlCommandType = SqlCommandType.SELECT; assistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum, flushCache, useCache, false, keyGenerator, keyProperty, keyColumn, null, languageDriver, null); id = assistant.applyCurrentNamespace(id, false); MappedStatement keyStatement = configuration.getMappedStatement(id, false); SelectKeyGenerator answer = new SelectKeyGenerator(keyStatement, executeBefore); configuration.addKeyGenerator(id, answer); return answer; }
Example #21
Source File: RawSqlSourceTest.java From mybaties with Apache License 2.0 | 5 votes |
private void test(String statement, Class<? extends SqlSource> sqlSource) { SqlSession sqlSession = sqlSessionFactory.openSession(); try { Assert.assertEquals(sqlSource, sqlSession.getConfiguration().getMappedStatement(statement).getSqlSource().getClass()); String sql = sqlSession.getConfiguration().getMappedStatement(statement).getSqlSource().getBoundSql('?').getSql(); Assert.assertEquals("select * from users where id = ?", sql); User user = sqlSession.selectOne(statement, 1); Assert.assertEquals("User1", user.getName()); } finally { sqlSession.close(); } }
Example #22
Source File: ExecutorTestHelper.java From mybaties with Apache License 2.0 | 5 votes |
public static MappedStatement prepareComplexSelectBlogMappedStatement(final Configuration config) { final TypeHandlerRegistry registry = config.getTypeHandlerRegistry(); final SqlSource sqlSource = new StaticSqlSource(config, "SELECT b.id, b.author_id, b.title, a.username, a.password, a.email, a.bio" + " FROM blog b" + " INNER JOIN author a ON b.author_id = a.id" + " WHERE b.id = ?"); final ParameterMap parameterMap = new ParameterMap.Builder(config, "defaultParameterMap", int.class, new ArrayList<ParameterMapping>() { { add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build()); } }).build(); final ResultMap resultMap = new ResultMap.Builder(config, "defaultResultMap", Blog.class, new ArrayList<ResultMapping>() { { add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)) .flags(new ArrayList<ResultFlag>() { { add(ResultFlag.ID); } }).build()); add(new ResultMapping.Builder(config, "title", "title", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.id", "author_id", registry.getTypeHandler(int.class)).build()); add(new ResultMapping.Builder(config, "author.username", "username", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.password", "password", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.email", "email", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.bio", "bio", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "posts", "id", registry.getTypeHandler(int.class)).javaType(List.class).nestedQueryId("selectPostsForBlog").build()); } }).build(); return new MappedStatement.Builder(config, "selectBlogById", sqlSource, SqlCommandType.SELECT) .parameterMap(parameterMap) .resultMaps(new ArrayList<ResultMap>() { { add(resultMap); } }).build(); }
Example #23
Source File: MybatisMapperBuildAssistant.java From spring-data-mybatis with Apache License 2.0 | 5 votes |
/** * build sql source for mybatis from string concat by array. */ protected SqlSource buildSqlSourceFromStrings(String[] strings, Class<?> parameterTypeClass) { final StringBuilder sql = new StringBuilder(); for (String fragment : strings) { sql.append(fragment); sql.append(" "); } LanguageDriver languageDriver = getLanguageDriver(); return languageDriver.createSqlSource(configuration, sql.toString().trim(), parameterTypeClass); }
Example #24
Source File: MybatisMapperBuildAssistant.java From spring-data-mybatis with Apache License 2.0 | 5 votes |
protected void addMappedStatement(String id, SqlSource sqlSource, StatementType statementType, SqlCommandType sqlCommandType, Integer fetchSize, Integer timeout, String parameterMap, Class<?> parameterType, String resultMap, Class<?> resultType, ResultSetType resultSetType, boolean flushCache, boolean useCache, boolean resultOrdered, KeyGenerator keyGenerator, String keyProperty, String keyColumn, String databaseId, LanguageDriver lang, String resultSets) { assistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterType, resultMap, resultType, resultSetType, flushCache, useCache, resultOrdered, keyGenerator, keyProperty, keyColumn, databaseId, lang, resultSets); }
Example #25
Source File: PageInterceptor.java From Zebra with Apache License 2.0 | 5 votes |
public MappedStatement buildMappedStatement(MappedStatement ms, SqlSource newSqlSource, String id, List<ResultMap> resultMaps) { MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), id, newSqlSource, ms.getSqlCommandType()); builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) { StringBuilder keyProperties = new StringBuilder(); for (String keyProperty : ms.getKeyProperties()) { keyProperties.append(keyProperty).append(","); } keyProperties.delete(keyProperties.length() - 1, keyProperties.length()); builder.keyProperty(keyProperties.toString()); } builder.timeout(ms.getTimeout()); builder.parameterMap(ms.getParameterMap()); builder.resultMaps(resultMaps); builder.resultSetType(ms.getResultSetType()); builder.cache(ms.getCache()); builder.flushCacheRequired(ms.isFlushCacheRequired()); builder.useCache(ms.isUseCache()); return builder.build(); }
Example #26
Source File: XMLStatementBuilder.java From mybatis with Apache License 2.0 | 5 votes |
private void parseSelectKeyNode(String id, XNode nodeToHandle, Class<?> parameterTypeClass, LanguageDriver langDriver, String databaseId) { String resultType = nodeToHandle.getStringAttribute("resultType"); Class<?> resultTypeClass = resolveClass(resultType); StatementType statementType = StatementType.valueOf(nodeToHandle.getStringAttribute("statementType", StatementType.PREPARED.toString())); String keyProperty = nodeToHandle.getStringAttribute("keyProperty"); String keyColumn = nodeToHandle.getStringAttribute("keyColumn"); boolean executeBefore = "BEFORE".equals(nodeToHandle.getStringAttribute("order", "AFTER")); //defaults boolean useCache = false; boolean resultOrdered = false; KeyGenerator keyGenerator = new NoKeyGenerator(); Integer fetchSize = null; Integer timeout = null; boolean flushCache = false; String parameterMap = null; String resultMap = null; ResultSetType resultSetTypeEnum = null; SqlSource sqlSource = langDriver.createSqlSource(configuration, nodeToHandle, parameterTypeClass); SqlCommandType sqlCommandType = SqlCommandType.SELECT; builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum, flushCache, useCache, resultOrdered, keyGenerator, keyProperty, keyColumn, databaseId, langDriver, null); id = builderAssistant.applyCurrentNamespace(id, false); MappedStatement keyStatement = configuration.getMappedStatement(id, false); configuration.addKeyGenerator(id, new SelectKeyGenerator(keyStatement, executeBefore)); }
Example #27
Source File: ExecutorTestHelper.java From mybatis with Apache License 2.0 | 5 votes |
public static MappedStatement prepareSelectBlogByIdAndAuthor(final Configuration config) { final TypeHandlerRegistry registry = config.getTypeHandlerRegistry(); final SqlSource sqlSource = new StaticSqlSource(config,"SELECT b.id, b.author_id, b.title, a.username, a.password, a.email, a.bio" + " FROM blog b" + " INNER JOIN author a ON b.author_id = a.id" + " WHERE b.id = ? and a.id = ?"); final ParameterMap parameterMap = new ParameterMap.Builder(config, "defaultParameterMap", Map.class, new ArrayList<ParameterMapping>() { { add(new ParameterMapping.Builder(config, "blogId", registry.getTypeHandler(int.class)).build()); add(new ParameterMapping.Builder(config, "authorId", registry.getTypeHandler(int.class)).build()); } }).build(); final ResultMap resultMap = new ResultMap.Builder(config, "defaultResultMap", Blog.class, new ArrayList<ResultMapping>() { { add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)) .flags(new ArrayList<ResultFlag>() { { add(ResultFlag.ID); } }).build()); add(new ResultMapping.Builder(config, "title", "title", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.id", "author_id", registry.getTypeHandler(int.class)).build()); add(new ResultMapping.Builder(config, "author.username", "username", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.password", "password", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.email", "email", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "author.bio", "bio", registry.getTypeHandler(String.class)).build()); add(new ResultMapping.Builder(config, "posts", "id", registry.getTypeHandler(int.class)).javaType(List.class).nestedQueryId("selectPostsForBlog").build()); } }).build(); return new MappedStatement.Builder(config, "selectBlogByIdAndAuthor", sqlSource, SqlCommandType.SELECT) .parameterMap(parameterMap) .resultMaps(new ArrayList<ResultMap>() { { add(resultMap); } }).build(); }
Example #28
Source File: OffsetLimitInterceptor.java From AsuraFramework with Apache License 2.0 | 5 votes |
private MappedStatement copyFromMappedStatement(MappedStatement ms,SqlSource newSqlSource) { Builder builder = new Builder(ms.getConfiguration(),ms.getId(),newSqlSource,ms.getSqlCommandType()); builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); if(ms.getKeyProperties() != null && ms.getKeyProperties().length !=0){ StringBuffer keyProperties = new StringBuffer(); for(String keyProperty : ms.getKeyProperties()){ keyProperties.append(keyProperty).append(","); } keyProperties.delete(keyProperties.length()-1, keyProperties.length()); builder.keyProperty(keyProperties.toString()); } //setStatementTimeout() builder.timeout(ms.getTimeout()); //setStatementResultMap() builder.parameterMap(ms.getParameterMap()); //setStatementResultMap() builder.resultMaps(ms.getResultMaps()); builder.resultSetType(ms.getResultSetType()); //setStatementCache() builder.cache(ms.getCache()); builder.flushCacheRequired(ms.isFlushCacheRequired()); builder.useCache(ms.isUseCache()); return builder.build(); }
Example #29
Source File: VelocitySqlSource.java From mybaties with Apache License 2.0 | 5 votes |
public BoundSql getBoundSql(Object parameterObject) { Map<String, Object> bindings = createBindings(parameterObject, configuration); VelocityContext context = new VelocityContext(bindings); StringWriter sw = new StringWriter(); script.merge(context, sw); VelocitySqlSourceBuilder sqlSourceParser = new VelocitySqlSourceBuilder(configuration); Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass(); SqlSource sqlSource = sqlSourceParser.parse(sw.toString(), parameterType); BoundSql boundSql = sqlSource.getBoundSql(parameterObject); for (Map.Entry<String, Object> entry : bindings.entrySet()) { boundSql.setAdditionalParameter(entry.getKey(), entry.getValue()); } return boundSql; }
Example #30
Source File: RecordInsert.java From Aooms with Apache License 2.0 | 5 votes |
@Override public void process() { MappedStatement mappedStatement = MetaObjectAssistant.getMappedStatement(metaObject); Object parameterObject = MetaObjectAssistant.getParameterObject(metaObject); Record record = (Record) parameterObject; String tableName = record.getGeneral(MyBatisConst.TABLE_NAME_PLACEHOLDER); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(" insert into "); stringBuilder.append(tableName); // tableName stringBuilder.append(" ({}) "); stringBuilder.append(" values "); stringBuilder.append(" ({}) "); StringBuilder columns = new StringBuilder(); StringBuilder values = new StringBuilder(); int index = 0; Iterator<String> keyIterator = record.keySet().iterator(); while (keyIterator.hasNext()) { String key = keyIterator.next(); if (index > 0) { columns.append(","); values.append(","); } columns.append(key); values.append("#{").append(key).append("}"); index++; } String sql = StrUtil.format(stringBuilder, columns, values); Configuration configuration = MetaObjectAssistant.getConfiguration(metaObject); SqlSource sqlSource = configuration.getLanguageRegistry().getDefaultDriver().createSqlSource(mappedStatement.getConfiguration(), sql, Map.class); BoundSql boundSql = sqlSource.getBoundSql(parameterObject); //metaObject.setValue("delegate.boundSql", boundSql); //metaObject.setValue("delegate.parameterHandler.boundSql", boundSql); MetaObjectAssistant.setDelegateBoundSql(metaObject,boundSql); MetaObjectAssistant.setDelegateParameterHandlerBoundSql(metaObject,boundSql); }