ibatis+spring 对clob的操作

applicationContext:
   <?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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
     <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
     <property name="username" value="test"/>
     <property name="password" value="test"/>
</bean>
<!-- 事务的一些配置 -->
<bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"></property>
</bean>
<!-- clob的字段的一些处理 -->
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler"
lazy-init="true">
</bean>
<!-- sqlmapClient的工厂类 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
     <property name="configLocation" value="SqlMapConfig.xml"/>
     <property name="dataSource" ref="dataSource"/>
     <property name="lobHandler" ref="lobHandler"></property>
</bean>
<!-- dao的一些配置
<bean id="UserDao" class="Dao.UserDaoImpl">
   <property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>    -->
</beans>     


SqlMapCofig

   <?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 
    cacheModelsEnabled="true"
    enhancementEnabled="true"
    lazyLoadingEnabled="true"
    maxRequests="32"
    maxSessions="10"
    maxTransactions="5"
    useStatementNamespaces="false"
/>

<typeHandler javaType="java.lang.String" jdbcType="CLOB"
             callback="org.springframework.orm.ibatis.support.ClobStringTypeHandler"/>

sqlMap

  <?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>
    <parameterMap class="entity.User" id="UserInsert">
     <parameter property="name" jdbcType="VARCHAR" javaType="java.lang.String"/>
     <parameter property="ms" jdbcType="Clob" javaType="java.lang.String"/>
    </parameterMap>

<insert id="insertUser"  parameterMap="UserInsert"> 
    insert into users (name, ms) 
    values ( ?, ? ) 
</insert> 
  </sqlMap>

Templateuser类
  
   public class TemplateUser {

private static SqlMapClientTemplate sqlMap;
private static DataSourceTransactionManager TRANSACTION_MANAGER;

static{
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("resource/applicationContext.xml");
//ApplicationContext ctx = new FileSystemXmlApplicationContext( new BaseProperties().getBasePropertiesPath() + "\\xml\\ibatis\\Ibatis-Context.xml" );
DataSource ds = (DataSource)ctx.getBean("dataSource");
SqlMapClient sqlMapClient = (SqlMapClient)ctx.getBean("sqlMapClient");
sqlMap = new SqlMapClientTemplate(ds,sqlMapClient);
            TRANSACTION_MANAGER =  (DataSourceTransactionManager) ctx.getBean("transcationManager");
                                                                             
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public static SqlMapClientTemplate getTemplate() throws DataAccessException{
return sqlMap;
}

    public static DataSourceTransactionManager getTransactionManager() {
         return TRANSACTION_MANAGER;
    }

    public static void init(){}
}



  Dao实现类
 
   public class UserDaoImpl implements UserDao {

private SqlMapClientTemplate myTemplate = TemplateUser.getTemplate();

public void add(User user) throws SQLException {
//getSqlMapClientTemplate().insert("insertUser", user);
myTemplate.insert("insertUser", user);
}

你可能感兴趣的:(spring,ibatis)