项目名称:海通天枢平台
KOCA版本 :5.0.1
KOCA模块 :mybatis-plus
模块版本 :3.4.3.4
场景 :
问题 :在执行savebatch和saveOrUpdateBatch时会把批量处理变成单条执行
报错细节 :
尝试解决方案:
DEMO验证:
父pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.szkingdom.koca.boot</groupId>
<artifactId>koca-boot-parent</artifactId>
<version>5.0.1</version>
<relativePath/>
</parent>
<packaging>pom</packaging>
<modelVersion>4.0.0</modelVersion>
<groupId>com.szkingdom.sample</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>koca-data-samples</artifactId>
<properties>
<project-version>5.0.1</project-version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>koca-datasource-samples</module>
<module>koca-mybatis-plus-samples</module>
<module>koca-jpa-samples</module>
<module>koca-mybatis-samples</module>
<module>koca-jdbc-samples</module>
<module>koca-dtc-samples</module>
<module>spring-ldap-samples</module>
<module>koca-shardingsphere-samples</module>
<module>alibaba-seata-samples</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.szkingdom.koca.cloud</groupId>
<artifactId>koca-cloud-dependencies</artifactId>
<version>${project-version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.szkingdom.koca.admin</groupId>
<artifactId>koca-admin-dependencies</artifactId>
<version>${project-version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.szkingdom.sample</groupId>
<artifactId>koca-data-samples</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>szkingdom.yf.sample</groupId>
<artifactId>koca-mybatis-plus-samples</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 多数据源 -->
<dependency>
<groupId>com.szkingdom.koca.base</groupId>
<artifactId>koca-multi-datasource</artifactId>
</dependency>
<!-- koca-mybatis -->
<!-- <dependency>-->
<!-- <groupId>com.szkingdom.koca.base</groupId>-->
<!-- <artifactId>koca-mybatis</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.szkingdom.koca.boot</groupId>
<artifactId>koca-boot-starter-mybatis</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.h2database</groupId>-->
<!-- <artifactId>h2</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Application.java
@MapperScan("com.szkingdom.sample.mybatisplus.crud.mapper")
@SpringBootApplication
public class CrudApplication {
public static void main(String[] args) {
SpringApplication.run(CrudApplication.class, args);
}
}
User.java
package com.szkingdom.sample.mybatisplus.crud.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* <p>
* 用户实体对应表 user
* </p>
*
* @author hubin
* @since 2018-08-11
*/
@Data
@Accessors(chain = true)
public class User {
private Long id;
private String name;
private Integer age;
private String email;
@TableField(exist = false)
private String ignoreColumn = "ignoreColumn";
@TableField(exist = false)
private Integer count;
}
UserMapper.java
package com.szkingdom.sample.mybatisplus.crud.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.szkingdom.sample.mybatisplus.crud.entity.User;
/**
* <p>
* MP 支持不需要 UserMapper.xml 这个模块演示内置 CRUD 咱们就不要 XML 部分了
* </p>
*
* @author hubin
* @since 2018-08-11
*/
public interface UserMapper extends BaseMapper<User> {
}
UserService.java
package com.szkingdom.sample.mybatisplus.crud.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.szkingdom.sample.mybatisplus.crud.entity.User;
public interface UserService extends IService<User> {
// 可以在这里声明额外的业务方法
}
UserServiceImpl.java
package com.szkingdom.sample.mybatisplus.crud.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.szkingdom.sample.mybatisplus.crud.entity.User;
import com.szkingdom.sample.mybatisplus.crud.mapper.UserMapper;
import com.szkingdom.sample.mybatisplus.crud.service.UserService;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
// 实现额外的业务逻辑
}
数据库表:
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
ServiceTest.java
package com.szkingdom.sample.mybatisplus.crud;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.szkingdom.sample.mybatisplus.crud.entity.User;
import com.szkingdom.sample.mybatisplus.crud.service.UserService;
@SpringBootTest
public class ServiceTest {
@Autowired
private UserService userService;
@Test
public void TestBatch() {
List<User> list = new ArrayList<>();
list.add(new User().setName("张三").setAge(18).setEmail(""));
list.add(new User().setName("李四").setAge(19).setEmail(""));
list.add(new User().setName("王五").setAge(20).setEmail(""));
userService.saveBatch(list,3);
}
}
启动测试,单点调试
测试结果:KOCA框架不会把mybatis-plus批量处理(savebatch)变成单条执行。