常常会用到切片技术
java代码
1 @Transactional(readOnly=false) 2 public void saveCommonMsg(CommonMsg commonMsg) { 3 4 //删除碎片 5 List<CommonMsgContent> list= commonMsgContentDao.findCollectionByConditionNoPage("", null, null); 6 7 commonMsgContentDao.deleteObjectByCollection(list); 8 //进行切片 9 10 String station = commonMsg.getStationRun(); 11 12 String dev = commonMsg.getDevRun(); 13 14 List<String> station_list = StringUtils.getContentByList(station, 30);//调用字符拆分,本博客中有 15 16 List<String> dev_list = StringUtils.getContentByList(dev, 30); 17 //保存 18 for (int i = 0; i < station_list.size(); i++) { 19 CommonMsgContent commonMsgContent = new CommonMsgContent(); 20 21 commonMsgContent.setContent(station_list.get(i)); 22 commonMsgContent.setType("1");//设置常量,在数据库中便于查找 23 24 commonMsgContent.setOrderby(i+1);//设置拆分字符的存放顺序,避免拼接字符串错误 25 26 commonMsgContentDao.save(commonMsgContent); 27 } 28 for (int i = 0; i < dev_list.size(); i++) { 29 CommonMsgContent commonMsgContent = new CommonMsgContent(); 30 31 commonMsgContent.setContent(dev_list.get(i)); 32 commonMsgContent.setType("2"); 33 34 commonMsgContent.setOrderby(i+1); 35 36 commonMsgContentDao.save(commonMsgContent); 37 38 } 39 40 commonMsgDao.saveMsg(commonMsg); 41 }
拼接字符串
1 public CommonMsg findcommonMsg() { 2 3 //查询所有的切片 4 String condition = " and o.type = ?"; 5 Object[] params = new Object[]{"1"}; 6 Map<String, String> orderby = new HashMap<String, String>(); 7 orderby.put(" o.orderby ", "asc"); 8 List<CommonMsgContent> list = commonMsgContentDao.findCollectionByConditionNoPage(condition, params, orderby); 9 10 String station_msg = ""; 11 12 for (CommonMsgContent commonMsgContent : list) { 13 station_msg += commonMsgContent.getContent();//拼接字符串 14 } 15 condition = " and o.type = ? "; 16 params = new Object[]{"2"}; 17 orderby = new HashMap<String, String>(); 18 orderby.put(" o.orderby ", "asc"); 19 list = commonMsgContentDao.findCollectionByConditionNoPage(condition, params, orderby); 20 21 String dev_msg = ""; 22 23 for (CommonMsgContent commonMsgContent : list) { 24 dev_msg += commonMsgContent.getContent();//拼接字符串 25 } 26 27 CommonMsg Msg = commonMsgDao.findMsg(); 28 29 if (Msg != null) { 30 //将拼接好的字符串放入到CommonMsg中
31 Msg.setDevRun(dev_msg);
32 Msg.setStationRun(station_msg);
33 return Msg;
34 }
35 return null;
36 }
原文:http://www.cnblogs.com/pi-qingwen/p/5110000.html