MyBatis-Plus使用@Insert注解插入List集合方式及说明

       简明:记录使用@Insert插入List集合的方式及易错地方

1、使用@Insert注解,可不写对应xml配置文件,作用是等价的(注意格式

@Param注解:对应foreach循环接收的参数(必须写)

<script/>标签:标记是JavaScript文本,对foreach语句进行解析(必须写)

foreach标签: (1)collection对应@Param注解集合参数   

                        (2)item对应每个集合内的元素

2、易错注意地方

(1)缺少@Param注解(报错:foreach无法获取对应集合参数

报错:org.apache.ibatis.binding.BindingException: Parameter 'studentList' not found. Available parameters are [collection, list]

(2)@Insert注解中缺少<script/>标签(报错:无法解析foreach

报错:org.apache.ibatis.binding.BindingException: Parameter 'item' not found. Available parameters are [studentList, param1] 

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot 是一个基于 Spring 框架的快速开发框架,而 MyBatis-Plus 则是 MyBatis 的增强工具,提供了很多方便的查询和操作数据库的 API。下面是 Spring BootMyBatis-Plus使用说明: 1. 配置 MyBatis-Plus 在 pom.xml 文件中添加 MyBatis-Plus 的依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.3</version> </dependency> ``` 在 application.yml 文件中添加数据库连接信息和 MyBatis-Plus 的配置: ``` spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: mapper-locations: classpath:mapper/*.xml global-config: db-config: id-type: auto field-strategy: not_empty table-prefix: mp_ ``` 2. 创建实体类和 Mapper 接口 创建实体类和对应的 Mapper 接口,使用 @TableName 注解指定表名,@TableField 注解指定字段名。 ``` @Data @TableName("user") public class User { @TableId(type=IdType.AUTO) private Long id; @TableField("username") private String username; @TableField("password") private String password; } @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 3. 使用 MyBatis-Plus 的 API 进行数据库操作 使用 MyBatis-Plus 的 API 进行数据库操作非常方便,比如查询所有用户: ``` @Autowired private UserMapper userMapper; public List<User> getAllUsers() { return userMapper.selectList(null); } ``` 其他常用的 API 包括:insert、updateById、deleteById、selectById 等。 以上就是 Spring BootMyBatis-Plus 的简单使用说明,更多详细的使用方法可以参考 MyBatis-Plus 的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值