This commit is contained in:
miemie 2024-10-22 14:50:44 +08:00
parent e1b68f53cb
commit 2fae817229
5 changed files with 60 additions and 11 deletions

View File

@ -90,6 +90,7 @@ public abstract class SqlUtils implements Constants {
return sql;
}
@SuppressWarnings("all")
public static String getSelectBody(String tableName, String alisa, String asAlisa, String escapeSymbol) {
TableInfo tableInfo = TableInfoHelper.getTableInfo(tableName);
Assert.notNull(tableInfo, "can not find TableInfo Cache by \"%s\"", tableName);
@ -108,14 +109,17 @@ public abstract class SqlUtils implements Constants {
final String sa = alisa.concat(DOT);
if (asA) {
int as = body.indexOf(AS);
String column;
String property;
if (as < 0) {
sb.append(sa).append(body).append(AS).append(escapeColumn(asAlisa.concat(DOT).concat(body), escapeSymbol));
column = body;
property = StringUtils.getTargetColumn(body);
} else {
String column = body.substring(0, as);
String property = body.substring(as + 4);
column = body.substring(0, as);
property = body.substring(as + 4);
property = StringUtils.getTargetColumn(property);
sb.append(sa).append(column).append(AS).append(escapeColumn(asAlisa.concat(DOT).concat(property), escapeSymbol));
}
sb.append(sa).append(column).append(AS).append(escapeColumn(asAlisa.concat(DOT).concat(property), escapeSymbol));
} else {
sb.append(sa).append(body);
}

View File

@ -1,5 +1,6 @@
package com.baomidou.mybatisplus.test.replaceplaceholder;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.io.Serializable;
@ -14,5 +15,15 @@ public class Entity implements Serializable {
private Long id;
@TableField("`name`")
private String name;
@TableField(exist = false)
private EntitySub es;
@Data
public static class EntitySub {
private Long id;
private String name;
}
}

View File

@ -14,6 +14,6 @@ public interface EntityMapper extends BaseMapper<Entity> {
@Select("select {@entity} from entity")
List<Entity> selectAll();
@Select("select {@entity:e} from entity e")
@Select("select {@entity:e:es} from entity e")
List<Entity> selectAll2();
}

View File

@ -0,0 +1,19 @@
package com.baomidou.mybatisplus.test.replaceplaceholder;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @author miemie
* @since 2020-06-23
*/
public interface EntitySubMapper extends BaseMapper<Entity> {
@Select("select {@entity} from entity")
List<Entity> selectAll();
@Select("select {@entity:e} from entity e")
List<Entity> selectAll2();
}

View File

@ -10,6 +10,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author miemie
* @since 2020-06-23
@ -19,15 +21,17 @@ public class ReplacePlaceholderTest extends BaseDbTest<EntityMapper> {
@Test
void replace() {
doTest(i -> {
i.selectAll();
i.selectAll2();
System.out.println(i.selectAll());
List<Entity> list = i.selectAll2();
System.out.println(list);
assertThat(list.getFirst().getEs().getName()).isNotBlank();
});
}
@Override
protected List<Interceptor> interceptors() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new ReplacePlaceholderInnerInterceptor());
interceptor.addInnerInterceptor(new ReplacePlaceholderInnerInterceptor("\""));
return Collections.singletonList(interceptor);
}
@ -41,14 +45,25 @@ public class ReplacePlaceholderTest extends BaseDbTest<EntityMapper> {
@Override
protected String tableDataSql() {
return "insert into entity(id,name) values(1,'1'),(2,'2');";
return "insert into entity(id,name) values(1,'1'),(2,'2');" +
"insert into entity_sub(id,name) values(1,'1'),(2,'2');";
}
@Override
protected List<String> tableSql() {
return Arrays.asList("drop table if exists entity", "CREATE TABLE IF NOT EXISTS entity (" +
return Arrays.asList("drop table if exists entity","drop table if exists entity_sub",
"CREATE TABLE IF NOT EXISTS entity (" +
"id BIGINT NOT NULL," +
"name VARCHAR(30) NULL DEFAULT NULL," +
"PRIMARY KEY (id))");
"PRIMARY KEY (id))",
"CREATE TABLE IF NOT EXISTS entity_sub (" +
"id BIGINT NOT NULL," +
"name VARCHAR(30) NULL DEFAULT NULL," +
"PRIMARY KEY (id))");
}
@Override
protected List<Class<?>> otherMapper() {
return List.of(EntitySubMapper.class);
}
}