项目代码下载地址:https://github.com/snowlavenderlove/springBatchImport.git
1.通过官网创建项目springBatchImport,创建项目参考博文:https://blog.csdn.net/qq_37231511/article/details/90669242
2.创建数据库springimport_excel,建表user,如图
3.准备表格数据如图:
4.通过genertator-mybatis自动生成代码,如图
5.编辑pom.xml,添加项目依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
com.xue
springBatchImport
0.0.1-SNAPSHOT
springBatchImport
Demo project for Spring Boot
1.8
3.0.0
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
commons-logging
commons-logging
1.2
com.alibaba
druid
1.1.17
org.apache.poi
poi
3.15
org.apache.poi
poi-ooxml
3.15
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
org.springframework.boot
spring-boot-maven-plugin
6.编辑属性文件application.properties
#mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springimport_excel?&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
#druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#dao
mybatis.type-aliases-package==com.xue.repository.dao
mybatis.mapper-locations=classpath*:com/xue/repository/mapper/*.xml
#thymeleaf
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.jsp
spring.thymeleaf.encoding=UTF-8
7.创建controller层,新建类UserController
package com.xue.controller;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.xue.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
/**
* 页面
*/
@RequestMapping("/index")
public String index(){
return "index";
}
/**
* 导入excel
*/
@RequestMapping("/import")
@ResponseBody
public String excelImport(@RequestParam(value="filename")MultipartFile file,HttpSession session){
// String fileName = file.getOriginalFilename();
int result = 0;
try {
result = userService.addUser(file);
} catch (Exception e) {
e.printStackTrace();
}
if(result != 0){
return "excel文件数据导入成功!";
}else{
return "excel数据导入失败!";
}
}
}
7.创建service层,新建接口UserService与接口实现类UserServiceImpl
UserService
package com.xue.service;
import org.springframework.web.multipart.MultipartFile;
public interface UserService {
// 增加用户
public int addUser(MultipartFile file) throws Exception;
}
UserServiceImpl
package com.xue.service.Impl;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.xue.entity.model.User;
import com.xue.repository.dao.UserMapper;
import com.xue.service.UserService;
import com.xue.transcation.MyException;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Override
public int addUser(MultipartFile file) throws Exception{
int result = 0;
SqlSession batchSqlSession = null;
// 存放excel表中所有user细腻信息
List userList = new ArrayList<>();
/**
*
* 判断文件版本
*/
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
InputStream ins = file.getInputStream();
Workbook wb = null;
if(suffix.equals("xlsx")){
wb = new XSSFWorkbook(ins);
}else{
wb = new HSSFWorkbook(ins);
}
/**
* 获取excel表单
*/
Sheet sheet = wb.getSheetAt(0);
/**
* line = 2 :从表的第三行开始获取记录
*
*/
if(null != sheet){
for(int line = 2; line <= sheet.getLastRowNum();line++){
User user = new User();
Row row = sheet.getRow(line);
if(null == row){
continue;
}
/**
* 判断单元格类型是否为文本类型
*/
if(1 != row.getCell(0).getCellType()){
throw new MyException("单元格类型不是文本类型!");
}
/**
* 获取第一个单元格的内容
*/
String username = row.getCell(0).getStringCellValue();
row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
/**
* 获取第二个单元格的内容
*/
String password = row.getCell(1).getStringCellValue();
user.setUsername(username);
user.setPassword(password);
userList.add(user);
}
// 逐条插入
// for(User userInfo:userList){
//
// /**
// * 判断数据库表中是否存在用户记录,若存在,则更新,不存在,则保存记录
// */
// String name = userInfo.getUsername();
//
// int count = userMapper.selectUser(name);
//
// if(0 == count){
// result = userMapper.addUser(userInfo);
// }else{
// result = userMapper.updateUser(userInfo);
// }
//
//
//
// }
// 每次批量插入的数量
int batchCount = 5;
//
int batchLastIndex = batchCount;//每批最后一条数据的下标
batchSqlSession = this.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH,false);
UserMapper mapper = batchSqlSession.getMapper(UserMapper.class);
/**
* 批量插入
*/
for(int index = 0; index < userList.size();){
/**
* 如果数据量小于设置的批量数量,则最后的下标值为实际数量
*/
if(batchLastIndex >= userList.size()){
batchLastIndex = userList.size();
result = result + mapper.importBatchInsert(userList.subList(index,batchLastIndex));
// 清楚缓存
batchSqlSession.clearCache();
break;
}else {
result = result + mapper.importBatchInsert(userList.subList(index, batchLastIndex));
batchSqlSession.clearCache();
index = batchLastIndex;
batchLastIndex = index + (batchCount -1);
}
}
// 将数据提交到数据库,否则执行但是未将数据插入到数据库
batchSqlSession.commit();
}
return result;
}
}
8.编辑UserMapper.java与UserMapper.xml
UserMapper.java,在其最后添加
// 增
int addUser(User user);
// 查
int selectUser(String username);
// 改
int updateUser(User user);
//批量插入数据库
int importBatchInsert(List list);
UserMapper.xml,在其最后添加
insert into user(username,password) values(#{username},#{password})
update user set password = #{password} where username = #{username}
insert into user (username,password)
values
(
#{item.username},
#{item.password}
)
9.新建transcation包,并新建类MyException
package com.xue.transcation;
public class MyException extends RuntimeException {
private static final long serialVersionId = 1L;
public MyException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
10.编辑程序启动类SpringBatchImportApplication
package com.xue;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.xue.repository.dao")
public class SpringBatchImportApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchImportApplication.class, args);
}
}
11. 在src/main/resources templates下新建index.html
Insert title here
导入EXCEL
12.至此程序编写完成,项目整体结构如图:
13.在浏览器输入localhost:8080/index,上传文件成功后数据库信息如图: