EasyExcel分批插入,如果后面的数据校验失败了,前面插入的数据会回滚吗?
时间: 2024-03-18 18:42:52 浏览: 23
EasyExcel分批插入数据时,如果后面的数据校验失败,前面已插入的数据不会自动回滚。你需要手动处理回滚事务。一种常用的方式是对每一批数据插入之前开启一个事务,在所有数据都插入成功后提交事务。如果发生校验错误,可以回滚事务。同时,你可以使用Spring框架中的声明式事务(@Transactional注解)来简化事务处理。在这种情况下,如果校验失败,Spring会自动回滚事务,撤销之前的所有数据库操作。
相关问题
使用Spring框架中的声明式事务实现EasyExcel分批插入,如果后面的数据校验失败了,前面插入的数据回滚
使用Spring框架中的声明式事务实现EasyExcel分批插入,在后面的数据校验失败时,前面插入的数据会自动回滚,可以按照以下步骤进行实现:
1. 配置数据源和事务管理器
在Spring配置文件中配置数据源和事务管理器,例如:
```
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
```
2. 配置声明式事务
在Spring配置文件中配置声明式事务,例如:
```
<tx:annotation-driven transaction-manager="transactionManager"/>
```
3. 编写Service层代码
在Service层中编写针对Excel数据分批插入的方法,并使用@Transactional注解进行事务控制,例如:
```
@Service
public class ExcelService {
@Autowired
private ExcelDao excelDao;
@Transactional(rollbackFor = Exception.class)
public void batchInsert(List<List<Object>> dataList) throws Exception {
for (List<Object> data : dataList) {
// 校验数据
if (!validate(data)) {
throw new Exception("数据校验失败");
}
// 插入数据
excelDao.insert(data);
}
}
private boolean validate(List<Object> data) {
// 数据校验逻辑
return true;
}
}
```
4. 编写Controller层代码
在Controller层中使用EasyExcel读取Excel文件,并调用Service层的方法进行分批插入,例如:
```
@Controller
public class ExcelController {
@Autowired
private ExcelService excelService;
@RequestMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) throws Exception {
List<List<Object>> dataList = readExcel(file);
excelService.batchInsert(dataList);
return "success";
}
private List<List<Object>> readExcel(MultipartFile file) throws Exception {
// 使用EasyExcel读取Excel文件,返回数据列表
return null;
}
}
```
在这种实现方式下,如果后面的数据校验失败,前面插入的数据会自动回滚。需要注意的是,使用@Transactional注解时,需要指定rollbackFor属性,以便在抛出指定类型的异常时触发事务回滚。在本例中,我们将其设置为Exception.class,表示回滚所有类型的异常。
使用spring的事务和EasyExcel实现excel分批插入数据库,如果后面的数据校验失败,则前面的数据回滚
使用Spring的事务和EasyExcel实现Excel分批插入数据库,在后面的数据校验失败时,前面的数据会自动回滚。可以按照以下步骤进行实现:
1. 配置数据源和事务管理器
在Spring配置文件中配置数据源和事务管理器,例如:
```xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
```
2. 配置声明式事务
在Spring配置文件中配置声明式事务,例如:
```xml
<tx:annotation-driven transaction-manager="transactionManager"/>
```
3. 编写Service层代码
在Service层中编写针对Excel数据分批插入的方法,并使用@Transactional注解进行事务控制,例如:
```java
@Service
public class ExcelService {
@Autowired
private ExcelDao excelDao;
@Transactional(rollbackFor = Exception.class)
public void batchInsert(List<List<Object>> dataList) throws Exception {
try {
for (List<Object> data : dataList) {
// 校验数据
if (!validate(data)) {
throw new Exception("数据校验失败");
}
// 插入数据
excelDao.insert(data);
}
} catch (Exception e) {
throw e;
}
}
private boolean validate(List<Object> data) {
// 数据校验逻辑
return true;
}
}
```
4. 编写Controller层代码
在Controller层中使用EasyExcel读取Excel文件,并调用Service层的方法进行分批插入,例如:
```java
@Controller
public class ExcelController {
@Autowired
private ExcelService excelService;
@RequestMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) throws Exception {
List<List<Object>> dataList = readExcel(file);
excelService.batchInsert(dataList);
return "success";
}
private List<List<Object>> readExcel(MultipartFile file) throws Exception {
// 使用EasyExcel读取Excel文件,返回数据列表
return null;
}
}
```
在这种实现方式下,如果后面的数据校验失败,前面插入的数据会自动回滚。需要注意的是,使用@Transactional注解时,需要指定rollbackFor属性,以便在抛出指定类型的异常时触发事务回滚。在本例中,我们将其设置为Exception.class,表示回滚所有类型的异常。同时,为了确保事务回滚,我们在Service层的catch块中重新抛出异常。