Skip to content

Instantly share code, notes, and snippets.

@an-sangkil
Last active August 5, 2021 10:05
Show Gist options
  • Save an-sangkil/71f90661fa7e7268f940 to your computer and use it in GitHub Desktop.
Save an-sangkil/71f90661fa7e7268f940 to your computer and use it in GitHub Desktop.
Mybatis ResultHandler
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.xxxxx.beans.SfProjectVerificationReport;
public abstract class SfAbstractReportCommonDao extends SqlSessionDaoSupport {
private Logger logger = LoggerFactory. getLogger( this.getClass() );
private final String NAMESPACE = this.getClass().getPackage().getName();
/**
*
* @param queryId
* @param maxCount QUERY LIMIT
* @param parameter
* @return
* @throws Exception
*/
public List<SfProjectVerificationReport> selectReportResultSet (String queryId , int maxCount , Map<String,Object> parameter) throws Exception {
boolean endPoint = true;
int firstDataNo = 0 ;
List<SfProjectVerificationReport> dataList = new ArrayList<>();
List<SfProjectVerificationReport> dataMergeList = new ArrayList<>();
while (endPoint) {
parameter.put("firstDataNo", firstDataNo);
parameter.put("customLimit", maxCount);
dataList = this.getSqlSession().selectList(queryId, parameter);
firstDataNo = (int) dataList.get(dataList.size()-1).getDataRowNum();
logger.trace("FirstDataNo = {} ", firstDataNo );
System.out.println();
if(!dataList.isEmpty()){
if(dataList.size() < maxCount) {
endPoint = false;
}
}
dataMergeList.addAll(dataList);
}
logger.trace("dataMergeList SIZE = {} ", dataMergeList.size() );
return dataMergeList;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxxxx.dao.postgresql.SfReportDao">
<resultMap id="verificationReportResult" type="com.xxxxx.beans.SfProjectVerificationReport" >
</resultMap>
<select id="listSecurityDetectDetail" parameterType="Map" resultMap="verificationReportResult">
select * form (
select
*
,ROW_NUMBER(r_no desc) as r_row_number ;
from reportTable
)
<if test="firstDataNo != null and firstDataNo > 0 and firstDataNo != ''">
WHERE r_row_number < = #{firstDataNo}
</if>
order by r_no desc
limit #{customLimit}
</select>
</mapper>
@Repository(value="sfReportDao")
public class SfReportDaoPostgreSql extends SfAbstractReportCommonDao implements SfReportDao{
private final String NAMESPACE = this.getClass().getPackage().getName();
@Autowired
private SfAuditLogDao sfAuditDao;
@Override
public List<SfProjectVerificationReport> listSecurityDetectDetailHandler(SfSession sfSession, Map<String, Object> dataMap){
class ReportResultHandler implements ResultHandler {
List<SfProjectVerificationReport> resultList = new ArrayList<SfProjectVerificationReport>();
@Override
public void handleResult(ResultContext context) {
if( context.getResultObject() instanceof SfProjectVerificationReport ){
SfProjectVerificationReport sfProjectVerificationReport = (SfProjectVerificationReport) context.getResultObject();
resultList.add(sfProjectVerificationReport);
}
}
}
ReportResultHandler reportResultHandler = new ReportResultHandler();
List<SfProjectVerificationReport> sfProjectVerificationReportList = reportResultHandler.resultList;
super.getSqlSession().select( NAMESPACE + ".SfReportDao.listSecurityDetectDetail", dataMap ,reportResultHandler);
return sfProjectVerificationReportList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment