Joburgess 5 gadi atpakaļ
vecāks
revīzija
f25ab8dcde

+ 0 - 3
edu-common/src/main/java/com/keao/edu/common/dal/BaseDAO.java

@@ -71,7 +71,4 @@ public interface BaseDAO<PK extends Serializable, T>{
 	 */
 	public int queryCount(Map<String, Object> params);
 
-	<K extends List> List<Map> getMaps(@Param("columnKey") String columnKey,
-							  @Param("columnValue") String columnValue,
-							  @Param("ids") K ids);
 }

+ 10 - 1
edu-common/src/main/java/com/keao/edu/common/service/BaseService.java

@@ -71,6 +71,15 @@ public interface BaseService<PK extends Serializable, T> {
 	 */
 	public int findCount(Map<String, Object> params);
 
-	<K extends List> Map getMap(String columnKey, String columnValue, K ids);
+	/**
+	 * @describe 获取columnKey-columnValue集合
+	 * @author Joburgess
+	 * @date 2020.06.23
+	 * @param columnKey: key所对应的列名
+	 * @param columnValue: value所对应的列名
+	 * @param ids: key条件
+	 * @return java.util.List<java.util.Map>
+	 */
+	<K extends List, Y, Z> Map<Y,Z> getMap(String tableName, String columnKey, String columnValue, K ids, Class<Y> keyType, Class<Z> valueType);
 	
 }

+ 45 - 7
edu-common/src/main/java/com/keao/edu/common/service/impl/BaseServiceImpl.java

@@ -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;
 	}
 }

+ 9 - 10
edu-user/edu-user-server/src/main/java/com/keao/edu/user/service/impl/ExamRegistrationServiceImpl.java

@@ -13,10 +13,9 @@ import com.keao.edu.util.collection.MapUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.math.BigDecimal;
+import java.util.*;
+import java.util.stream.Collectors;
 
 @Service
 public class ExamRegistrationServiceImpl extends BaseServiceImpl<Long, ExamRegistration> implements ExamRegistrationService {
@@ -37,19 +36,19 @@ public class ExamRegistrationServiceImpl extends BaseServiceImpl<Long, ExamRegis
 		Map<String, Object> params = new HashMap<String, Object>();
 		MapUtil.populateMap(params, queryInfo);
 
-		List<ExamRegistration> dataList = new ArrayList<>();
+		List<ExamRegistration> dataList = Collections.EMPTY_LIST;
 		int count = this.findCount(params);
 		if (count > 0) {
 			pageInfo.setTotal(count);
 			params.put("offset", pageInfo.getOffset());
 			dataList = this.getDAO().queryPage(params);
+			List<Integer> agencyIds = dataList.stream().map(ExamRegistration::getAgencyId).collect(Collectors.toList());
+			Map<BigDecimal, String> agencyIdNameMap = this.getMap("agency", "id_", "name_", agencyIds, BigDecimal.class, String.class);
+			for (ExamRegistration examRegistration : dataList) {
+				examRegistration.setSongJson(agencyIdNameMap.get(examRegistration.getAgencyId()));
+			}
 		}
 		pageInfo.setRows(dataList);
-
-		List<Integer> agencyIds=new ArrayList<>();
-		agencyIds.add(3);
-		Map map = agencyService.getMap("id_", "name_", agencyIds);
-
 		return pageInfo;
 	}
 }

+ 0 - 13
edu-user/edu-user-server/src/main/resources/config/mybatis/AgencyMapper.xml

@@ -28,19 +28,6 @@
 	<select id="findAll" resultMap="Agency">
 		SELECT * FROM agency WHERE tenant_id_ = #{tenantId} ORDER BY id_
 	</select>
-
-	<select id="getMaps" resultType="map">
-		<![CDATA[
-			SELECT
-				${columnKey} AS 'key',
-				${columnValue} AS 'value'
-			FROM agency
-			WHERE ${columnKey} IN
-		]]>
-			<foreach collection="ids" item="id" separator="," open="(" close=")">
-				#{id}
-			</foreach>
-	</select>
 	
 	<!-- 向数据库增加一条记录 -->
 	<insert id="insert" parameterType="com.keao.edu.user.entity.Agency" useGeneratedKeys="true" keyColumn="id" keyProperty="id">