Spring 集合表达式

版权声明:转载请随意! https://blog.csdn.net/qq_41723615/article/details/89187689

只要是开发框架的操作,永远都不可避免的要去进行集合数据的操作处理,在之前一直强调过,Spring认为数据与List集合是等价的,所以如果要想操纵List集合,利用"{内容,内容,...}"的形式就可以完成了。

范例:操作List集合内容

public class TestELSimple {

	 public static void main(String[] args) throws NoSuchMethodException, SecurityException{
		    ExpressionParser parpser = new SpelExpressionParser();
			Expression exp = parpser.parseExpression("{10,20,30}");
			EvaluationContext context = new StandardEvaluationContext();
			List<Integer> list = exp.getValue(context,List.class);
			System.out.println(list);
	 }
}

输出结果:[10, 20, 30]

而如果你只是定义一个空的List集合,那么就不设置内容,例如"{}".但是需要记住的是,此时List集合严格的来讲就是相当于使用了Collections,在这个工具类下面可以创建空集合,但是许多方法都是不支持的实现。

当然,如果现在真定义出了集合,那么也可以利用表达式采用索引的方式进行访问。

范例:索引访问集合

Expression exp = parpser.parseExpression("{10,20,30}[1]");

输出结果:[20]

正常来讲,如果真的要进行开发操作,往往都可以将集合设置为操作的变量进行处理。

范例:设置集合内容

public class TestELSimple {

	 public static void main(String[] args) throws Exception{
			List<String> all = new ArrayList<String>();
			all.add("jianzhu");
			all.add("diyi");
			ExpressionParser parpser = new SpelExpressionParser();
			Expression exp = parpser.parseExpression("#allData[1]");
			EvaluationContext context = new StandardEvaluationContext();
			context.setVariable("allData", all);
			System.out.println(exp.getValue(context));
	 }
}

输出结果:diyi

以上的这个操作设置的是List集合,既然是集合,那么set集合也一定可以设置。

范例:观察Set集合的配置

Set<String> all = new HashSet();

输出结果:diyi

除了List与Set集合之外,那么Map集合一定是不可能少的集合了.

范例:操作Map集合

public class TestELSimple {

	 public static void main(String[] args) throws Exception{
			Map<String,String> all = new HashMap<String, String>();
			all.put("jianzhu","diyi");
			all.put("diyi","jianzhu");
			ExpressionParser parpser = new SpelExpressionParser();
			Expression exp = parpser.parseExpression("#allData['diyi']");
			EvaluationContext context = new StandardEvaluationContext();
			context.setVariable("allData", all);
			System.out.println(exp.getValue(context));
	 }
}

输出结果:jianzhu

除了数据的设置之外,还可以进行数据的修改操作

范例:修改List集合数据

public class TestELSimple {

	 public static void main(String[] args) throws Exception{
			List<String> all = new ArrayList();
			all.add("jianzhu");
			all.add("diyi");
			ExpressionParser parpser = new SpelExpressionParser();
			Expression exp = parpser.parseExpression("#allData[1] = 'jcn'");
			EvaluationContext context = new StandardEvaluationContext();
			context.setVariable("allData", all);
			System.out.println(exp.getValue(context));
	 }
}

输出结果:jcn

那么既然List集合可以修改,那么也可以修改Map集合。

public class TestELSimple {

	 public static void main(String[] args) throws Exception{
		    Map<String,String> all = new HashMap<String, String>();
			all.put("jianzhu","diyi");
			all.put("diyi","jianzhu");
			ExpressionParser parpser = new SpelExpressionParser();
			Expression exp = parpser.parseExpression("#allData['diyi'] = '剑主天下第一'");
			EvaluationContext context = new StandardEvaluationContext();
			context.setVariable("allData", all);
			System.out.println(exp.getValue(context));
			System.out.println(all);
	 }
}

输出结果:

剑主天下第一
{jianzhu=diyi, diyi=剑主天下第一}

实际上在Spring又考虑到集合数据的批量处理问题,所以此处也可以对集合数据内容进行处理。

范例:处理list集合

public class TestELSimple {

	 public static void main(String[] args) throws Exception{
		    List<String> all = new ArrayList();
			all.add("jianzhu");
			all.add("diyi");
			//但是现在处理完成之后改变的并不是已有的集合,已有的集合不会发生变化
			ExpressionParser parpser = new SpelExpressionParser();
			Expression exp = parpser.parseExpression("#allData.!['你好:' + #this]");
			EvaluationContext context = new StandardEvaluationContext();
			context.setVariable("allData", all);
			System.out.println(exp.getValue(context));//相当于这是一个新的集合
			System.out.println(all);
	 }
}

输出结果:

[你好:jianzhu, 你好:diyi]
[jianzhu, diyi]

修改完成之后相当于重新创建了一个新的List集合

在整个过程里面不要忘记了,Map集合也可以进行处理。

范例:处理Map集合

public class TestELSimple {

	 public static void main(String[] args) throws Exception{
		    Map<String,String> all = new HashMap<String, String>();
			all.put("jianzhu","diyi");
			all.put("diyi","jianzhu");
			ExpressionParser parpser = new SpelExpressionParser();//key-value
			Expression exp = parpser.parseExpression("#allData.![#this.key + ' _ ' + #this.value]");
			EvaluationContext context = new StandardEvaluationContext();
			context.setVariable("allData", all);
			System.out.println(exp.getValue(context));
			System.out.println(all);
	 }
}

输出结果:

[jianzhu _ diyi, diyi _ jianzhu]
{jianzhu=diyi, diyi=jianzhu}

这个时候在表达式之中将Map中的key与value进行混合的处理,所以处理完的就是单值的数据也就形成了新的List集合操作数据。

实际上在整个表达式的操作之中也提供有数据的筛选操作支持。

范例:数据的筛选操作

public class TestELSimple {

	 public static void main(String[] args) throws Exception{
		    Map<String,String> all = new HashMap<String, String>();
			all.put("jcn","diyi");
			all.put("diyi","jianzhu");
			ExpressionParser parpser = new SpelExpressionParser();//key-value
			Expression exp = parpser.parseExpression("#allData.?[#this.key == 'jcn']");
			EvaluationContext context = new StandardEvaluationContext();
			context.setVariable("allData", all);
			System.out.println(exp.getValue(context));
	 }
}

输出结果:{jcn=diyi}

 整个筛选的过程里面就可以进行各种类方法的调用(主要是String类的支持方法)。

猜你喜欢

转载自blog.csdn.net/qq_41723615/article/details/89187689