想请教下,表名不固定的时候应该怎么办
luozongle opened this issue · 7 comments
luozongle commented
我们现在经常会遇到表的数量不固定,但是表结构是相同的。比如我们有一个表A记录了所有的运行商与表名的对应关系,每有一个新运营商接入我们系统,就增加一张表,并且在表A中插入一条记录 记录了运营商的名称,对应的表名,当这个运营商登录我们的系统,操作的就是它自己的表,这种场景应该怎么办呢,目前我们是在代码里手动拼接sql,感觉好麻烦啊
zhangzhenhuajack commented
zhangzhenhuajack commented
zhangzhenhuajack commented
zhangzhenhuajack commented
zhangzhenhuajack commented
貌似这种种做法行不通
zhangzhenhuajack commented
public class AutoTableName extends EmptyInterceptor {
private String targetName;// 目标表名
private String tableName;// 操作表名
public AutoTableName() {}
public AutoTableName(String targetName,String tableName) {
this.targetName= targetName;
this.tableName= tableName;
}
@Override
public String onPrepareStatement(String sql) {
sql = sql.replaceAll(targetName, tableName);
return sql;
}
}
zhangzhenhuajack commented
public class TNamingStrategy implements NamingStrategy{
@Override
public String classToTableName(String className) {
// TODO Auto-generated method stub
return tableName(StringHelper.unqualify(className).toUpperCase());
}
@Override
public String columnName(String columnName) {
// TODO Auto-generated method stub
return columnName;
}
@Override
public String propertyToColumnName(String propertyName) {
// TODO Auto-generated method stub
return propertyName.toUpperCase();
}
@Override
public String propertyToTableName(String className, String propertyName) {
// TODO Auto-generated method stub
return classToTableName(className) + '_' + propertyToColumnName(propertyName);
}
@Override
public String tableName(String tableName) {
// TODO Auto-generated method stub
return "T_"+tableName;
}
}