首页 > 数据库技术 > 详细

Mysql切片保存数据

时间:2016-01-07 13:17:30      阅读:387      评论:0      收藏:0      [点我收藏+]

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<ElecRunCommonMsg> list=msg_dao.findCollectionByConditionNoPage("", null, null);
msg_dao.deleteByCollection(list);

//2. 将站点运行情况和设备运行情况切片
String station=entity.getSituationRun();
String devRun=entity.getDevRun();
List<String> station_list=StringUtils.getContentByList(station, 20);
List<String> devRun_list=StringUtils.getContentByList(devRun, 20);
//3. 保存切片的数据
//保存站点的运行情况
for (int i = 0; i <station_list.size(); 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 <devRun_list.size(); 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<String,String> orderby = new HashMap<String, String>();
orderby.put("o.orderby", "asc");
List<ElecRunCommonMsg> 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<String, String>();
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<String> getContentByList(String wholecontent,int cutcontent){

List<String> list=new ArrayList<String>();
//获取完整内容字符串的总长度
int contentlen = wholecontent.length();
System.out.println(contentlen);
//内容截取,用内容总长和截取长度进行比较,无须截取的话直接插入
if (contentlen<cutcontent) {
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();
}*/
}

Mysql切片保存数据

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!