hibernate操作大文本字段Blob和Clob解决方案:

1.大文本字段Blob和Clob(流);

2.截串存取

第一步: 创建新表:Elec_CommonMsg_Content

create table Elec_CommonMsg_Content(comID varchar(50) not null primary key comment '主键ID',type char(2) null comment '判断站点运行和设备运行的标示',content varchar(5000) null comment '数据内容',orderby int null comment '数据显示排序'
)

第二步:在elec.domain中创建ElecCommonMsgContent的javabean和映射文件

package com.elec.domain;import java.io.Serializable;@SuppressWarnings("serial")
public class ElecCommonMsgContent implements Serializable{private String comID;//主键IDprivate String type;//判断站点和设备运行的标示private String content;//数据内容private Integer orderby;//数据显示排序public String getComID() {return comID;}public void setComID(String comID) {this.comID = comID;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public Integer getOrderby() {return orderby;}public void setOrderby(Integer orderby) {this.orderby = orderby;}}

 映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping><class name="com.elec.domain.ElecCommonMsgContent" table="elec_commonmsg_content"><id name="comID" type="string" column="comID"><generator class="uuid"></generator>            </id><property name="type" type="string" column="type"></property><property name="content" type="string" column="content"></property><property name="orderby" type="integer" column="orderby"></property></class>
</hibernate-mapping>

在hibernate中添加该映射文件:

    <!-- 添加映射 --><mapping resource="com/elec/domain/ElecText.hbm.xml"/><mapping resource="com/elec/domain/ElecCommonMsg.hbm.xml"/><mapping resource="com/elec/domain/ElecCommonMsgContent.hbm.xml"/>

第三步:创建两个Dao

IElecCommonMsgContentDao.java

package com.elec.dao;import com.elec.domain.ElecCommonMsgContent;public interface IElecCommonMsgContentDao extends ICommonDao<ElecCommonMsgContent>{public static final String SERVICE_NAME = "com.elec.dao.impl.ElecCommonMsgContentDaoImpl";}

ElecCommonMsgContentDaoImpl.java

package com.elec.dao.impl;import org.springframework.stereotype.Repository;import com.elec.dao.IElecCommonMsgContentDao;
import com.elec.dao.IElecCommonMsgDao;
import com.elec.domain.ElecCommonMsg;
import com.elec.domain.ElecCommonMsgContent;
/*** @Repository() == <bean id="" class="">* @author kj**/
@Repository(IElecCommonMsgContentDao.SERVICE_NAME)
public class ElecCommonMsgContentDaoImpl extends CommonDaoImpl<ElecCommonMsgContent> implements IElecCommonMsgContentDao{}

第四步:创建分割文本字符串的方法:StringUtil.java

package com.elec.web.utils;import java.util.ArrayList;
import java.util.List;public class StringUtil {
/*** * @param wholecontent:传递文本字符串* @param count :需要分隔符的字符串的长度* @return 分割后的List集合,    存放结果集*/public static List<String> getContentByList(String wholeContent, int cutCount){List<String> list = new ArrayList<>();//获取完整内容字符串的总长度int contentlen = wholeContent.length();//内容截取,用内容总长度和截取长度进行比较,无需截取的话就直接插入if(contentlen < cutCount){list.add(wholeContent);}else{//定义并初始化内容String contentPart = "";//定义并初始化被截取的段落数量int contentRound = 0;//开始位置int beginCount = 0;//判断截取的段落数int contentCutPart = contentlen/cutCount;int contentCutParts = contentlen%cutCount;//求余数//如果余数为零,说明被整除,内容的长度正好是截取长度的倍数if(contentCutParts == 0){contentRound = contentCutPart;}else{contentRound = contentCutPart + 1;}//循环截取内容for(int i = 1; i <= contentRound; i++){//如果不是最后一个截取内容if(i != contentRound){//按照截断长度截取内容contentPart = wholeContent.substring(beginCount, cutCount*i    );}else{//截取最后一部分内容contentPart =     wholeContent.substring(beginCount,contentlen);}//赋值下一个截取部分的起点位置beginCount = cutCount * i;list.add(contentPart);}}return list;}
}

第四步:修改对应的service文件:

ElecCommonMsgServiceImpl.java

package com.elec.service.impl;import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;import javax.annotation.Resource;import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import com.elec.dao.IElecCommonMsgContentDao;
import com.elec.dao.IElecCommonMsgDao;
import com.elec.domain.ElecCommonMsg;
import com.elec.domain.ElecCommonMsgContent;
import com.elec.service.IElecCommonMsgService;
import com.elec.web.utils.StringUtil;/*** Service *相当于在spring容器中定义:*<bean id > </bean>* @author kj**/
@Service(IElecCommonMsgService.SERVICE_NAME)
@Transactional(readOnly=true)
public class ElecCommonMsgServiceImpl implements IElecCommonMsgService {
//运行监控表Dao@Resource(name=IElecCommonMsgDao.SERVICE_NAME)IElecCommonMsgDao elecCommonMsgDao;//运行监控数据表Dao@Resource(name=IElecCommonMsgContentDao.SERVICE_NAME)IElecCommonMsgContentDao elecCommonMsgContentDao;@Override@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)public void saveElecCommonMsg(ElecCommonMsg elecCommonMsg) {List<ElecCommonMsgContent> contentList = elecCommonMsgContentDao.findCollectionByConditionNoPage("", null, null);elecCommonMsgContentDao.deleteObjectByCollection(contentList);//从页面获取站点运行情况String stationRun = elecCommonMsg.getStationRun();String devRun = elecCommonMsg.getDevRun();//调用StringUtil方法,分割字符串List<String>  stationList = StringUtil.getContentByList(stationRun, 50);if(stationList != null && stationList.size() > 0){for(int i=0;i<stationList.size();i++){ElecCommonMsgContent elecCommonMsgContent = new ElecCommonMsgContent();elecCommonMsgContent.setType("1");elecCommonMsgContent.setContent(stationList.get(i));elecCommonMsgContent.setOrderby(i + 1);elecCommonMsgContentDao.save(elecCommonMsgContent);}}List<String> devList = StringUtil.getContentByList(devRun, 50);if(devList != null && devList.size() > 0){for(int i = 0;i<devList.size();i++){ElecCommonMsgContent elecCommonMsgContent = new ElecCommonMsgContent();elecCommonMsgContent.setType("2");elecCommonMsgContent.setContent(devList.get(i));elecCommonMsgContent.setOrderby(i + 1);elecCommonMsgContentDao.save(elecCommonMsgContent);}}}@Overridepublic void updateElecCommonMsg(ElecCommonMsg elecCommonMsg) {// TODO Auto-generated method stub
        }@Overridepublic ElecCommonMsg findCommonMsg() {List<ElecCommonMsg> list = elecCommonMsgDao.findCollectionByConditionNoPage("", null, null);ElecCommonMsg commonMsg = null;if(list != null && list.size() > 0){commonMsg = list.get(0);//获取数据内容,以类型为条件,按照显示顺序升序排列,查询站点运行情况String stationCondition = " and o.type=? ";Object [] stationParams = {"1"};Map<String,String> stationOrderBy = new LinkedHashMap<String,String>();stationOrderBy.put("o.orderby", "asc");List<ElecCommonMsgContent> stationList = elecCommonMsgContentDao.findCollectionByConditionNoPage(stationCondition, stationParams, stationOrderBy);//获取返回的数据String stationContent = "";if(stationList != null && stationList.size() > 0){for(ElecCommonMsgContent elecCommonMsgContent:stationList){String content = elecCommonMsgContent.getContent();stationContent += content;}}//    将数据赋值给页面的属性
            commonMsg.setStationRun(stationContent);String devCondition = " and o.type=? ";Object [] devParams = {"2"};Map<String,String> devOrderBy = new LinkedHashMap<String,String>();stationOrderBy.put("o.orderby", "asc");List<ElecCommonMsgContent> devList = elecCommonMsgContentDao.findCollectionByConditionNoPage(devCondition, devParams, devOrderBy);//获取返回的数据String devContent = "";if(devList != null && devList.size() > 0){for(ElecCommonMsgContent elecCommonMsgContent:devList){String content = elecCommonMsgContent.getContent();devContent += content;}}//    将数据赋值给页面的属性
            commonMsg.setDevRun(devContent);}return commonMsg;}
}

  

转载于:https://www.cnblogs.com/taiguyiba/p/6897137.html

panzer 电力项目十一--hibernate操作大文本字段Blob和Clob相关推荐

  1. mysql中的字段 TEXT类型区别、用于存储比较大文本字段

    TEXT :一个BLOB或TEXT列,最大长度为65535(2^16-1)个字符. MEDIUMTEXT 一个BLOB或TEXT列,最大长度为16777215(2^24-1)个 LONGTEXT 一个 ...

  2. mysql储存大文本_mysql 的大文本存储TEXT BLOB

    TEXT & BLOB 一般在保存少量字符串的时候,我们会选择 CHAR 或者 VARCHAR:而在保存较大文本时, 通常会选择使用 TEXT 或者 BLOB,二者之间的主要差别是 BLOB ...

  3. Django项目实践3 - Django模型(字段、数据库操作及模型继承)

    http://blog.csdn.net/pipisorry/article/details/45725953 Django数据库字段类型(Field types) AutoField class A ...

  4. mybatis+oracle存储大文本类型

    业务需求存大文本字段,不能使用NVARCHAR2 默认2000字符,使用CLOB字段,表结构 CREATE TABLE "wait_authorization" ( "i ...

  5. (翻译)文本字段的最优显示方式

      文本字段是表单中请求用户输入信息的最常用组件,它们有各种尺寸.形状和样式.有这么多的选项可供选择,能得到最优可用性的文本字段的显示方式是什么? 强烈的视觉提示   最优方法是展示文本字段的功能及与 ...

  6. 项目一 国家电力项目思路总结

    Day01 项目框架(SSH) 1.项目介绍 2.项目框架(SSH) 第一步:创建数据库 第二步:创建项目(导入jar包SSH) 第三步:持久层 (1)在cn.itheima.domain中创建Ele ...

  7. 国家电力项目思路总结

    Day01 项目框架(SSH)  1.项目介绍  2.项目框架(SSH)  第一步:创建数据库  第二步:创建项目(导入jar包SSH)  第三步:持久层  (1)在cn.itheima.domain ...

  8. 网络云存储技术Windows server 2012 (项目十一 NAS服务器磁盘配额的配置与管理)

    网络云存储技术Windows server 2012 (项目十一 NAS服务器磁盘配额的配置与管理) 目录 前言 一.项目背景 二.项目实训题 三.环境准备 四.操作步骤 前言 网络存储技术,是以互联 ...

  9. 数据库相关(JDBC,存储过程,以及大文本数据处理,mvc设计模式)

    目录 1.jdbc总结(模板.八股文): 2.CallableStatement:调用 存储过程.存储函数 3.1JDBC调用存储过程的步骤: 3.2调存储函数: 3.处理CLOB/BLOB类型 4. ...

最新文章

  1. IASetIndexBuffer Offset
  2. 事务隔离级别IsolationLevel
  3. Netty通信框架Java实现小记
  4. easyUI的引用方式
  5. Bash中的文件名匹配
  6. 【搜索引擎基础知识1】搜索引擎基本架构
  7. oracle虚拟机怎么装系统,Virtualbox怎么安装系统 VirtualBox虚拟机安装Win8系统教程 (3)...
  8. Taro+react开发(53) Taro提示操作
  9. Spring.Net学习笔记九(自定义对象行为)
  10. CVPR 2021 论文开放下载了!
  11. Java基础学习总结(99)——Java代码性能优化总结
  12. go kegg_玩转GO和KEGG富集因子图的N种姿势: 3种数据处理(含在线筛选条目),3种排序方式,本地交互图片...
  13. 交互式SHELL脚本对话框(whiptail)
  14. Java完全参考手册笔记1
  15. linux服务器安装杀毒软件
  16. 程序质量:代码静态检查
  17. 计算机辅助制造系统英文,计算机辅助集成制造系统,computer-aided integrated manufacturing system,音标,读音,翻译,英文例句,英语词典...
  18. MySQL8 NDB Cluster安装部署
  19. MySQL-5.5.32 配置文件优化详解
  20. [Photography] 新摄影笔记

热门文章

  1. 不修改代码就能优化ASP.NET网站性能的一些方法
  2. laravel生成php代码,laravel代码生成器
  3. php fpm core,在php-fpm下,服务器间歇出现core dump 追踪到php代码是include一个php文件...
  4. 广度优先搜索生成树怎么画_LeetCode0938: 二叉搜索树的范围和
  5. linux 内网文件传输工具_不管你是新手PHP程序员还是大佬都要知道的PHP十大必备工具...
  6. java 多线程数据分发_使用Java多线程实现任务分发
  7. uni-app微信小程序登录授权
  8. mysql内外三种连接_mysql之内连接,外连接(左连接,右连接),union,union all的区别...
  9. Mybatis foreach 性能问题
  10. sqlserver调用mysql存储过程_sqlserver调用存储过程