Browse Source

添加 JavaType 映射定制

James 4 years ago
parent
commit
b125d2025c

+ 31 - 1
src/main/java/com/jfinal/plugin/activerecord/ActiveRecordPlugin.java

@@ -34,6 +34,8 @@ import com.jfinal.plugin.activerecord.sql.SqlKit;
  */
 public class ActiveRecordPlugin implements IPlugin {
 	
+	protected TableBuilder tableBuilder = new TableBuilder();
+	
 	protected IDataSourceProvider dataSourceProvider = null;
 	protected Boolean devMode = null;
 	
@@ -223,7 +225,7 @@ public class ActiveRecordPlugin implements IPlugin {
 		
 		config.sqlKit.parseSqlTemplate();
 		
-		new TableBuilder().build(tableList, config);
+		tableBuilder.build(tableList, config);
 		DbKit.addConfig(config);
 		isStarted = true;
 		return true;
@@ -300,6 +302,34 @@ public class ActiveRecordPlugin implements IPlugin {
 	public Config getConfig() {
 		return config;
 	}
+	
+	/**
+	 * 一般用于配置 TableBuilder 内的 JavaType
+	 * <pre>
+	 * 例如:
+	 *    ActiveRecordPlugin arp = ...;
+	 *    JavaType jt = arp.getTableBuilder().getJavaType();
+	 *    
+	 *    jt.addType(org.postgresql.geometric.PGpoint.class);
+	 *    jt.addType(org.postgresql.geometric.PGbox.class);
+	 *    jt.addType(org.postgresql.geometric.PGcircle.class);
+	 *    jt.addType(org.postgresql.geometric.PGline.class);
+	 *    jt.addType(org.postgresql.geometric.PGlseg.class);
+	 *    jt.addType(org.postgresql.geometric.PGpath.class);
+	 *    jt.addType(org.postgresql.geometric.PGpolygon.class);
+	 * </pre>
+	 */
+	public TableBuilder getTableBuilder() {
+		return tableBuilder;
+	}
+	
+	/**
+	 * 可用于切换 TableBuilder 实现类
+	 */
+	public ActiveRecordPlugin setTableBuilder(TableBuilder tableBuilder) {
+		this.tableBuilder = tableBuilder;
+		return this;
+	}
 }
 
 

+ 16 - 0
src/main/java/com/jfinal/plugin/activerecord/JavaType.java

@@ -80,6 +80,22 @@ public class JavaType {
 	public Class<?> getType(String typeString) {
 		return strToType.get(typeString);
 	}
+	
+	public void addType(Class<?> type) {
+		strToType.put(type.getName(), type);
+	}
+	
+	public void removeType(Class<?> type) {
+		strToType.remove(type);
+	}
+	
+	public void addTypeMapping(Class<?> from, Class<?> to) {
+		strToType.put(from.getName(), to);
+	}
+	
+	public void addTypeMapping(String from, Class<?> to) {
+		strToType.put(from, to);
+	}
 }
 
 

+ 8 - 0
src/main/java/com/jfinal/plugin/activerecord/TableBuilder.java

@@ -31,6 +31,14 @@ public class TableBuilder {
 	
 	protected JavaType javaType = new JavaType();
 	
+	public JavaType getJavaType() {
+		return javaType;
+	}
+	
+	public void setJavaType(JavaType javaType) {
+		this.javaType = javaType;
+	}
+	
 	public void build(List<Table> tableList, Config config) {
 		// 支持 useAsDataTransfer(...) 中的 arp.start() 正常运作
 		if (config.dataSource instanceof NullDataSource) {