标签:
思路:比如现在10月份,需要把10月份之前的数据放入到另一张表中.
步骤:
A.查询出10月份之前的数据.
B.利用insert into 表(字段) values (值);将数据插入到新的表中.
C.删除10月份之前的数据,在旧表中.
代码如下:
JSP页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <script src="admin/addPageInit.js?${JSCSS}" type="text/javascript"></script> <form name="form1" method="post" action="exportBills" id="form1"> <table class="table table-bordered"> <tr> <td class="w50 tc"><h2>导出电话账单日期:</h2></td> <td class="w50 tc"><input class="form-control" name="addTime" id="datetimepicker" type="text" /></td> </tr> </table> <div class="form-group"> <input type="submit" value="提交" id="Button1" class="form-control btn btn-success" /> </div> </form> <%@include file="datePicker.jsp"%>//时间控件
当点击submit的时候,调用exportBills方法
Action:
// 导出电话账单 @Action(value = "exportBills") public String manageExportBills() throws Exception { // 查询此日期的电话账单 billService.exportBills(getParameter("addTime", "0"), map); return SUCCESS; }
service(实现类):
@Override @Transactional(propagation = Propagation.REQUIRED)//开启事务 public void exportBills(String addTime, Map<String, Object> map) throws Exception { // 查询此日期的电话账单 List<Bill> models = findByHql("from Bill where addTime<=:addTime and flag>:flag", DataUtils.getMap("addTime", addTime,"flag","-1000")); for(Bill bill:models){ map = YTTools.getFieldAndValue(bill); map.put("uid", bill.getUser().getId()); map.put("id", bill.getId()); map.put("flag", bill.getFlag()); map.put("addTime", bill.getAddTime()); StringBuilder fields = new StringBuilder(); for (Entry<String, Object> entry : map.entrySet()) { fields.append(","+entry.getKey()); } if(map.size()>0){ String values = fields.toString().replaceAll(",", ",:"); String hql = "insert into Billhistory("+fields.toString().substring(1, fields.length())+")" + " values("+values.substring(1,values.length())+")"; // 新增操作 executeBySql(hql, map); } } // 删除操作 executeByHql("delete Bill where addTime<=:addTime and flag>:flag", DataUtils.getMap("addTime", addTime,"flag","-1000")); }
工具类:
package cn.yitongworld.util; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; public class YTTools { public static Map<String,Object> getFieldAndValue(Object model) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ Map<String,Object> map = new HashMap<String,Object>(); //获取实体类的所有属性,返回Field数组 Field[] fields = model.getClass().getDeclaredFields(); //遍历所有属性 for(Field field:fields){ //获取属性的名字 String oldName = field.getName(); Object value = null; String name = oldName.substring(0,1).toUpperCase()+oldName.substring(1); //将属性的首字符大写,方便构造get,set方法 String type = field.getGenericType().toString(); //获取属性的类型 if(type.equals("class java.lang.String")||type.equals("class java.lang.Integer") ||type.equals("class java.lang.Double")||type.equals("class java.lang.Boolean")){ //如果type是类类型,则前面包含"class ",后面跟类名 Method m = model.getClass().getMethod("get"+name); value = m.invoke(model); //调用getter方法获取属性值 map.put(oldName, value); } } return map; } }
参考文献:
1.java的反射机制,通过class找到java:http://blog.csdn.net/liujiahan629629/article/details/18013523
2.遍历map的两种方法:http://blog.163.com/fw_long/blog/static/51771186201392982041337/
3.java Class getDeclaredFields()与getFields()的区别:http://hw1287789687.iteye.com/blog/1942056
标签:
原文地址:http://my.oschina.net/Tsher2015/blog/516929