Mybatis-Plus 查询获取数据集最大值最小值

使用Mybatisplus进行查询时, 遇到了需要查询最大值的情况,特此记录一下。虽然能解决问题,但不推荐使用。

在serviceImpl中编写如下代码

最大值:

this.getOne(new LambdaQueryWrapper<Test>().orderByDesc(Test::getSorlt).last("limit 1"));

最小值

this.getOne(new LambdaQueryWrapper<Test>().orderByAsc(Test::getSorlt).last("limit 1"));
  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在SpringBoot中使用MyBatis-Plus获取数据源的步骤如下: 1. 在 pom.xml 中引入 MyBatis-Plus 和相关的数据库驱动依赖。 2. 在 application.properties 或 application.yml 中配置数据源信息,例如: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 创建一个数据源配置类,例如: ```java @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return new DruidDataSource(); } @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); return sessionFactory.getObject(); } } ``` 在这个配置类中,使用 @ConfigurationProperties(prefix = "spring.datasource") 注解将配置文件中的数据源信息注入到 DataSource 对象中,然后将 DataSource 对象设置到 SqlSessionFactoryBean 中。 4. 在 Mapper 接口中使用 MyBatis-Plus 提供的注解来操作数据库,例如: ```java @Repository public interface UserMapper extends BaseMapper<User> { } ``` 在这个 Mapper 接口中,继承了 MyBatis-Plus 提供的 BaseMapper 接口,就可以使用其中的方法来操作数据库了。 总的来说,使用 MyBatis-Plus 获取数据源的步骤与使用 MyBatis 是类似的,只是需要额外引入 MyBatis-Plus 的依赖,并且在 Mapper 接口中使用 MyBatis-Plus 提供的注解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值