批量导出
jsp界面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function() {
$("#selectAll").click(function() {
if (this.checked) {
$("input[type=checkbox]:gt(0)").attr("checked", "checked");
} else {
$("input[type=checkbox]:gt(0)").attr("checked", false);
}
});
$("#deleteAll")
.click(
function() {
var array = new Array();
$("input[type=checkbox]:gt(0):checked").each(
function() {
array.push($(this).parent().next()
.text());
});
console.log(array);
location.href = "${pageContext.request.contextPath }/user/delete?ids="
+ array;
});
$("#dao")
.click(
function() {
var array = $("input[type=checkbox]:gt(0):checked");
if (array.length == 0) {
alert("请选择你要导出的内容!");
} else {
var ids = new Array();
array.each(function() {
ids.push($(this).parent().next().text());
});
//alert(ids);
$.post(
"${pageContext.request.contextPath}/user/selectByIds",
"ids=" + ids, function(d) {
if (d == "ok") {
alert("数据导入成功!");
} else {
alert("数据导入失败!");
}
location.reload();
}, "json");
}
});
});
</script>
</head>
<body>
<table border="1">
<tr>
<td><input type="checkbox" id="selectAll"><input
type="button" value="删除" id="deleteAll"></td>
<td>ID</td>
<td>用户名</td>
<td>密码</td>
<td>年龄</td>
<td>性别</td>
</tr>
<c:forEach items="${list }" var="us">
<tr>
<td><input type="checkbox"></td>
<td>${us.id }</td>
<td>${us.username }</td>
<td>${us.password }</td>
<td>${us.age }</td>
<td>${us.sex }</td>
</tr>
</c:forEach>
</table>
<input type="button" value="导出" id="dao">
</body>
</html>
mapper:
package com.bwie.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.bwie.pojo.User;
public interface UserMapper {
public void insert(User user);
public List<User> select();
public void deleteById(Integer id);
public List<User> selectByIds(@Param("ids") int[] ids);
}
controller :
package com.bwie.controller;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService us;
@RequestMapping("insert")
public String insert(Model model, User user) {
us.insert(user);
return "redirect:/user/select";
}
@RequestMapping("select")
public String select(Model model, User user) {
List<User> list = us.select();
model.addAttribute("list", list);
return "insertok";
}
@RequestMapping("delete")
public String dalete(Model model, String[] ids) {
System.out.println("///");
for (String string : ids) {
us.deleteById(Integer.parseInt(string));
}
List<User> list = us.select();
model.addAttribute("list", list);
return "insertok";
}
@RequestMapping("selectByIds")
@ResponseBody
public String selectByIds(Model model, String[] ids) throws IOException {
FileOutputStream outputStream =null;
OutputStreamWriter writer=null;
BufferedWriter writer2=null;
try {
int[] idss=new int[ids.length];
for(int i=0;i<ids.length;i++){
idss[i]=Integer.parseInt(ids[i]);
}
List<User> list = us.selectByIds(idss);
outputStream = new FileOutputStream(new File("D://Users.txt"));
writer=new OutputStreamWriter(outputStream, "utf-8");
writer2 = new BufferedWriter(writer);
for (User user : list) {
writer2.write(user.getId()+"#"+user.getUsername()+"#"+user.getPassword()+"#"+user.getAge()+"#"+user.getSex());
writer2.newLine();
}
return "ok";
} catch (Exception e) {
return "error";
}finally{
writer2.close();
writer.close();
outputStream.close();
}
}
}
mapper.xml
<?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.bwie.dao.UserMapper">
<insert id="insert" parameterType="user">
insert into
t_user(username,password,age,sex)
value(#{username},#{password},#{age},#{sex})
</insert>
<select id="select" resultType="user">
select * from t_user
</select>
<delete id="deleteById" parameterType="int">
delete from t_user where
id=#{id}
</delete>
//导出
<select id="selectByIds" parameterType="int" resultType="user">
select * from t_user
<where>
<foreach collection="ids" item="a" close=")" open="id in ("
separator=",">
#{a}
</foreach>
</where>
</select>
</mapper>