本文主要是介绍使用Spring的JdbcTemplate处理Clob,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 数据库表 req_seq
CREATE TABLE req_seq
(
req_id VARCHAR2(36) PRIMARY KEY,
seq VARCHAR2(100),
req_xml CLOB,
req_time DATE
);
2. 实体类
import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;public class ReqSeqBean implements Serializable {/*** @Fields serialVersionUID : TODO*/private static final long serialVersionUID = -4200518336372141644L;// 主键private String reqId;// 请求序列private String req;// 请求XMLprivate String reqXml;// 请求时间private Timestamp reqTime;public String toString() {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");StringBuffer buf = new StringBuffer("ReqSeqBean:[");buf.append("reqId=").append(reqId).append(", ");buf.append("req=").append(req).append(", ");buf.append("reqXml=").append(reqXml).append(", ");buf.append("reqTime=").append(df.format(reqTime));return buf.toString();}// 省略 setter/getter
}
注意: 数据库表中,REQ_XML 的类型是 CLOB,实体类中是 String
3. DAO层配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans><bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"lazy-init="true"/><bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true"><property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/></bean><bean id="reqSeqDAO" class="com.dao.ReqSeqDAO"><property name="jdbcTemplate" ref="jdbcTemplate" /><property name="hisJdbcTemplate" ref="jdbcTemplate" /><property name="lobHandler" ref="oracleLobHandler"/></bean> </beans>
4. DAO
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;public class ReqSeqDAO extends BaseDao {private static final Logger log = LoggerFactory.getLogger(ReqSeqDAO.class);private LobHandler lobHandler;/*** @Title: addReqSeq* @deprecated: 如果字段REQ_XML是clob类型,则用此方法,但是需要oracle版本和jdk版本一致* @param bean* @throws Exception* @author * @date 2013年10月23日*/public void addReqSeq(final ReqSeqBean bean) throws Exception {StringBuffer sql = new StringBuffer();sql.append("INSERT INTO REQ_SEQ(REQ_ID, SEQ, REQ_XML, REQ_TIME) ");sql.append("VALUES(SYS_GUID(), ?, ?, ");sql.append("TO_DATE(TO_CHAR(SYSDATE,'yyyy-MM-dd hh24:mi:ss'),'yyyy-MM-dd hh24:mi:ss'))");getJdbcTemplate().execute(sql.toString(),new AbstractLobCreatingPreparedStatementCallback(lobHandler) {@Overrideprotected void setValues(PreparedStatement ps,LobCreator lobCreator) throws SQLException,DataAccessException {ps.setString(1, bean.getReq());lobCreator.setClobAsString(ps, 2, bean.getReqXml());}});}/*** @Title: queryReqSeqList* @deprecated: 按条件查找请求序列信息(如果字段REQ_XML是clob类型,则用此方法,但是需要oracle版本和jdk版本一致)* @param bean* 查询条件* @return List* @throws Exception* @author * @date 2013年10月23日*/public List<ReqSeqBean> queryReqSeqList(ReqSeqBean bean) throws Exception {StringBuffer sql = new StringBuffer("SELECT * FROM REQ_SEQ WHERE 1 = 1 ");// 查询条件List<String> params = new ArrayList<String>(1);if (null != bean) {/* 添加查询条件 */if (null != bean.getReq()) {// 请求序列号sql.append(" AND SEQ = ? ");params.add(bean.getReq());}}sql.append(" ORDER BY REQ_TIME DESC");final List<ReqSeqBean> list = new ArrayList<ReqSeqBean>(10);getJdbcTemplate().query(sql.toString(), params.toArray(),new RowMapper() {@Overridepublic Object mapRow(ResultSet rs, int rowNum)throws SQLException {ReqSeqBean seq = new ReqSeqBean();seq.setReqId(rs.getString("REQ_ID"));seq.setReq(rs.getString("SEQ"));seq.setReqXml(lobHandler.getClobAsString(rs,"REQ_XML"));seq.setReqTime(rs.getTimestamp("REQ_TIME"));list.add(seq);return seq;}});return list;}public LobHandler getLobHandler() {return lobHandler;}public void setLobHandler(LobHandler lobHandler) {this.lobHandler = lobHandler;}
}
这篇关于使用Spring的JdbcTemplate处理Clob的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!