panzer 电力项目十一--hibernate操作大文本字段Blob和Clob
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的更多相关文章
- Hibernate or JPA Annotation中BLOB、CLOB注解写法
BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的,而CLOB是可以直接存储字符串的. 在hibernate or JPA Annotation中,实体BLOB.CLOB类型的注解与普 ...
- 电力项目十一--js添加浮动框
1.添加浮动窗口样式 <!-- 浮动窗口样式css begin --> <style type="text/css"> #msg_win{border:1p ...
- 利用JDBC处理mysql大数据--大文本和二进制文件等
转载自http://www.cnblogs.com/xdp-gacl/p/3982581.html 一.基本概念 大数据也称之为LOB(Large Objects),LOB又分为:clob和blob, ...
- 使用JDBC处理MySQL大文本和大数据
LOB,Large Objects,是一种用于存储大对象的数据类型,一般LOB又分为BLOB与CLOB.BLOB通常用于存储二进制数据,比如图片.音频.视频等.CLOB通常用于存储大文本,比如小说. ...
- mysql 的大文本存储TEXT & BLOB
TEXT & BLOB 一般在保存少量字符串的时候,我们会选择 CHAR 或者 VARCHAR:而在保存较大文本时,通常会选择使用 TEXT 或者 BLOB,二者之间的主要差别是 BLOB 能 ...
- JDBC中级篇(MYSQL)——处理大文本(CLOB)
注意:其中的JdbcUtil是我自定义的连接工具类:代码例子链接: package b_blob_clob; import java.io.FileNotFoundException; import ...
- Oracle中Blob和Clob类型的区别与操作
Oracle中Blob和Clob类型 1.Oracle中Blob和Clob类型的区别 BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的 ...
- SSH电力项目一 搭建Hibernate框架
Hibernate所需要的基本文件: ElectText.java ElecText.hbm.xml hibernate.cfg.xml 第一步:创建测试表Elec_Text: create tabl ...
- SSH电力项目
第一步:创建测试表Elec_Text: create table Elec_Text(textID varchar(50) not null primary key,textName varchar( ...
随机推荐
- intelliJ IDEA 配置MySQL数据库 详解
1> 在主界面中,点击右边侧栏的 Database ,在点击 + ,再Data Source 选择数据库 2> 填入 Database 数据库名,在输入 User 和 Password ...
- Laravel Eloquent ORM--整理(未转)
转:http://blog.csdn.net/a437629292/article/details/46312695 http://www.jianshu.com/p/cb641a4f3599
- firefox因 HTTP 严格传输安全(HSTS)机制无法打开网页
1.打开about:config 2.查找: security.enterprise_roots.enabled ,默认为false,改为true就可以了 3.吐槽,firefox太极端了,这是作死.
- atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结
atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结 1. DND的操作流程 1 2. Html5 注 ...
- Atitit. 状态模式(State)attilax 总结 跟个策 略模式的区别
Atitit. 状态模式(State)attilax 总结 跟个策 略模式的区别 1. 状态模式(State)概览 1 2. 状态的维护和转换:① 在Context 中.② 在状态的处理类中.2 3. ...
- Hp && Dell服务器硬件监控
HP 安装HP工具: yum install hpssacli 1 查看控制器状态 raid卡型号等hpssacli ctrl all show status 2 查看硬盘类型.大小 raid级别.状 ...
- 偏于SQL语句的 sqlAlchemy 增删改查操作
ORM 江湖 曾几何时,程序员因为惧怕SQL而在开发的时候小心翼翼的写着sql,心中总是少不了恐慌,万一不小心sql语句出错,搞坏了数据库怎么办?又或者为了获取一些数据,什么内外左右连接,函数存储过程 ...
- JS学习笔记(5)--一道返回整数数组的面试题(经验之谈)
说明: 1. 微信文章里看到的,作者是马超 网易高级前端技术经理,原文在网上搜不到,微信里可以搜“为什么你的前端工作经验不值钱?”,里面写着“转载自网易实践者社区”.(妈蛋,第二天网上就有了http: ...
- sama5d36 OUT0-OUT3 对应关系 带光模块的系统
ARM-IO9 PA8 OUT0 ARM-IO10 PA1 OUT1 ARM-IO11 PA3 OUT2 ARM-IO12 PA9 OUT3
- 本机添加多个git仓库账号
我们可能会需要在一台电脑上以不同的github账户去使用git,这时就需要去解决如何管理本机上的多个ssh key的问题了. 生成新ssh key 如果我们电脑上已经存在了一个ssh key,那么我们 ...