首页 > 编程语言 > 详细

springboot整合mybatis,批量新增

时间:2021-06-16 11:49:17      阅读:40      评论:0      收藏:0      [点我收藏+]

pom

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>

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

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
启动类

技术分享图片

 

 application.yml

server:
port: 8082

spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/liberalartsy?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver

mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.fh.entity

#showSql
logging:
level:
com:
example:
mapper : debug

技术分享图片

 

 批量新增

@RequestMapping("bachAdd")
public String bachAdd(@RequestBody List<User> userList){
userService.bachAdd(userList);

return "成功";
}
controller

技术分享图片

 

mapper

void bachAdd(@Param("list") List<User> list);

技术分享图片

 

mapper.xml

<insert id="bachAdd" parameterType="java.util.List">
insert into t_user
(
username,
age
)
VALUES
<foreach collection="list" item="item" separator=",">
(
#{item.username},
#{item.age}
)
</foreach>
</insert>

技术分享图片

 

springboot整合mybatis,批量新增

原文:https://www.cnblogs.com/liberalartsy/p/14888509.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!