使用HibernateTemplate查询Clob内的内容

在项目中要对数据表的Clob字段进行检索,用于Clob是采用字节方式存储的,因此普通的查询语句不能检索Clob字段内的内容,使用SELECT * FROM 表名 WHERE dbms_lob.instr(表名.Clob字段名,'内容',1,1) > 0; 或 SELECT * FROM 表名 别名 WHERE dbms_lob.instr(别名.Clob字段名, '内容', 1, 1) > 0;语句可以检索Clob内的内容,使用Spring的HibernateTemplate执行上述语句的例子如下:所采用的数据表: Sql代码 drop table csms_ps_genericContent; create table csms_ps_genericContent ( id char(32) not null, --ID categoryId varchar2(60) not null, --类别ID featureId varchar2(60) null, --专题ID title varchar2(150) not null, --标题 titlePicPath varchar2(500) null, --标题图片路径 publishTime date not null, --发布时间 content clob not null, --内容 keyword varchar2(255) null, --关键字 isChar char(1) default '1', --是否是文字内容 primary key (id) ); drop table csms_ps_genericContent; create table csms_ps_genericContent ( id char(32) not null, --ID categoryId varchar2(60) not null, --类别ID featureId varchar2(60) null, --专题ID title varchar2(150) not null, --标题 titlePicPath varchar2(500) null, --标题图片路径 publishTime date not null, --发布时间 content clob not null, --内容 keyword varchar2(255) null, --关键字 isChar char(1) default '1', --是否是文字内容 primary key (id) ); 使用HIbernateTemplate的实现方法是: Java代码 @SuppressWarnings("unchecked") public List fullTextSearch(final String queryContent) throws Exception { final String queryString = "select * from CSMS_PS_GENERICCONTENT gc " + "where dbms_lob.instr(gc.content, :queryContent, 1, 1) > 0 or gc.title like :title"; return (List)super.execute(new HibernateCallback() { public Object doInHibernate(Session session) { SQLQuery query = session.createSQLQuery(queryString); query.addEntity("gc", GenericContentModel.class); query.setString("queryContent", queryContent); query.setString("title", new StringBuffer("%").append(queryContent).append("%").toString()); return query.list(); } }); } @SuppressWarnings("unchecked") public List fullTextSearch(final String queryContent) throws Exception { final String queryString = "select * from CSMS_PS_GENERICCONTENT gc " + "where dbms_lob.instr(gc.content, :queryContent, 1, 1) > 0 or gc.title like :title"; return (List)super.execute(new HibernateCallback() { public Object doInHibernate(Session session) { SQLQuery query = session.createSQLQuery(queryString); query.addEntity("gc", GenericContentModel.class); query.setString("queryContent", queryContent); query.setString("title", new StringBuffer("%").append(queryContent).append("%").toString()); return query.list(); } }); }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值