2017-03-31 19:28
没有异常。就是getSql 返回空,就崩溃了。我刚才去掉了自动绑定,是可以的成功的
public static void mapping(ActiveRecordPlugin arp) {
arp.addMapping("advert_startpage", "id", AdvertStartpage.class);
arp.addMapping("advert_slide", "id", AdvertSlide.class);
}
如果使用自动绑定 就有了问题。不知道问题在哪来,麻烦指导一下,谢谢!
;/***
* 自动绑定model与数据库表
*/
public class AutoTableBindPlugin extends ActiveRecordPlugin {
private TableNameStyle tableNameStyle;
public AutoTableBindPlugin(DataSource dataSource) {
super(dataSource);
}
public AutoTableBindPlugin(IDataSourceProvider dataSourceProvider, TableNameStyle tableNameStyle) {
super(dataSourceProvider);
this.tableNameStyle = tableNameStyle;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public boolean start() {
try {
List modelClasses = ClassKit.findClasses(BaseModel.class);
TableBind tb = null;
for (Class modelClass : modelClasses) {
tb = (TableBind) modelClass.getAnnotation(TableBind.class);
if (tb == null) {
this.addMapping(tableName(modelClass), modelClass);
} else {
if (StrKit.notBlank(tb.name())) {
if (StrKit.notBlank(tb.pk())) {
this.addMapping(tb.name(), tb.pk(), modelClass);
} else {
this.addMapping(tb.name(), modelClass);
}
} else {
if (StrKit.notBlank(tb.pk())) {
this.addMapping(tableName(modelClass), tb.pk(), modelClass);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return super.start();
}
@Override
public boolean stop() {
return super.stop();
}
// 根据类名生成Table名
private String tableName(Class clazz) {
String tableName = clazz.getSimpleName();
if (tableNameStyle == TableNameStyle.UP) {
tableName = tableName.toUpperCase();
} else if (tableNameStyle == TableNameStyle.LOWER) {
tableName = tableName.toLowerCase();
} else if (tableNameStyle == TableNameStyle.LOWER_UNDERLINE) {
tableName = tableName.replaceAll("[A-Z]", "_$0");
tableName = tableName.substring(1, tableName.length()).toLowerCase();
} else {
tableName = StrKit.firstCharToLowerCase(tableName);
}
String tablePrefix = ConfigKit.get("jdbc.dbPrefix");
if (!StrKit.isBlank(tablePrefix))
tableName = tablePrefix + tableName;
System.out.println("DATABASE_TABLE_MAPPING---->" + clazz.getName() + "=>" + tableName);
return tableName;
}
}