mysql切片库_Mysql切片保存数据

package com.cn.elec.service.impl;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cn.elec.dao.ElecRunCommonMsgDao;

import com.cn.elec.dao.IElecRunSituationDao;

import com.cn.elec.domain.ElecRunCommonMsg;

import com.cn.elec.domain.ElecRunSituation;

import com.cn.elec.service.ElecRunSituationService;

import com.cn.elec.utils.StringUtils;

@Service(ElecRunSituationService.SERVICE_NAME)

public class ElecRunSituationServiceImpl implements ElecRunSituationService {

@Resource(name=IElecRunSituationDao.DAO_NAME)

private IElecRunSituationDao dao;

@Resource(name=ElecRunCommonMsgDao.DAO_NAME)

private ElecRunCommonMsgDao msg_dao;

public void saveRunMeseage(ElecRunSituation entity) {

//1. 清空碎片表里面所有的数据

List list=msg_dao.findCollectionByConditionNoPage("", null, null);

msg_dao.deleteByCollection(list);

//2. 将站点运行情况和设备运行情况切片

String station=entity.getSituationRun();

String devRun=entity.getDevRun();

List station_list=StringUtils.getContentByList(station, 20);

List devRun_list=StringUtils.getContentByList(devRun, 20);

//3. 保存切片的数据

//保存站点的运行情况

for (int i = 0; i

ElecRunCommonMsg msgg=new ElecRunCommonMsg();

msgg.setContent(station_list.get(i));

msgg.setType("1");

msgg.setOrderby(i+1);

msg_dao.save(msgg);

}

//3. 保存切片的数据

//保存站点的运行情况

for (int i = 0; i

ElecRunCommonMsg msge=new ElecRunCommonMsg();

msge.setContent(devRun_list.get(i));

msge.setType("2");

msge.setOrderby(i+1);

msg_dao.save(msge);

}

//4. 更新或者保存ElecCommonMsg

dao.saveRunMeseage(entity);

}

public ElecRunSituation findRunMesage() {

//return dao.findRunMesage();

//1. 查询所有的切片数据(分类型查询)

String condition = " and o.type = ?";

Object[] params = new Object[]{"1"};

Map orderby = new HashMap();

orderby.put("o.orderby", "asc");

List list =

msg_dao.findCollectionByConditionNoPage(condition, params, orderby);

String station_content = ""; //站点的运行情况

for(ElecRunCommonMsg content : list)

{

station_content = station_content + content.getContent();

}

//查询设备的运行情况

condition = " and o.type = ?";

params = new Object[]{"2"};

orderby = new HashMap();

orderby.put("o.orderby", "asc");

list =

msg_dao.findCollectionByConditionNoPage(condition, params, orderby);

String dev_content = ""; //设备的运行情况

for(ElecRunCommonMsg content : list)

{

dev_content = dev_content + content.getContent();

}

ElecRunSituation msg =dao.findRunMesage();

if(null != msg)

{

//3. 设置站点的运行情况和设备的运行情况

msg.setDevRun(dev_content);

msg.setSituationRun(station_content);

return msg;

}

return null;

}

}

字符串的工具类

package com.cn.elec.utils;

import java.util.ArrayList;

import java.util.List;

public class StringUtils {

/**

* @param wholecontent:传递的文本字符串

* @param cutcount:需要分割的字符串的长度

* @return,分割后的List集合,存放结果集

*/

public static List getContentByList(String wholecontent,int cutcontent){

List list=new ArrayList();

//获取完整内容字符串的总长度

int contentlen = wholecontent.length();

System.out.println(contentlen);

//内容截取,用内容总长和截取长度进行比较,无须截取的话直接插入

if (contentlen

list.add(wholecontent);

}

//内容长度超过截取长度

else{

//定义并初始化内容段落

String contentpart="";

//定义并初始化被截取的段落数量

int contentcount=0;

//开始截取的位置

int begincount=0;

//判断截取的段落数

int contentcutpart=contentlen/cutcontent;

int contentcutpars=contentlen%cutcontent;//求余数

//若余数为0,说明被整除,内容的长度正好是截取长度的倍数。

if (contentcutpars==0) {

contentcount=contentcutpart;

} else {

contentcount=contentcutpart+1;

}

//循环截取内容

for (int i = 1; i <=contentcount; i++) {

if (i!=contentcount) {

//按照截断长度截取内容

contentpart=wholecontent.substring(begincount,cutcontent*i);

}else{

//截取最后一部分内容

contentpart=wholecontent.substring(begincount, contentlen);

}

//赋值下一截取部分的起点位置

begincount=cutcontent*i;

System.out.println(begincount);

list.add(contentpart);

}

}

return list;

}

测试

/*public static void main(String[] args) {

List list=getContentByList("slflsdfapif",3);

for (Object object : list) {

System.out.println(object);

}

System.out.println();

}*/

}

原文:http://www.cnblogs.com/xiaotimo/p/5109257.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值