使用IBatisNet对NText类型的字段插入超长文本

 

使用IBatisNet当对NText类型的字段插入超长的文本时,报错误"当前命令发生了严重错误。应放弃任何可能产生的结果"

查了一下资料,当使用命名参数式sql语句时,对于NText类型的参数,如指定length,则可避免上面的问题,
但IBatisNet的resultMap的property里只有dbType而没有length,不知为何不提供? NHibernate里就有类似length的属性。

不过property里提供了一个typeHandler,typeHandler是一个Type处理的扩展点,下面我们来看看如何使用它。

1. 类别处理实现
 public class NTextTypeHandler : ITypeHandlerCallback
 {
  #region ITypeHandlerCallback 成员

  public object ValueOf(string s)
  {
   return s;
  }

  public object GetResult(IResultGetter getter)
  {
   if(getter!=null)
    return getter.Value.ToString();
   return "";
  }

  public void SetParameter(IParameterSetter setter, object parameter)
  {
   if(parameter==null)
   {
    setter.Value="";
   }
   else
   {
    // 设置Parameter的其它属性,本例为size.
    ((SqlParameter)setter.DataParameter).Size = int.MaxValue;
    setter.Value = parameter;
   }
  }

  #endregion
 }

2. resultMap定义
  <resultMap id="InfoResult" class="Info">
   <result property="InfoID" column="infoId"/>
   <result property="Title" column="title"/>
   <result property="Author" column="author"/>
   <result property="Keywords" column="keywords" />
   <result property="PubDate" column="pubDate"/>
   <result property="Content" column="content" dbtype="NText" typeHandler="NTextTypeHandler" />
  </resultMap>

3. statement定义
  <insert id="InsertInfo" parameterClass="Info">
   INSERT INTO infos (title, author, pubDate, keywords, content)
    VALUES (#Title#, #Author#, #PubDate#, #Keywords#, #Content,handler=NTextTypeHandler#)
   <selectKey resultClass="int" type="post" property="InfoID">
    select SCOPE_IDENTITY()
   </selectKey>
  </insert>

在sql语句里一定要用 #Content, handler=xxx# 的方式来指定,如不指定则在resultMap里的设置是不起作用的,感觉这两片定义应该有个地方是多余的,等有时间再说了。

总结,IBatisNet提供的TypeHandler提供了一个灵活且简单的类型处理机制。

<script src="http://www.cchensoft.com/gad/blog_csdn_article.js" type=text/javascript></script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值