springboot整合mybatis,批量新增
Posted liberalartsy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot整合mybatis,批量新增相关的知识,希望对你有一定的参考价值。
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,批量新增的主要内容,如果未能解决你的问题,请参考以下文章