ibatis操作clob

問題主要是插入和更新,我項目中只做了一個插入的例子,更新應該也是可以的

 

1.spring的applicationContext.xml的中加入ibatis的sqlmap

  <!-- Spring提供的iBatis的SqlMap配置-->
  <bean id="sqlMapClient"  class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
    <property name="dataSource" ref="dataSource"/>
  </bean>

 

  <bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${database.driver}"></property>  
    <property name="jdbcUrl" value="${database.url}"></property>
    <property name="user" value="${database.user}"></property>
    <property name="password" value="${database.password}"></property> 
    <property name="minPoolSize" value="${database.minPoolSize}"></property>
    <property name="maxPoolSize" value="${database.maxPoolSize}"></property>
    <property name="maxIdleTime" value="${database.maxIdleTime}"></property>
  </bean>

 

2.ibatis的sqlMapConfig配置

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig    
 PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
 <!-- 使用命名空間 用於區分重名問題 -->
 <settings useStatementNamespaces="true" />
 <typeHandler jdbcType="BLOB" javaType="[B" callback="com.bussiness.download.entity.OracleBlobTypeHandlerCallback" /> //方法1
 <sqlMap resource="com/sqlmap/business/download/download.xml"/>
</sqlMapConfig>

 

3.download.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="download">
 <typeAlias alias="DownloadVo" type="com.bussiness.download.entity.DownloadVo"/>
 <typeAlias alias="blobHandler" type="com.bussiness.download.entity.OracleBlobTypeHandlerCallback"/> //方法2
          
 <!-- 檢索出下載文件 -->
 <select id="select_all" parameterClass="DownloadVo" resultClass="DownloadVo">
 <![CDATA[
  select
  id as id,
  Merchant_id as merchantId,
  filename as fileName,
  content as content,
  content_type as contentType
  from download_file_list
 ]]>
 </select>
 
 <!-- 根據ID檢索文件 -->
 <select id="select_by_id" parameterClass="java.lang.String" resultClass="DownloadVo">
 <![CDATA[
  select
  id as id,
  Merchant_id as merchantId,
  filename as fileName,
  content as content,
  content_type as contentType
  from download_file_list
  where id = #id#
 ]]>
 </select>

 <!-- 插入文件 -->//方法1
 <insert id="insert" parameterClass="DownloadVo">
  <selectKey resultClass="String" keyProperty="id">
   SELECT to_char(SEQ_DOWNLOAD_FILE_LIST.NEXTVAL,'FM0000') AS id FROM DUAL
  </selectKey>
  insert into DOWNLOAD_FILE_LIST(ID,MERCHANT_ID,FILENAME,CONTENT,CONTENT_TYPE)
  values (#id:BLOB#,#merchantId#,#fileName#,#content#,#contentType#)

  </insert>


 <!-- 插入文件 -->//方法2
 <insert id="insert" parameterClass="DownloadVo">
  <selectKey resultClass="String" keyProperty="id">
   SELECT to_char(SEQ_DOWNLOAD_FILE_LIST.NEXTVAL,'FM0000') AS id FROM DUAL
  </selectKey>
  insert into DOWNLOAD_FILE_LIST(ID,MERCHANT_ID,FILENAME,CONTENT,CONTENT_TYPE)
  values (#id:handler=blobHandler#,#merchantId#,#fileName#,#content#,#contentType#)

  </insert>


</sqlMap> 

 

PS:使用方法1的時候,將方法2註銷掉,反之依然。

       方法1:配置的是全局變量,也就是說在sqlMapConfig有多個不同的子xml文件時,配置一個,其他需要操作blob字段的時候,直接使用就可以了

       方法2:配置的是單個文件的,要是隻有一個dao需要操作blob字段,就只配置該dao對應的sqlmap就可以了。我這次用的是download.xml,所以使用了第二種方法

 

 

 4.OracleBlobTypeHandlerCallback類 ,該類是實現TypeHandlerCallback,ibatis操作clob、blob時,必須實現此類

 

import java.sql.SQLException;

import oracle.sql.BLOB;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

public class OracleBlobTypeHandlerCallback implements TypeHandlerCallback {

 public void setParameter(ParameterSetter setter, Object parameter)
   throws SQLException {
  // TODO Auto-generated method stub
  
 }

 public Object getResult(ResultGetter getter) throws SQLException {
     BLOB blob = (BLOB) getter.getClob();
     if (blob == null || blob.length() == 0) {
       return "";
     }
     else
       return blob.getBytes(1L, (int) blob.length());

 }

 public Object valueOf(String s) {
  // TODO Auto-generated method stub
  return null;
 }

}

 

5.dao的實現類DownloadDaoImpl ,操作ibatis

 

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.huateng.bussiness.download.entity.DownloadVo;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class DownloadDaoImpl extends SqlMapClientDaoSupport implements IDownloadDao {
 
 private static final String SELECT_ALL = "download.select_all";
 private static final String SELECT_BY_ID = "download.select_by_id";
 private static final String INSERT = "download.insert";
 
 public List<DownloadVo> findAllData() {
  return getSqlMapClientTemplate().queryForList(SELECT_ALL);
 }
 
 public DownloadVo findById(String id) {
  return (DownloadVo)getSqlMapClientTemplate().queryForObject(SELECT_BY_ID, id);
 }
 
 public void doInsert(List<DownloadVo> tempList) {
  final List<DownloadVo> downloadList = tempList;
  try {
   this.getSqlMapClientTemplate().execute(
     new SqlMapClientCallback<Object>() {
      public Object doInSqlMapClient(SqlMapExecutor executor)
        throws SQLException {
       executor.startBatch();
       for (DownloadVo temp : downloadList) {
        executor.insert(INSERT, temp);
       }
       executor.executeBatch();
       return null;
      }
     });
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

 

6.實體bean  DownloadVo

 

public class DownloadVo {
 private String id;
 private int merchantId;
 private byte[] content;
 private String contentType;
 private String fileName;
 
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public int getMerchantId() {
  return merchantId;
 }
 public void setMerchantId(int merchantId) {
  this.merchantId = merchantId;
 }
 public String getContentType() {
  return contentType;
 }
 public void setContentType(String contentType) {
  this.contentType = contentType;
 }
 public byte[] getContent() {
  return content;
 }
 public void setContent(byte[] content) {
  this.content = content;
 }
 public String getFileName() {
  return fileName;
 }
 public void setFileName(String fileName) {
  this.fileName = fileName;
 }
}

 

發佈了108 篇原創文章 · 獲贊 22 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章