閒來沒事,想了一個應用的例子:用java如何把數據庫的數據根據咱們指定的某幾列,如第2列,第4列,第6列導出來到Excel裏?html
寫代碼也是爲了應用的,寫好的代碼更重要的是在於思考。我本身思考了這個示例。java
問題:指定列把數據庫數據根據列導出到Excel裏。數據庫
那麼要處理這個問題,它實際上是很簡單的,可是再簡單的問題,也須要去拆分,思考,所謂,麻雀雖小,五臟俱全嘛。apache
拆分:1. Load DB data; 2. 把數據和指定列映射起來;3. 導出數據到Excel。數組
本文把第一步操做省略了,第一步操做就是讀取數據庫的數據,而後把它映射到Entity上去,也就是下面代買的Student.java,咱們假設這一步已經成功。框架
如下三個類重點實現第二步,第三步,只要把已經map好的數據,導出到Excel,能夠利用apache poi,詳見前一篇文章:java POI建立Excel示例(xslx和xsl區別 )ide
第二步的實現我以前想着是直接把類的get方法用case的方式跟傳進去的參數一一匹配,而後每次寫數據的時候執行那個方法來獲取當前要導出的列的數據。post
以下:測試
1 public static Object getStudentInfo(Student student, int index) { 2 switch(index) { 3 case 1 : 4 return student.getName(); 5 case 2 : 6 return student.getSex(); 7 ... 8 } 9 }
但是這樣看起來太傻逼了,因而接着想有沒有比較合理的方式實現呢?ui
後來想到了用註解和java的反射機制來實現。
如今java註解用途愈來愈普遍了,如Google的Guice,一輕量級的IOC框架,大有跟Spring抗衡的趨勢。
我就用註解來標註get方法獲取的數據在數據庫中對應列的索引,而後,根據傳進去的列數組匹配get方法,再利用java的反射,執行方法體獲取列的數據。具體代碼以下。
MapValue是一個Annotation,裏面只有一個index()方法,用來標註方法的索引。
1 package com.cnblogs.rolf.dao; 2 3 4 import java.lang.annotation.ElementType; 5 import java.lang.annotation.Retention; 6 import java.lang.annotation.RetentionPolicy; 7 import java.lang.annotation.Target; 8 /** 9 * 放在方法上的註解,這個註解能夠映射方法的索引 10 * @author 草原戰狼 11 * 12 */ 13 @Target(value = {ElementType.METHOD}) 14 @Retention(RetentionPolicy.RUNTIME) 15 public @interface MapValue { 16 /** 17 * 標註方法的索引 18 * @return 方法索引值 19 */ 20 int index() default 0; 21 }
Student類是用來map到數據庫的實體類,上面的get方法用MapValue標註了它在數據庫中表列的順序。
1 package com.cnblogs.rolf.entity; 2 3 import com.cnblogs.rolf.dao.MapValue; 4 5 /** 6 * Student 至關於一個表的實體類,咱們能夠假想它是從數據庫讀出來映射出來的一張表信息 7 * STUDENT_COLUMN_NAMES: 這張表的類名 8 * @author 草原戰狼 9 * 10 */ 11 public class Student { 12 public static String[] STUDENT_COLUMN_NAMES = {"Name", "Sex", "Age", "Height", "Weight", "Grade"}; 13 private String name; 14 private String sex; 15 private int age; 16 private int height; 17 private int weigth; 18 private int grade; 19 20 public Student(String name, String sex, int age, int height, int weight, int grade) { 21 this.name = name; 22 this.sex = sex; 23 this.height = height; 24 this.weigth = weight; 25 this.age = age; 26 this.grade = grade; 27 } 28 public Student() { 29 30 } 31 32 @MapValue(index = 1) 33 public String getName() { 34 return name; 35 } 36 public void setName(String name) { 37 this.name = name; 38 } 39 @MapValue(index = 2) 40 public String getSex() { 41 return sex; 42 } 43 public void setSex(String sex) { 44 this.sex = sex; 45 } 46 @MapValue(index = 3) 47 public int getAge() { 48 return age; 49 } 50 public void setAge(int age) { 51 this.age = age; 52 } 53 @MapValue(index = 4) 54 public int getHeight() { 55 return height; 56 } 57 public void setHeight(int height) { 58 this.height = height; 59 } 60 @MapValue(index = 5) 61 public int getWeigth() { 62 return weigth; 63 } 64 public void setWeigth(int weigth) { 65 this.weigth = weigth; 66 } 67 @MapValue(index = 6) 68 public int getGrade() { 69 return grade; 70 } 71 public void setGrade(int grade) { 72 this.grade = grade; 73 } 74 }
TestMain是測試的類,裏面還有兩個方法,這兩個方法是用來處理實體類和要導出列數組的匹配。
1 package com.cnblogs.rolf; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.Method; 5 6 import com.cnblogs.rolf.dao.MapValue; 7 import com.cnblogs.rolf.entity.Student; 8 /** 9 * 能夠應用在從數據庫導出數據,根據指定要導出的列,導出到Excel, Word等。這個只是中間的環節 10 * @author 草原戰狼 11 * 12 */ 13 public class TestMain { 14 public static void main(String[] args) { 15 Student student = new Student("XiaoMing", "Male", 20, 175, 135, 2); 16 int[] indexs = {34,2,5,3,0,1,2,4,6}; 17 System.out.println(student.getAge()); 18 String[] names = Student.STUDENT_COLUMN_NAMES; 19 String printColumns = getColumnName(names, indexs); 20 String pringStudentInfo = getStudentInfo(student, indexs); 21 System.out.println(printColumns); 22 System.out.println(pringStudentInfo); 23 } 24 /** 25 * 接收一張表的實體類和外面傳進來的要導出的列的索引順序,好比要導出第2列,第4列,那麼能夠傳入{2,4} 26 * 返回獲取到的指定列的值 27 * @param student 表的實體類 28 * @param indexs 要導出的列的索引 29 * @return 指定列的值 30 */ 31 public static String getStudentInfo(Student student, int[] indexs) { 32 String studentInfo = ""; 33 Method[] methods = Student.class.getMethods(); 34 for(int index : indexs) { 35 for(Method method : methods) { 36 MapValue mapValue = method.getAnnotation(MapValue.class); 37 if(mapValue == null){ 38 continue; 39 }else { 40 if(index == mapValue.index()) { 41 Object value = null; 42 try { 43 value = method.invoke(student); 44 } catch (IllegalAccessException 45 | IllegalArgumentException 46 | InvocationTargetException e) { 47 System.out.println("parameters number is wrong."); 48 e.printStackTrace(); 49 } 50 studentInfo += value.toString() + ", "; 51 break; 52 } 53 } 54 } 55 } 56 return studentInfo; 57 } 58 /** 59 * 根據指定的索引導出列的名稱 60 * @param names 整張表全部列的名稱 61 * @param indexs 要導出的列的索引 62 * @return 導出的列的名稱 63 */ 64 public static String getColumnName(String[] names, int[] indexs) { 65 String result = ""; 66 for(int index : indexs) { 67 if(index > names.length || index < 1) { 68 continue; 69 } 70 result += names[index - 1] + ", "; 71 } 72 return result; 73 } 74 }
測試的結果:
Sex, Weight, Age, Name, Sex, Height, Grade,
Male, 135, 20, XiaoMing, Male, 175, 2,
草原戰狼淘寶小店:http://xarxf.taobao.com/ 淘寶搜小矮人鞋坊,主營精緻美麗時尚女鞋,爲您的白雪公主挑一雙哦。謝謝各位博友的支持。
====================================================================================
====================== 以上分析僅表明我的觀點,歡迎指正與交流 =========================
====================== 草原戰狼博客,轉載請註明出處,萬分感謝 =========================
====================================================================================