被老板逼着实现了Excle的透视表分析算法
Posted yangfeiORfeiyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了被老板逼着实现了Excle的透视表分析算法相关的知识,希望对你有一定的参考价值。
目前仅能实现二维二重表,后面功能会继续改进
package com.example.demo;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.alibaba.fastjson.JSON;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({ @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext.xml" }),
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring-mvc-servlet.xml" }) })
public class QueryTest {
public static void main(String[] args) throws SQLException {
List l = new ArrayList();
l.add(new FieldDefinitionDomain("zz","int","money","line"));
l.add(new FieldDefinitionDomain("xx","String","date","row"));
l.add(new FieldDefinitionDomain("zz","String","company","row") );
String fieldJsonList = JSON.toJSONString(l);
Map fieldMap = new HashMap();
List<FieldDefinitionDomain> parseArray = JSON.parseArray(fieldJsonList, FieldDefinitionDomain.class);
ArrayList<String[]> strlist = new ArrayList<>();
List<String[]> intList = new ArrayList<>();
String headfieldName = null;
String headType = null;
String headTableName = null;
String headDirection = null;
for (FieldDefinitionDomain fd : parseArray) {
String [] strarray = new String[4];
strarray[0] = fd.getTableName();
strarray[1] = fd.getFieldName();
strarray[2] = fd.getType();
strarray[3] = fd.getDirection();
if(fd.getType().equals("int")||fd.getType().equals("long")) {
intList.add(strarray);
}else {
strlist.add(strarray);
}
}
headTableName = strlist.get(0)[0];
headfieldName = strlist.get(0)[1];
headType = strlist.get(0)[2];
headDirection = strlist.get(0)[3];
for (String[] array : strlist) {
if(!array[1].equals(headfieldName)) {
List arrayList = new ArrayList();
addList(arrayList,array);
fieldMap.put(array[0], arrayList);
}
}
List<Integer> iilist = new ArrayList();
for (String[] array : intList) {
List list = (List)fieldMap.get(array[0]);
if(!notEmpty(list)) {
List arrayList = new ArrayList();
addList(arrayList,array);
fieldMap.put(array[0], arrayList);
}
((List)list.get(0)).add(array[1]);//字段名
((List)list.get(1)).add(array[2]);//字段类型
((List)list.get(2)).add(array[3]);//排列(行还是竖)
iilist.add(((List)list.get(2)).size());
}
List resultList = new ArrayList();
TableSQLUtils tableSQLUtils = new TableSQLUtils();
List list = tableSQLUtils.query(headTableName, headType, headfieldName, null);
Map mm = new HashMap();
list.forEach(ll1 ->mm.put(((List)ll1).get(0), new ArrayList()));
for (Object o2 : fieldMap.keySet()) {
List llist= (List)fieldMap.get(o2);
List fieldList = (List) llist.get(0);
List typeList = (List) llist.get(1);
String fieldStr = headfieldName+",";
String typeStr = headType+",";
for (Object object : fieldList) {
fieldStr+=object+",";
}
for (Object object : typeList) {
typeStr+=object+",";
}
fieldStr=fieldStr.substring(0,fieldStr.length()-1);
typeStr=typeStr.substring(0,typeStr.length()-1);
List query = tableSQLUtils.query((String)o2, typeStr, fieldStr,null);
query.forEach(ll->{
List ll1 = (List)ll;
List ll2 = ((List) mm.get(ll1.get(0)));
if(ll2!=null)
ll2.add(ll1);
});
mm.put("numberindex", iilist);
System.out.println(mm);
}
}
private static void addList(List arrayList, String[] array) {
List arrayList2 = new ArrayList();
List arrayList3 = new ArrayList();
List arrayList4 = new ArrayList();
arrayList2.add(array[1]);
arrayList3.add(array[2]);
arrayList4.add(array[3]);
arrayList.add(arrayList2);
arrayList.add(arrayList3);
arrayList.add(arrayList4);
}
private static void sum(List <Long> list) {
Long sum = 0L;
if(notEmpty(list)) {
for (int i = 0; i < list.size(); i++) {
sum+=list.get(i);
}
list.add(sum);
}
}
public static Boolean notEmpty(List list) {
if(list!=null&&list.size()>0)return true;
return false;
}
}
package com.example.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TableSQLUtils {
private String driver="com.mysql.jdbc.Driver";
private String username="finance_uatuser";
private String password="e2guTHK2U6y_G2o0LTQJGneTuWOOqp/wy";
private String url="jdbc:mysql://121.43.155.144:3306/finance_uat?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull";
public List query(String table,String type,String fieldName,String prefs) throws SQLException {
Connection connection = null;
PreparedStatement prepareStatement =null;
ResultSet rs = null;
// Map m = new HashMap();
Map map = new HashMap();
String[] fieldsplit = fieldName.split(",");
List resultList = new ArrayList();
String[] types = type.split(",");
// if(fieldsplit.length>1) {
// for (int i = 1; i < fieldsplit.length; i++) {
// map.put(fieldsplit[i],new ArrayList());
// }
// }if(fieldsplit.length==1) {
// for (int i = 0; i < fieldsplit.length; i++) {
// map.put(fieldsplit[i],new ArrayList());
// }
// }
try {
//1.获取Connection
connection = getConnection();
//3.准备Sql
String sql = "select ";
for (String sf : fieldsplit) {
sql+=sf+",";
}
sql=sql.substring(0, sql.length()-1);
sql+=" from "+table;
//if(fieldsplit.length>1)
// sql+=" where "+fieldsplit[0]+" = ?";
prepareStatement = connection.prepareStatement(sql);
//if(fieldsplit.length>1)
//prepareStatement.setString(1,prefs);
//4.执行查询,得到ResultSet
rs = prepareStatement.executeQuery();
//5.处理ResultSet
while(rs.next()){
//rs.get+数据库中对应的类型+(数据库中对应的列别名)
switchType(types,fieldsplit,rs,resultList);
}
return resultList;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
rs.close();
prepareStatement.close();
connection.close();
}
}
private void switchType(String[] types, String[] field, ResultSet rs, List resultList) throws SQLException {
List arr = new ArrayList();
for (int i = 0; i < field.length; i++) {
listAdd(rs,types[i],field[i], arr);
}
resultList.add(arr);
}
private void listAdd(ResultSet rs,String type, String field, List arr) throws SQLException {
Object o = null;
if(type.equals("int")) {
o = (long) rs.getInt(field);
}
if(type.equals("String")) {
o = rs.getString(field);
}
if(type.equals("Long")) {
o = rs.getLong(field);
}
arr.add(o);
}
private Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, username, password);
return connection;
}
public static void main(String[] args) throws SQLException {
TableSQLUtils tableSQLUtils = new TableSQLUtils();
List query = tableSQLUtils.query("zz", "String,int", "date,money",null);
query.forEach(str->System.out.println(str));
}
}
以上是关于被老板逼着实现了Excle的透视表分析算法的主要内容,如果未能解决你的问题,请参考以下文章