jeefast 框架(SpringBoot+Mybatis-Plus+Bootstrap+Vue)实现选中那条 导出那条
在做后台系统的时候看经常会遇到导入和导出数据 ,今天分享一下怎么做导出以及选中那条导出那条
自动生成代码等省略
第一步:
在生成的.xml文件中书写sql语句:
<select id="selectBatchIds" resultType="cn.jeefast.stu.entity.StuStudent">
SELECT s.*,c.`cname` ccName FROM stu_student s
LEFT JOIN stu_class c ON s.`ccid`=c.`cid`
where sid in
<foreach item="ids" collection="list" open="(" separator="," close=")">
#{ids}
</foreach>
</select>
在这里用到的是selectBatchIds
这个方法,这个是基类的方法,是不需要在service ,impl,dao等文件中声明 ,因为已经默认继承了 collection=“list”,等于list的原因也是因为selectBatchIds
这个方法只认list类型的,
var noticeIds = getSelectedRows();
if(noticeIds == null){
var ids=0;
}else{
var ids=noticeIds.join(",");
}
window.top.location.href = baseURL + "stu/student/exportExcel?token="+token+"&ids="+ids;
这里是js 端,获取选中的id 没有的话传0
public void exportExcel(HttpServletResponse response,HttpServletRequest request) throws Exception{
Map<String, Object> params = new HashMap<String, Object>();
List<StuStudent> userList = null;
List<Long> ids = new ArrayList<Long>();
String str=request.getParameter("ids");
if (str.equals("0") || str == "0") {
userList = studentService.selectList(new EntityWrapper<StuStudent>());
}else {
String[] noticeIds = str.split(",");
for (int i = 0; i < noticeIds.length; i++) {
ids.add(Long.parseLong(noticeIds[i]));
}
userList = studentService.selectBatchIds(ids);
}
OutputStream os = response.getOutputStream();
Map<String, String> map = new HashMap<String, String>();
map.put("title", "学生信息表");
map.put("total", userList.size()+" 条");
map.put("date", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
//响应信息,弹出文件下载窗口
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode("学生信息表.xls", "UTF-8"));
ExcelTemplate et = ExcelUtil.getInstance().handlerObj2Excel("web-info-template.xls", userList, StuStudent.class, true);
et.replaceFinalData(map);
et.wirteToStream(os);
os.flush();
os.close();
}
这里是控制器端,接收过来以后,判断是不是0如果是的话就是代表导出全部,就调用selectList方法,查询全部