mybaits plus 3.5 的default boolean updateBatchById(Collection<T> entityList) { return Db.updateBatchById(entityList); } 改成 mybatis plus 3.3的写法
时间: 2024-02-03 13:04:16 浏览: 22
MyBatis Plus 3.3 中没有 Db.updateBatchById(Collection<T> entityList) 这个方法,因此也没有相应的默认实现。但是,我们可以手动实现一个类似的方法,以达到相同的效果。
下面是一个可能的实现:
```
public boolean updateBatchById(List<T> entityList) {
if (CollectionUtils.isEmpty(entityList)) {
return false;
}
int rows = 0;
for (T entity : entityList) {
rows += this.updateById(entity);
}
return rows == entityList.size();
}
```
这个方法的实现比较简单,就是遍历实体列表,逐个调用 updateById 方法更新数据库,并统计更新成功的行数。最终,如果更新成功的行数等于实体列表的大小,就说明批量更新操作成功,否则就失败。
需要注意的是,这个实现方式可能会对数据库造成较大的压力,因为它需要执行多条 SQL 语句,而且每个 SQL 语句都需要与数据库建立连接、执行、关闭连接等操作。因此,在批量更新数据时,应该尽量减少 SQL 语句的数量,以提高效率和性能。
相关问题
spring boot 2.1.1.RELEASE 和mybaits plus 3.5 启动报错 Factory method 'mybatisPlusInterceptor' threw exception; nested exception is java.lang.NoSuchMethodError: net.sf.jsqlparser.schema.Column.withColumnName(Ljava/lang/String;)Lnet/sf/jsqlparser/schema/Co
这个问题是由于你的项目中使用的 Mybatis Plus 版本与 JSqlParser 版本不兼容引起的。Mybatis Plus 3.5 版本默认使用的是 JSqlParser 2.1 版本,而与之前版本不同的是,JSqlParser 2.1 版本中的 `Column` 类中没有 `withColumnName` 方法。因此,如果你的项目中同时使用了 JSqlParser 2.1 版本以下的版本,就会导致 `NoSuchMethodError` 异常。
解决这个问题的方法是升级项目中的 JSqlParser 版本到 2.1 或以上版本。如果你的项目中使用的是 Maven,可以在 `pom.xml` 文件中将 JSqlParser 的版本号升级到 2.1 或以上版本:
```xml
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>2.1</version>
</dependency>
```
如果你的项目中使用的是 Gradle,可以在 `build.gradle` 文件中将 JSqlParser 的版本号升级到 2.1 或以上版本:
```groovy
dependencies {
// ...
implementation 'com.github.jsqlparser:jsqlparser:2.1'
// ...
}
```
升级 JSqlParser 版本后,再重新启动应用程序,这个问题应该就会被解决了。
spring 整合mybaitsPlus 代码生成器依赖
要将 MyBatis-Plus 代码生成器集成到 Spring 项目中,需要在 Maven 或 Gradle 中添加以下依赖:
Maven 依赖:
```xml
<dependencies>
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- MyBatis-Plus 代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.2</version>
</dependency>
<!-- Velocity 模板引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
```
Gradle 依赖:
```groovy
dependencies {
// MyBatis-Plus
implementation 'com.baomidou:mybatis-plus-boot-starter:3.4.2'
// MyBatis-Plus 代码生成器
implementation 'com.baomidou:mybatis-plus-generator:3.4.2'
// Velocity 模板引擎
implementation 'org.apache.velocity:velocity-engine-core:2.2'
}
```
值得注意的是,代码生成器需要使用 Velocity 模板引擎,请确保在项目中添加了该依赖。同时,在 `application.yml` 或 `application.properties` 配置文件中添加 MyBatis-Plus 的配置信息以及代码生成器的配置信息即可。