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. AutoFac文档3(转载)

    目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 服务类型,名称和键 同 ...

  2. with(上下文的用法)以及其他知识点

    一.上下文 class Sxw(object): def __enter__(self): '''进入''' print("你好啊") def __exit__(self, exc ...

  3. windows gvim插入当前时间

    :nnoremap <F5> "=strftime("%c")<CR>P :inoremap <F5> <C-R>=str ...

  4. UIImageView的一些用法

    转自:http://blog.sina.com.cn/s/blog_60e2dbcf01014bfm.html //初始化 UIImageView  *imageView=[[UIImageView ...

  5. location 禁止以/data开头的文件

    [root@web01 default]# tree data/ data/ └── index.html directories, file [root@web01 default]# cat da ...

  6. AngularJS 和 Electron 构建桌面应用

    译]使用 AngularJS 和 Electron 构建桌面应用 原文: Creating Desktop Applications With AngularJS and GitHub Electro ...

  7. 【转】Linux中多线程wait使用注意

    使用管道生成的while,是无法进行并发管理的 在处理日志的时候,采用管道多线程,怎么都实现不了wait功能,经上篇文章才知道,使用管道生成的while,无法进行并发管理. while read qu ...

  8. hdu6121 Build a tree 模拟

    /** 题目:hdu6121 Build a tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:n个点标号为0~n-1:节点i的父节点 ...

  9. jQuery无刷新分页完整实例代码

    在线演示地址如下: http://demo.jb51.net/js/2015/jquery-wsx-page-style-demo/ <!DOCTYPE html> <head> ...

  10. JavaScript实现网页安全登录(转)

    现在很多商业网站的用户登录都是明码传输的,而一般用户又习惯于所有帐号使用相同的密码来保存,甚至很多人使用的密码和自己的银行帐号都一样哦!所 以嘛还是有一定的安全隐患的,YAHOO的免费邮箱登录使用了M ...