@小徐同学 参考了这篇文章https://xiaoxustudent.top/p/389
不建议修改jfinal源码,可以实现一个jsonfactory,然后将代码copy过来即可
大写字段转model(这个是抄的sojpt里面的)
public class MysqlSojptMetaBuilder extends MetaBuilder { public MysqlSojptMetaBuilder(DataSource dataSource) { super(dataSource); } /** * 构造 colName 所对应的 attrName,mysql 数据库建议使用小写字段名或者驼峰字段名 * Oralce 反射将得到大写字段名,所以不建议使用驼峰命名,建议使用下划线分隔单词命名法 * * 重构此处即可以 */ protected String buildAttrName(String colName) { if (dialect instanceof OracleDialect) { colName = colName.toLowerCase(); } else if (dialect instanceof MysqlDialect) { colName = colName.toLowerCase(); } return StrKit.toCamelCase(colName); } }
gernerator.setMetaBuilder(new MysqlSojptMetaBuilder(getDataSource()));
下划画转驼峰修改jfinal 源码如下:
public class JFinalJsonExt extends JFinalJson { public String mapToJson(Map map, int depth) { if (map == null) { return "null"; } StringBuilder sb = new StringBuilder(); boolean first = true; Iterator iter = map.entrySet().iterator(); sb.append('{'); while (iter.hasNext()) { if (first) first = false; else sb.append(','); Map.Entry entry = (Map.Entry) iter.next(); toKeyValue(StrKit.toCamelCase(String.valueOf(entry.getKey())), entry.getValue(), sb, depth); } sb.append('}'); return sb.toString(); } public static JFinalJsonExt getJson() { return new JFinalJsonExt(); } }
public class MixJsonFactoryExt implements IJsonFactory { private static final MixJsonFactoryExt me = new MixJsonFactoryExt(); public static MixJsonFactoryExt me() { return me; } private static MixJsonFactoryExt.MixedJson mixedJson = new MixJsonFactoryExt.MixedJson(); public Json getJson() { return mixedJson; } private static class MixedJson extends Json { private static JFinalJsonExt jFinalJson = JFinalJsonExt.getJson(); private static FastJson fastJson = FastJson.getJson(); public String toJson(Object object) { return jFinalJson.toJson(object); } public <T> T parse(String jsonString, Class<T> type) { return fastJson.parse(jsonString, type); } } }
还是很简单,在也不用纠结了,当然转化还是有性能损失的,建议数据库用驼峰。