ibatis在处理lob字段时不是太好,所以另一种处理的方法是用jdbc:
public int addTopic(SpeechForm form) { int accessFlag = 0;
try { Connection conn = getSqlMapClientTemplate().getDataSource(). getConnection(); conn.setAutoCommit(false); PreparedStatement preStmt = conn.prepareStatement("INSERT INTO VO_TOPICTAB(TOPICID,TITLE,CONTENT,WRITERID,WRITER,FORUMID,POINTS) VALUES (?,?,?,?,?,?,?)"); preStmt.setString(1, form.getTopicID()); preStmt.setString(2, form.getTitle()); preStmt.setClob(3, oracle.sql.CLOB.empty_lob()); preStmt.setInt(4, form.getWriterID()); preStmt.setString(5, form.getWriter()); preStmt.setInt(6, form.getForumID()); preStmt.setInt(7, form.getPoints()); preStmt.executeUpdate(); preStmt.close();
preStmt = conn.prepareStatement( "select CONTENT from VO_BBSTOPICTAB where topicID=? for update"); preStmt.setString(1, form.getTopicID()); ResultSet rset = preStmt.executeQuery(); rset.next(); oracle.sql.CLOB resClob= (oracle.sql.CLOB) rset.getClob(1); resClob.putString(1, form.getContent()); preStmt = conn.prepareStatement( "update vo_bbstopictab set content=? where topicID=?"); preStmt.setClob(1, resClob); preStmt.setString(2, form.getTopicID()); preStmt.executeUpdate(); preStmt.close(); conn.commit(); // conn.close(); return accessFlag = 0;
} catch (SQLException ex1) { ex1.printStackTrace(); accessFlag = -1; } return accessFlag; }
在javaBean中,content还是定义为String类型。不用定义为java.sql.Clob类型。
读取clob字段
iBATIS的SQLMap在处理CLOB类型的数据时,在javabean中印射成String类型。这样做在进行插入的时候没问题,但在SELECT的时候没法正确处理CLOB,也就是无法正确显示CLOB类型的数据的值,显示的是“null”。
一定要在XML配置文件中使用result map才能让SQLMap正确处理CLOB。
<resultMap id="topicDetail" class="com.weihua.xwen.beans.SpeechDetailBean"> <result property="title" column="TITLE" columnIndex="1"/> <result property="content" column="content" columnIndex="2" jdbcType="CLOB" javaType="java.lang.String" /> <result property="writerID" column="writerid" columnIndex="3"/> <result property="writer" column="writer" columnIndex="4"/> <result property="speechTime" column="speechtime" columnIndex="5"/> <result property="points" column="points" columnIndex="6"/> </resultMap> <select id="getTopicDetail" parameterClass="java.lang.String" resultMap="topicDetail"> <![CDATA[ select title,content,writerid,writer,speechtime,points from ( (select title,content,writerid,writer,speechtime,points from vo_bbstopictab where topicid=#value#) union all (select title,content,writerid,writer,speechtime,points from vo_bbsreplytab where topicid=#value# ) )
]]> </select>
|