下面列出了org.apache.ibatis.annotations.Options#org.apache.ibatis.mapping.SqlSource 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
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();
}
@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();
}
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);
}
}
/**
* 复制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();
}
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();
}
/**
* @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);
}
}
/**
* @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);
}
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();
}
@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;
}
@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);
}
}
}
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);
}
}
@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;
}
@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);
}
}
}
@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);
}
@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 = String.valueOf(record.getOrDefault(MyBatisConst.TABLE_PK_NAME_PLACEHOLDER, AoomsVar.ID));
Object pkValue = record.get(pkName);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(" update ");
stringBuilder.append(tableName); // tableName
stringBuilder.append(" set {} ");
stringBuilder.append(" where "+ pkName +" = #{"+ pkName +"} ");
StringBuilder columns = new StringBuilder();
int index = 0;
Iterator<String> keyIterator = record.keySet().iterator();
while (keyIterator.hasNext()) {
String key = keyIterator.next();
if (index > 0) {
columns.append(",");
}
columns.append(key).append(" = ").append("#{").append(key).append("}");
index++;
}
String sql = StrUtil.format(stringBuilder, columns);
//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);
}
@Test
public void test() {
// String sql = "SELECT * FROM Author WHERE name = #{name} AND age = #{age}";
String sql = "SELECT * FROM Author WHERE age = #{age,javaType=int,jdbcType=NUMERIC}";
SqlSourceBuilder sqlSourceBuilder = new SqlSourceBuilder(new Configuration());
SqlSource sqlSource = sqlSourceBuilder.parse(sql, Author.class, new HashMap<>());
BoundSql boundSql = sqlSource.getBoundSql(new Author());
System.out.println(String.format("SQL: %s\n", boundSql.getSql()));
System.out.println(String.format("ParameterMappings: %s", boundSql.getParameterMappings()));
}
private Query getQuery(MappedStatement mappedStatement) throws NoSuchFieldException, IllegalAccessException {
SqlSource sqlSource = mappedStatement.getSqlSource();
if (!(sqlSource instanceof DynamicSqlSource)) {
return new Query(mappedStatement.getBoundSql(null).getSql());
}
// 動的クエリ(XMLで組み立てるもの)をエミュレート
DynamicSqlSource dynamicSqlSource = (DynamicSqlSource) sqlSource;
Field rootSqlNode = DynamicSqlSource.class.getDeclaredField("rootSqlNode");
rootSqlNode.setAccessible(true);
SqlNode sqlNode = (SqlNode) rootSqlNode.get(dynamicSqlSource);
if (sqlNode instanceof MixedSqlNode) {
StringBuilder sql = new StringBuilder();
MixedSqlNode mixedSqlNode = (MixedSqlNode) sqlNode;
Field contents = mixedSqlNode.getClass().getDeclaredField("contents");
contents.setAccessible(true);
List list = (List) contents.get(mixedSqlNode);
for (Object content : list) {
if (content instanceof StaticTextSqlNode) {
StaticTextSqlNode staticTextSqlNode = (StaticTextSqlNode) content;
Field text = StaticTextSqlNode.class.getDeclaredField("text");
text.setAccessible(true);
String textSql = (String) text.get(staticTextSqlNode);
sql.append(textSql);
}
}
String sqlText = sql.toString().trim();
LOGGER.debug("動的SQLの組み立てをエミュレートしました。ID={}", mappedStatement.getId());
LOGGER.debug("組み立てたSQL: [{}]", sqlText);
return new Query(sqlText);
}
return new Query(mappedStatement.getBoundSql(null).getSql());
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod selectOne = SqlMethod.SELECT_ONE;
String sqlScript = String.format(selectOne.getSql(), tableInfo.getSelectColumnSegments(true),
tableInfo.getTableName(), getWrapperScript());
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sqlScript, modelClass);
return this.addMappedStatement(mapperClass, selectOne.getMethod(), sqlSource, SqlCommandType.SELECT, String.class, null, modelClass, new NoKeyGenerator(), tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod deleteById = SqlMethod.DELETE_BY_ID;
String sqlScript = String.format(deleteById.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), "#{id}");
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sqlScript, modelClass);
return this.addMappedStatement(mapperClass, deleteById.getMethod(), sqlSource, SqlCommandType.DELETE, String.class, null, Integer.class, new NoKeyGenerator(), tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod delete = SqlMethod.DELETE;
String sqlScript = String.format(delete.getSql(), tableInfo.getTableName(), getWrapperScript());
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sqlScript, modelClass);
return this.addMappedStatement(mapperClass, delete.getMethod(), sqlSource, SqlCommandType.DELETE, String.class, null, Integer.class, new NoKeyGenerator(), tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod insert = SqlMethod.INSERT;
String sqlScript = String.format(insert.getSql(), tableInfo.getTableName(), tableInfo.getInsertColumnSegments(), tableInfo.getInsertValueSegments());
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sqlScript, modelClass);
return this.addMappedStatement(mapperClass, insert.getMethod(), sqlSource, SqlCommandType.INSERT, String.class, null, Integer.class, new NoKeyGenerator(), tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod selectList = SqlMethod.SELECT_LIST;
String sqlScript = String.format(selectList.getSql(), tableInfo.getSelectColumnSegments(true),
tableInfo.getTableName(), getWrapperScript());
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sqlScript, modelClass);
return this.addMappedStatement(mapperClass, selectList.getMethod(), sqlSource, SqlCommandType.SELECT, String.class, null, modelClass, new NoKeyGenerator(), null, tableInfo.getKeyColumn());
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod update = SqlMethod.UPDATE;
String sqlScript = String.format(update.getSql(), tableInfo.getTableName(), tableInfo.getSetSegments(), getWrapperScript());
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sqlScript, modelClass);
return this.addMappedStatement(mapperClass, update.getMethod(), sqlSource, SqlCommandType.UPDATE, String.class, null, Integer.class, new NoKeyGenerator(), tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod selectById = SqlMethod.SELECT_BY_ID;
String sqlScript = String.format(selectById.getSql(), tableInfo.getSelectColumnSegments(false),
tableInfo.getTableName(), tableInfo.getKeyColumn(), "#{id}");
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sqlScript, modelClass);
return this.addMappedStatement(mapperClass, selectById.getMethod(), sqlSource, SqlCommandType.SELECT, null, null, modelClass, new NoKeyGenerator(), tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod deleteBatchByIds = SqlMethod.DELETE_BATCH_BY_IDS;
String sqlScript = String.format(deleteBatchByIds.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), getIdsScript());
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sqlScript, modelClass);
return this.addMappedStatement(mapperClass, deleteBatchByIds.getMethod(), sqlSource, SqlCommandType.DELETE, String.class, null, Integer.class, new NoKeyGenerator(), tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod selectCount = SqlMethod.SELECT_COUNT;
String sqlScript = String.format(selectCount.getSql(), tableInfo.getTableName(), getNormalSqlSegment());
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sqlScript, modelClass);
return this.addMappedStatement(mapperClass, selectCount.getMethod(), sqlSource, SqlCommandType.SELECT, String.class, null, Integer.class, new NoKeyGenerator(), null, tableInfo.getKeyColumn());
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
String tableNameAlias = StringUtil.lowerCase(modelClass.getSimpleName());
String sql = SqlInjectorUtil.parseSql(builderAssistant, SqlCustomMethod.FIND_RELATION_PAGE, modelClass, tableInfo,
sqlWhereEntityWrapper(tableInfo, tableNameAlias));
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
return this.addSelectMappedStatementForTable(mapperClass, SqlCustomMethod.FIND_RELATION_PAGE.getMethod(), sqlSource, tableInfo);
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
String tableNameAlias = StringUtil.lowerCase(modelClass.getSimpleName());
String sql = SqlInjectorUtil.parseSql(builderAssistant, SqlCustomMethod.FIND_RELATION_LIST, modelClass, tableInfo,
sqlWhereEntityWrapper(tableInfo, tableNameAlias));
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
return this.addSelectMappedStatementForTable(mapperClass, SqlCustomMethod.FIND_RELATION_LIST.getMethod(), sqlSource, tableInfo);
}
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);
}