Skip to content

Commit

Permalink
code
Browse files Browse the repository at this point in the history
  • Loading branch information
miemieYaho committed Oct 22, 2024
1 parent e1b68f5 commit 2fae817
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static String replaceSqlPlaceholder(String sql, List<String> placeHolder,
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);
Expand All @@ -108,14 +109,17 @@ public static String getNewSelectBody(String selectBody, String alisa, String as
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.baomidou.mybatisplus.test.replaceplaceholder;

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;

import java.io.Serializable;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author miemie
* @since 2020-06-23
Expand All @@ -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);
}

Expand All @@ -41,14 +45,25 @@ protected List<Interceptor> interceptors() {

@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);
}
}

0 comments on commit 2fae817

Please sign in to comment.