💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## SELECT简单的条件查询 我们还是以User为例,我们需要查询这条SQL ``` SELECT * FROM `user` WHERE `id` BETWEEN 1 AND 1640 AND `name` LIKE '%t%' AND `create_time` IS NOT NULL ORDER BY id desc ``` 直接上代码: ```java //JDK8 强烈推荐使用!! LambdaQuery<User> query = sqlManager.lambdaQuery(User.class); query.andBetween(User::getId,1,1640) .andLike(User::getName,"%t%") .andIsNotNull(User::getCreateTime) .desc(User::getId).select(); //普通用法 Query<User> query = sqlManager.query(User.class); List<User> list = query.andBetween("id", 1, 1640) .andLike("name", "%t%") .andIsNotNull("create_time") .orderBy("id desc").select(); ``` 是不是感觉和写SQL一样爽。 如果我们只要查询其中的几个字段怎么办?比如我只要name和id字段,SQL如下: ``` SELECT name,id FROM `user` ``` Query也提供了定制字段的方法,只要传入你需要的字段名即可: ```java //JDK8 强烈推荐使用!! LambdaQuery<User> query = sqlManager.lambdaQuery(User.class); query.select(User::getName,User::getId); //普通用法 Query<User> query = sqlManager.query(User.class); List<User> list = query.select("name", "id"); ``` 比如时间比较大小: ```sql SELECT name,id FROM `user` WHERE `id` = 1637 AND `create_time` < now() AND `name` = 'test' ``` ```java //JDK8 强烈推荐使用!! LambdaQuery<User> query = sqlManager.lambdaQuery(User.class); query.select(User::getName,User::getId); //普通用法 Query<User> query = sqlManager.query(User.class); List<User> list = query.andEq("id", 1637) .andLess("create_time", new Date()) .andEq("name", "test") .select("name", "id"); ``` 有的同学会说,OR子句怎么用,和AND一样简单: ``` SELECT * FROM `user` WHERE `name` = 'new name' OR `id` = 1637 limit 0 , 10 ``` ``` LambdaQuery<User> query = sqlManager.lambdaQuery(User.class); query.andEq(User::andName,"new name") .orEq(User::getId,1637) .limit(1,10) .select(); ``` 为了兼容其他数据库,这里limit都是统一从1开始哦,后面也会提到。