3.2K Star 8.8K Fork 3.6K

GVPJFinal / JFinal

 / 详情

关于jfinal不能支持postgresql JSON和JSONB类型字段的问题

待办的
创建于  
2017-03-21 11:42

目前使用的是postgresql 9.4+的版本,对于NoSQL特性比较需要。
而为jfinal中的tableBuilder类为对PGobject类型无判断。

通过判断字段的类型,在自定义数据库方言类中做处理SQL:
Sql: insert into "sys_log"("id", "post_data", "log_type", "account_id", "power_code", "ip", "resp_data", "ip_info") values(?, ?::jsonb, ?, ?, ?, ?, ?::jsonb, ?::jsonb)

需要附带::json或::jsonb的转换。

目前我的做法是在TableBulder类增加判断,并对JavaType类增加java.lang.Object 类型。
代码如下:
TableBulder.class:

private void doBuild(Table table, Connection conn, Config config) throws SQLException {
		table.setColumnTypeMap(config.containerFactory.getAttrsMap());
		if (table.getPrimaryKey() == null) {
			table.setPrimaryKey(config.dialect.getDefaultPrimaryKey());
		}
		
		String sql = config.dialect.forTableBuilderDoBuild(table.getName());
		Statement stm = conn.createStatement();
		ResultSet rs = stm.executeQuery(sql);
		ResultSetMetaData rsmd = rs.getMetaData();
		System.out.println("table-->"+table.getName());
		for (int i=1; i<=rsmd.getColumnCount(); i++) {
			String colName = rsmd.getColumnName(i);
			String colClassName = rsmd.getColumnClassName(i);
			System.out.println(colName+"\t"+colClassName);

			Class<?> clazz = javaType.getType(colClassName);
			if (clazz != null) {
				table.setColumnType(colName, clazz);
			}
			else {
				int type = rsmd.getColumnType(i);
				if (type == Types.BINARY || type == Types.VARBINARY || type == Types.BLOB) {
					table.setColumnType(colName, byte[].class);
				}
				else if (type == Types.CLOB || type == Types.NCLOB) {
					table.setColumnType(colName, String.class);
				}else if (type == Types.JAVA_OBJECT) {
					table.setColumnType(colName, Object.class);
				}else {
					table.setColumnType(colName, String.class);
				}
				// core.TypeConverter
				// throw new RuntimeException("You've got new type to mapping. Please add code in " + TableBuilder.class.getName() + ". The ColumnClassName can't be mapped: " + colClassName);
			}
		}
		
		rs.close();
		stm.close();
	}

JavaType.class:

// json jsonb
		put("java.lang.Object", java.lang.Object.class);

评论 (0)

龙影 创建了任务

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(1)
5147 loyin 1578914312
Java
1
https://gitee.com/jfinal/jfinal.git
git@gitee.com:jfinal/jfinal.git
jfinal
jfinal
JFinal

搜索帮助