Java中使用C#的Linq

一.介绍  

  由于习惯了C#中Linq的强大功能,遗憾的是,在Java里没有这样强大的功能支持,于是尝试在网上去寻找有无这样的实现。结果还真发现有这么一套开源代码,在Java上实现在C#上有的功能,可以说是用爱发电吧。

  上该源码的git地址 https://github.com/timandy/linq

  Linq查询的优势是,在做条件筛选、排序等功能时可以用最少的代码去完成。同时,比起stream api更有优势,文档上强调,在大多数复杂情况下,Linq比stream api更快。

  这里的Linq使用只有方法链的使用,类似sql的表达也就C#的语法糖才有,在Java上不支持。

二.使用

  maven安装。

<dependency>
    <groupId>com.bestvike</groupId>
    <artifactId>linq</artifactId>
    <version>3.1.0</version>
</dependency>

  准备测试数据。

public class User {

    private Integer id;
    private String name;
    private Integer age;
    private Integer money;
    private Integer roleID;
    private Role role;

    //...public User(Integer id, String name, Integer age, Integer money, Integer roleID) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.money = money;
        this.roleID = roleID;
    }
}

public class Role {

    private Integer id;
    private String name;

    //...
public Role(Integer id, String name) { this.id = id; this.name = name; } } List<User> list = new ArrayList<>(); list.add(new User(1, "张三", 15, 100, 1)); list.add(new User(2, "李四", 16, 200, 1)); list.add(new User(3, "王五", 17, 400, 2)); list.add(new User(4, "宋六", 17, 250, 2)); list.add(new User(5, "龙七", 17, 250, 2)); List<Role> roleList = new ArrayList<>(); roleList.add(new Role(1, "普通用户")); roleList.add(new Role(2, "超级用户"));

  带where查询

//方法链
var ddd = Linq.of(list).where(x -> x.getAge() > 15).toList();

//sql
String sql = "select * from user where age>15";

  简单的函数计算(max、min、count、sum)

//方法链
//var ddd = Linq.of(list).max(x -> x.getAge());//获取最大的年龄
//var ddd = Linq.of(list).min(x -> x.getAge());//获取最小的年龄
//var ddd = Linq.of(list).count();//获取总数
var ddd = Linq.of(list).sumInt(x -> x.getAge());//获取总数

//sql
//String sql = "select max(age) from user";
//String sql = "select min(age) from user";
//String sql = "select count(1) from user";
String sql = "select sum(age) from user";

  排序

//方法链
//var ddd = Linq.of(list).orderBy(x -> x.getAge());//正序
var ddd = Linq.of(list).orderByDescending(x -> x.getAge());//倒序

//sql
//String sql = "select * from user order by age";
String sql = "select * from user order by age desc";

  top1

//方法链
var ddd = Linq.of(list).firstOrDefault();

//sql
String sql = "select top 1 from user";

   跳过前面多少条数据,取剩下的数据

//方法链
var ddd = Linq.of(list).orderByDescending(x -> x.getAge()).skip(1);

//sql
String sql = "select * from (select ROW_NUMBER() over(order by age desc) as rowNum,*) t wher rowNum>1";

  分页查询

//方法链
//跳过第一条,取两条,即取第二条到第四条
var ddd = Linq.of(list).orderByDescending(x -> x.getAge()).skip(1).take(3);

//sql
String sql = "select * from (select ROW_NUMBER() over(order by age desc) as rowNum,*) t wher rowNum>1 and rowNum<=4";

  包含,相当于like

//方法链
var ddd = Linq.of(list).where(x->x.getName().contains(""));

//sql
String sql = "select * from user where name like '%李%'";

  分组group by

//方法链
var ddd = Linq.of(list).groupBy(x -> x.getAge()).toList();
for (var t : ddd) {
    System.out.println(t.getKey() + "--" + t.minInt(x -> x.getMoney()) + "--" + t.maxInt(x -> x.getMoney()) + "--" + t.sumInt(x -> x.getMoney()));
}

//sql
String sql = "select age,min(money),max(money),sum(money) from user group by age";

  sql中的in

//方法链
List<Integer> ages = new ArrayList<>();
ages.add(15);
ages.add(16);
var ddd = Linq.of(list).where(x -> ages.contains(x.getAge()));

//sql
String sql = "select * from user where age in (15,16)";

三.总结

  由于对这个插件不是太熟悉,个别api不太了解用法。不过,基本的查询使用应该足够了。

  

posted @ 2021-02-09 14:19  shine声  阅读(3179)  评论(0编辑  收藏  举报