亲宝软件园·资讯

展开

Mybatis-Plus多主键批量保存

李长渊哦 人气:0

一、依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.jeffreyning</groupId>
            <artifactId>mybatisplus-plus</artifactId>
            <version>1.5.1-RELEASE</version>
            <scope>compile</scope>
        </dependency>

二、启动类注解

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
@EnableMPP
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

三、表结构

CREATE TABLE `product` (
  `type` varchar(255) NOT NULL,
  `number` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`number`,`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

四、配置文件

server:
  port: 8888

#数据库配置
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/avlicy?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&allowMultiQueries=true&rewriteBatchedstatements=true
      username: root
      password: a123456
      # 连接池的配置信息
      # 初始化大小,最小,最大
      initial-size: 5
      min-idle: 5
      max-active: 20


#mybatis-plus
mybatis-plus:
  #映射mapper.xml文件存放路径
  mapper-locations: classpath:/mapper/*Mapper.xml
  #实体扫描,多个package用逗号或者分号分隔
  type-aliases-package: com.example.demo.entity.base,com.example.demo.entity.integration
  configuration:
    #下划线转驼峰配置
    map-underscore-to-camel-cas: true
    #使用二级缓存容易出现脏读,建议避免使用二级缓存
    cache-enabled: false
    #指定 MyBatis 应如何自动映射列到字段或属性。 NONE 表示取消自动映射;PARTIAL 只会自动映射没有定义嵌套结果集映射的结果集。
    #FULL 会自动映射任意复杂的结果集(无论是否嵌套)。默认是partial,这是一种全局设置
    auto-mapping-behavior: full
    #控制台输出日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl



五、代码

1、实体类

@Data
public class Product implements Serializable {

    @MppMultiId
    @TableField(value = "type")
    private String type;

    @MppMultiId
    @TableField(value = "number")
    private Integer number;

    @TableField(value = "name")
    private String name;
}

2、持久层

public interface ProductMapper extends MppBaseMapper<Product> {
}

3、服务层

public interface ProductService extends IMppService<Product> {
}
@Service
public class ProductServiceImpl extends MppServiceImpl<ProductMapper, Product> implements ProductService {
}

4、逻辑层

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @PostMapping("/saveProduct")
    public List<Product> querySchoolStudent2(@RequestBody List<Product> product){
        productService.saveOrUpdateBatchByMultiId(product);
        return product;
    }
}

六、测试

Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b]
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@391601d9] will be managed by Spring
==>  Preparing: SELECT type,number,name FROM product WHERE number=? and type=?
==> Parameters: 1(Integer), 电脑(String)
<==    Columns: type, number, name
<==        Row: 电脑, 1, 红米
<==      Total: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b]
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@391601d9] will be managed by Spring
==>  Preparing: UPDATE product SET type=?, number=?, name=? WHERE number=? and type=?
==> Parameters: 电脑(String), 1(Integer), 红米(String), 1(Integer), 电脑(String)
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] from current transaction
==>  Preparing: SELECT type,number,name FROM product WHERE number=? and type=?
==> Parameters: 1(Integer), 冰箱(String)
<==    Columns: type, number, name
<==        Row: 冰箱, 1, 美的
<==      Total: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b]
==> Parameters: 冰箱(String), 1(Integer), 美的(String), 1(Integer), 冰箱(String)
Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b]
Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b]

加载全部内容

相关教程
猜你喜欢
用户评论