|
@@ -12,12 +12,19 @@ import com.keao.edu.common.page.PageInfo;
|
|
|
import com.keao.edu.common.page.QueryInfo;
|
|
|
import com.keao.edu.common.service.BaseService;
|
|
|
import com.keao.edu.util.collection.MapUtil;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.ibatis.io.Resources;
|
|
|
+import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* SERVICE操作基类
|
|
@@ -28,6 +35,9 @@ public abstract class BaseServiceImpl<PK extends Serializable, T> implements Bas
|
|
|
|
|
|
public abstract BaseDAO<PK, T> getDAO();
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SqlSessionFactory sqlSessionFactory;
|
|
|
+
|
|
|
/**
|
|
|
* 通过主键id加载对象
|
|
|
* @param id
|
|
@@ -108,8 +118,36 @@ public abstract class BaseServiceImpl<PK extends Serializable, T> implements Bas
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public <K extends List> Map getMap(String columnKey, String columnValue, K ids){
|
|
|
- List<Map> idColumnMaps = this.getDAO().getMaps(columnKey, columnValue, ids);
|
|
|
- return MapUtil.convertIntegerMap(idColumnMaps);
|
|
|
+ public <K extends List, Y, Z> Map<Y,Z> getMap(String tableName, String columnKey, String columnValue, K ids, Class<Y> keyType, Class<Z> valueType){
|
|
|
+ StringBuffer sql=new StringBuffer();
|
|
|
+ Map<Y,Z> result=new HashMap();
|
|
|
+ try {
|
|
|
+ sql.append("select ").append(columnKey).append(",").append(columnValue).append(" FROM ").append(tableName).append(" WHERE ").append(columnKey).append(" IN (").append(StringUtils.join(ids, ",")).append(")");
|
|
|
+ PreparedStatement ps = sqlSessionFactory.openSession().getConnection().prepareStatement(sql.toString());
|
|
|
+ ResultSet resultSet = ps.executeQuery();
|
|
|
+ while (resultSet.next()){
|
|
|
+ Y key=null;
|
|
|
+ Z value=null;
|
|
|
+ if(keyType.isAssignableFrom(BigDecimal.class)){
|
|
|
+ key = (Y) BigDecimal.class.getDeclaredConstructor(String.class).newInstance(resultSet.getString(1));
|
|
|
+ }else if(keyType.isAssignableFrom(String.class)){
|
|
|
+ key = (Y) resultSet.getString(1);
|
|
|
+ }else{
|
|
|
+ key = (Y) keyType.getMethod("valueOf", String.class).invoke(null,resultSet.getString(1));
|
|
|
+ }
|
|
|
+ if(valueType.isAssignableFrom(BigDecimal.class)){
|
|
|
+ value = (Z) BigDecimal.class.getDeclaredConstructor(String.class).newInstance(resultSet.getString(2));
|
|
|
+ }else if(valueType.isAssignableFrom(String.class)){
|
|
|
+ value = (Z) resultSet.getString(2);
|
|
|
+ }else{
|
|
|
+ value = (Z) valueType.getMethod("valueOf", String.class).invoke(null,resultSet.getString(2));
|
|
|
+ }
|
|
|
+ result.put(key, value);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
}
|
|
|
}
|