前提:已經編寫好相應的接口個xml文件
public void exportExcel_bw() throws Exception {
//封裝sql需要查詢的sql的條件
Map<String, Object> paramMap = new HashMap();
paramMap.put("parentName", "權限管理");
paramMap.put("pageBegin", 0);
paramMap.put("pageSize", 20);
//獲取執行sql
Configuration con = sqlSessionFactory.getConfiguration();
//傳xml文件中的 需要執行的id編號 如下圖說明
MappedStatement s = con.getMappedStatement(com.sgd.eic.nxdb.yhqd.dao.YhqdSuspectEleStealDao.selectSuspectUserEleStealStat);
BoundSql bSql = s.getBoundSql(paramMap);
//獲取到sql中封裝的參數的數量
List<ParameterMapping> paramValues = bSql.getParameterMappings();
String sql=getExecuteSql(bSql.getSql(),paramValues,paramMap);
//創建連接
SqlSession session = sqlSessionFactory.openSession();
Statement cs = session.getConnection().createStatement();
ResultSet set = cs.executeQuery(sql);//執行查詢
System.out.println(sql);
}
/**
* 生成可執行sql
* @param sql 獲取的sql
* @param paramValues 動態參key
* @param map 動態參valur
* @return
*/
private String getExecuteSql(String sql, List<ParameterMapping> paramValues,Map map) {
while(sql.indexOf("?") != -1 && paramValues.size() > 0) {
String paramName = paramValues.get(0).getProperty();
String paramValue = map.get(paramName).toString();
String value = "";
if (paramValue instanceof String) {
value = "'" + paramValue + "'";
}
sql = sql.replaceFirst("\\?", value);
paramValues.remove(0);
}
return sql;
}
參考:http://www.360doc.com/content/17/0415/16/21706453_645828496.shtml