@ResponseBody處理Clob數據

數據表

create table test
(
  TASKID        NUMBER(10) not null,
  CONFIG CLOB
)

controller

	@ResponseBody
	@RequestMapping("/getTest")
	public List getTest(HttpServletRequest request, HttpServletResponse response){
		return this.Service.getTest();
	}

sql.xml

<select id="gettest"
		resultMap="hashmap">
		select t.config as config,
		       t.taskid
		from test t
	</select>


1、沒有做任何處理情況,程序報錯如下

Caused by:
org.codehaus.jackson.map.JsonMappingException: No serializer found for class
oracle.sql.LobDBAccessImpl and no properties discovered to create
BeanSerializer (to avoid exception, disable
SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain:
java.util.ArrayList[0]->java.util.HashMap["CONFIG"]->oracle.sql.CLOB["dbaccess"])

2、在sql.xml中,對CLOB字段Config進行to_char()處理,開始能夠解決問題,後面出現數據庫報錯的情況

sql.xml

<select id="gettest"
		resultMap="hashmap">
		select to_char(t.config) as config,
		       t.taskid
		from test t
	</select>

數據庫報錯

wKiom1frLtyAOSI2AABP9uv8ZX4265.png

3、去掉@ResponseBody,將返回的結果打印出來,發現config是一個對象

controller

        //@ResponseBody
	@RequestMapping("/getTest")
	public List getTest(HttpServletRequest request, HttpServletResponse response){
	        System.out.println(this.Service.getTest());
		return this.Service.getTest();
	}

debug

[{CONFIG=oracle.sql.CLOB@16e2b70,TASKID=38}]

4、在Mybatis中採用resultMap處理,程序正常,debug時config爲具體內容。

sql.xml

	<select id="gettest"
		resultMap="testMap">
		select t.config as config,
		       t.taskid
		from test t
	</select>
	
	<resultMap type="hashmap" id="testMap">
		<result property="CONFIG" column="config" javaType="String" jdbcType="CLOB"/>
		<result property="TASKID" column="taskid"/>
	</resultMap>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章