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

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

2.截串存取

第一步: 创建新表:Elec_CommonMsg_Content

create table Elec_CommonMsg_Content(
comID varchar() not null primary key comment '主键ID',
type char() null comment '判断站点运行和设备运行的标示',
content varchar() 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;//主键ID
private 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 = ;
//开始位置
int beginCount = ;
//判断截取的段落数
int contentCutPart = contentlen/cutCount;
int contentCutParts = contentlen%cutCount;//求余数
//如果余数为零,说明被整除,内容的长度正好是截取长度的倍数
if(contentCutParts == ){
contentRound = contentCutPart;
}else{
contentRound = contentCutPart + ;
}
//循环截取内容
for(int i = ; 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, );
if(stationList != null && stationList.size() > ){
for(int i=;i<stationList.size();i++){
ElecCommonMsgContent elecCommonMsgContent = new ElecCommonMsgContent();
elecCommonMsgContent.setType("");
elecCommonMsgContent.setContent(stationList.get(i));
elecCommonMsgContent.setOrderby(i + );
elecCommonMsgContentDao.save(elecCommonMsgContent);
}
}
List<String> devList = StringUtil.getContentByList(devRun, );
if(devList != null && devList.size() > ){
for(int i = ;i<devList.size();i++){
ElecCommonMsgContent elecCommonMsgContent = new ElecCommonMsgContent();
elecCommonMsgContent.setType("");
elecCommonMsgContent.setContent(devList.get(i));
elecCommonMsgContent.setOrderby(i + );
elecCommonMsgContentDao.save(elecCommonMsgContent);
}
}
}
@Override
public void updateElecCommonMsg(ElecCommonMsg elecCommonMsg) {
// TODO Auto-generated method stub }
@Override
public ElecCommonMsg findCommonMsg() {
List<ElecCommonMsg> list = elecCommonMsgDao.findCollectionByConditionNoPage("", null, null);
ElecCommonMsg commonMsg = null;
if(list != null && list.size() > ){
commonMsg = list.get();
//获取数据内容,以类型为条件,按照显示顺序升序排列,查询站点运行情况
String stationCondition = " and o.type=? ";
Object [] stationParams = {""};
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() > ){
for(ElecCommonMsgContent elecCommonMsgContent:stationList){
String content = elecCommonMsgContent.getContent();
stationContent += content;
}
}
// 将数据赋值给页面的属性
commonMsg.setStationRun(stationContent); String devCondition = " and o.type=? ";
Object [] devParams = {""};
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() > ){
for(ElecCommonMsgContent elecCommonMsgContent:devList){
String content = elecCommonMsgContent.getContent();
devContent += content;
}
}
// 将数据赋值给页面的属性
commonMsg.setDevRun(devContent);
}
return commonMsg;
}
}

  

panzer 电力项目十一--hibernate操作大文本字段Blob和Clob的更多相关文章

  1. Hibernate or JPA Annotation中BLOB、CLOB注解写法

    BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的,而CLOB是可以直接存储字符串的. 在hibernate or JPA Annotation中,实体BLOB.CLOB类型的注解与普 ...

  2. 电力项目十一--js添加浮动框

    1.添加浮动窗口样式 <!-- 浮动窗口样式css begin --> <style type="text/css"> #msg_win{border:1p ...

  3. 利用JDBC处理mysql大数据--大文本和二进制文件等

    转载自http://www.cnblogs.com/xdp-gacl/p/3982581.html 一.基本概念 大数据也称之为LOB(Large Objects),LOB又分为:clob和blob, ...

  4. 使用JDBC处理MySQL大文本和大数据

    LOB,Large Objects,是一种用于存储大对象的数据类型,一般LOB又分为BLOB与CLOB.BLOB通常用于存储二进制数据,比如图片.音频.视频等.CLOB通常用于存储大文本,比如小说. ...

  5. mysql 的大文本存储TEXT & BLOB

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

  6. JDBC中级篇(MYSQL)——处理大文本(CLOB)

    注意:其中的JdbcUtil是我自定义的连接工具类:代码例子链接: package b_blob_clob; import java.io.FileNotFoundException; import ...

  7. Oracle中Blob和Clob类型的区别与操作

    Oracle中Blob和Clob类型 1.Oracle中Blob和Clob类型的区别 BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的 ...

  8. SSH电力项目一 搭建Hibernate框架

    Hibernate所需要的基本文件: ElectText.java ElecText.hbm.xml hibernate.cfg.xml 第一步:创建测试表Elec_Text: create tabl ...

  9. SSH电力项目

    第一步:创建测试表Elec_Text: create table Elec_Text(textID varchar(50) not null primary key,textName varchar( ...

随机推荐

  1. unity, switch platform

    例如一开始是iPhone, iPod Touch and iPad,如图: 想切换成PC, Mac & Linux Standalone,如图: 方法是File->Build Setti ...

  2. java Arrays对数组操作

    Arrays.sort(Array)对数组排序 public static void main(String[] args) throws IOException { int[] a = {1,3,9 ...

  3. vector常见用法

    #include <boost/foreach.hpp> #include <iostream> #include <vector> #include <bo ...

  4. 8086汇编之 CALL 和 RET指令

    Ret 和 call 也是转移指令,可是他们跟jmp不同的是,这两个转移指令都跟栈有关系. <1> ret 用栈中的数据改动IP的地址,从而实现近转移 ( ip ) = ( (ss)*16 ...

  5. 32位嵌入式微处理器(processor)一览

    32位嵌入式微处理器(processor)一览 由于嵌入式系统的专用型与定制性,与全球PC市场不同,没有一种微处理器或者微处理器公司可以主导嵌入式系统.本文分析了当前市场上主流的一些32位嵌入式微处理 ...

  6. [svc]entrypoint.sh shell脚本解析

    最近搞influxdb绘图,看到其dockerfile的entry.sh,无奈看的不是很懂. 于是查了下.. docker run 通过传参实现配置文件覆盖 实现启动镜像时候可指定配置文件 如果不指定 ...

  7. 【Android】14.3 浏览手机中的所有文件夹和文件

    分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 前面我们了解了内部存储.外部存储的含义,用一句话说,内部存储实际上是保存在"data"文件夹下 ...

  8. Makefile 8——使用依赖关系文件

    Makefile中存在一个include指令,它的作用如同C语言中的#include预处理指令.在Makefile中,可以通过include指令将自动生成的依赖关系文件包含进来,从而使得依赖关系文件中 ...

  9. Eclipse中安装JBoss Tools插件

    1.先访问JBoss Tools网站,看看上面怎么说: http://tools.jboss.org -> 进入下载界面 看到下面这句话: Drag and drop this  icon in ...

  10. oozie4.3.0的安装与配置 + hadoop2.7.3

    安装步骤 mysql的配置 oozie的安装 oozie的配置 oozie的启动与登录 常用oozie的命令 1. mysql的配置 mysql的安装自行解决,然后在mysql上 创建oozie数据库 ...