zoukankan      html  css  js  c++  java
  • panzer 电力项目十一--hibernate操作大文本字段Blob和Clob

    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;//主键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 = 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);
                }
            }
        }
        @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() > 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;
        }
    }

     

       

  • 相关阅读:
    PC端微信扫码支付和支付宝跳转支付 斧头帮
    微信支付 斧头帮
    PC端实现浏览器点击分享到QQ好友,空间,微信,微博等 斧头帮
    图片,word,Excel等附件上传 斧头帮
    java定时任务详解 斧头帮
    手机浏览器实现分享给好友或是朋友圈 斧头帮
    Java缓存EhcacheEhcache的Cache在SSM框架中的配置 斧头帮
    通过精确地址获取经纬度 斧头帮
    SpringBoot中设置自定义拦截器 斧头帮
    Visual Studio 2008创建项目(ATL)
  • 原文地址:https://www.cnblogs.com/taiguyiba/p/6897137.html
Copyright © 2011-2022 走看看