标签:spring原理进程
一个excel中有两个sheet页,第一个sheet页保存的是本月的数据,第二个sheet页保存的是次月的数据,两个sheet页中的数据都分别对应着数据库中不同的两张表,如何实现excel往数据库的导入呢。
/** * 将excel读入到Map * key为sheet页名称,value为将exceld单元格解析成的二维数据 * @param file * @return */ private static Map<String,String[][]> reader(File file){ Map<String,String[][]> map=new HashMap<String,String[][]>(); //可以根据sheet页名称作循环解析 map.put("excelService4CurrentMonth", null); map.put("excelService4CNextMonth", null); return map; }
package com.springIoc.service; import com.springIoc.mapper.IExcelMapper; /** * excel服务类 * @author Administrator * */ public class ExcelServer { /**excel封装类,作解耦合使用*/ private IExcelMapper excelMapper; /**excel返回的二维数组*/ private String[][] values; public String[][] getValues() { return values; } public void setValues(String[][] values) { this.values = values; } public IExcelMapper getExcelMapper() { return excelMapper; } public void setExcelMapper(IExcelMapper excelMapper) { this.excelMapper = excelMapper; } /**可以通过hibernate保存方法实现数组的保存*/ public void save(String[][] values){ Object obj=excelMapper.mappering(values); System.out.println(obj+"保存成功"); } }
package com.springIoc.mapper; /** * excel映射接口 * @author Administrator * */ public interface IExcelMapper { public abstract Object mappering(String[][] values); }
package com.springIoc.mapper.impl; import com.springIoc.mapper.IExcelMapper; /** * 本月数据封装 * @author yj.shi * */ public class ExcelMapper4CurrentMonth implements IExcelMapper{ @Override public Object mappering(String[][] values) { System.out.println("本月数据封装"); return "本月数据"; } }在service中调用接口,是不是就实现了数据的解耦呢?service跟数据映射类(ExcelMapper4CurrentMonth)是不是就可以随便拆开用了。
#本月excel映射 excelMapper4CurrentMonth=com.springIoc.mapper.impl.ExcelMapper4CurrentMonth #次月excel映射 excelMapper4NextMonth=com.springIoc.mapper.impl.ExcelMapper4NextMonth excelService4CurrentMonth=com.springIoc.service.ExcelServer excelService4CNextMonth=com.springIoc.service.ExcelServer excelService4CurrentMonth.excelMapper=excelMapper4CurrentMonth excelService4CNextMonth.excelMapper=excelMapper4NextMonth
package com.springIoc.container; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.apache.commons.beanutils.PropertyUtils; /** * 相当于spring容器,用来保存bean * @author yj.shi * */ public class Container { private static Map<String,Object> map=new HashMap<String,Object>(); public Container(){ Properties properties=new Properties(); InputStream in = null; try { in=Container.class.getClassLoader().getResourceAsStream("ioc.properties"); properties.load(in); for(Map.Entry<Object, Object> entity:properties.entrySet()){ String key=entity.getKey().toString(); String value=entity.getValue().toString(); mappeingBean(key,value); } } catch (Exception e) { e.printStackTrace(); } finally{ if(in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 对properties的内容进行映射 * @param key * @param value * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException * @throws InvocationTargetException */ private void mappeingBean(String key,String value) throws InstantiationException, IllegalAccessException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException{ String[] parts=key.split("\\."); if(parts.length==1){//对象创建 map.put(parts[0],Class.forName(value).newInstance() ); }else{//注入引用关系 Object target=getBeanById(parts[0]); Object resource=getBeanById(value); PropertyUtils.setProperty(target, parts[1], resource); } } /** * 通过key过去spring容器中的数据 * @param key * @return */ public static Object getBeanById(String key){ return map.get(key); } }
package com.springIoc.test; import java.io.File; import java.util.HashMap; import java.util.Map; import com.springIoc.container.Container; import com.springIoc.service.ExcelServer; public class TestMain { @SuppressWarnings("static-access") public static void main(String[] args) { Container container=new Container(); Map<String,String[][]> map=reader(null); for(String key:map.keySet()){ ExcelServer service=(ExcelServer) container.getBeanById(key); service.save(map.get(key)); } } /** * 将excel读入到Map * key为sheet页名称,value为将exceld单元格解析成的二维数据 * @param file * @return */ private static Map<String,String[][]> reader(File file){ Map<String,String[][]> map=new HashMap<String,String[][]>(); //可以根据sheet页名称作循环解析 map.put("excelService4CurrentMonth", null); map.put("excelService4CNextMonth", null); return map; } }
Spring原理探索第一步-以excel导入为列探讨SpringIoc原理
标签:spring原理进程
原文地址:http://blog.csdn.net/qzshiyongjie123/article/details/41727979