mysql数据库,mybatis,mybatis-plus查询的注意事项

        List<Product> productList = productService.list(new LambdaQueryWrapper<Product>().in(Product::getProdId, prodIds));

简单说,mysql的查询会按照主键排序,当你需要参数的顺序关系时,请使用map,而不是直接for循环获取。

例子:
代码中的prodIds假设为 7,6,3
查询后的list id排序为3,6,7
如果此时你认为查询后的list还是按照之前的ID排序,并以这个关系操作时就会有问题。

后续补充

发现这个问题和索引有关

select p.* from dy_doudian_prod  p where p.product_id in (3508823072759261766,3509226009411078761,35088610555290529230,3509217952245406923) 

使用普通索引时,查询应该通过普通索引查找,但是返回顺序按id排序

select p.product_id from dy_doudian_prod  p where p.product_id in (3508823072759261766,3509226009411078761,35088610555290529230,3509217952245406923) 

使用覆盖索引时 查找结果顺序按照索引顺序排列

select p.product_id from dy_doudian_prod  p where p.id in (1,4,7,8,5);

使用主键索引时 查找结果顺序按照主键索引排序

比较有疑问的第一种情况,第一种情况查询肯定是用索引,如果使用索引那么回表查询后的数据中id一定乱序,而目前情况是排序的,说明做了排序,我们知道排序需要在内存中创建临时排序表,消耗系统资源。

那么当我们没有指定排序参考列时就是默认主键排序吗?

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Spring Boot 是一个快速开发框架,MyBatisMyBatis-Plus 是两个流行的 ORM 框架,Spring Boot 与 MyBatis/MyBatis-Plus 的整合可以帮助我们更快更方便地进行开发。 下面是使用Java Spring Boot整合MyBatis/MyBatis-Plus的步骤: 1. 在 pom.xml 文件添加 MyBatis/MyBatis-PlusMySQL 驱动的依赖: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` 2. 在 application.properties/application.yml 文件配置数据源和 MyBatis/MyBatis-Plus 的配置信息: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.type-aliases-package=com.example.demo.entity mybatis-plus.mapper-locations=classpath:/mapper/*.xml ``` 3. 在 Spring Boot 启动类上添加 `@MapperScan` 注解,指定 MyBatis/MyBatis-Plus 的 Mapper 所在的包: ```java @SpringBootApplication @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4. 创建实体类和 Mapper 接口: ```java public class User { private Long id; private String name; private Integer age; // 省略 getter 和 setter 方法 } @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 5. 创建 Mapper 的 XML 文件(如果使用 MyBatis-Plus 可以省略此步骤): ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </resultMap> <select id="selectById" resultMap="BaseResultMap"> select * from user where id = #{id} </select> </mapper> ``` 6. 在 Service 使用 Mapper: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectById(id); } } ``` 这样就完成了 Java Spring Boot 整合 MyBatis/MyBatis-Plus 的基本步骤。需要注意的是,在使用 MyBatis-Plus 的情况下,Mapper 接口无需自己编写 CRUD 操作的方法,直接继承 `BaseMapper` 接口即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值