从头认识Spring-1.16 SpEl对集合的操作-查询集合以及投影元素集合
Posted 李灵晖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从头认识Spring-1.16 SpEl对集合的操作-查询集合以及投影元素集合相关的知识,希望对你有一定的参考价值。
这一章节我们来讨论一下查询集合以及投影元素集合。
我们下面用一个例子说明上面的这两个部分。
1.domain
蛋糕类:(不变)
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21;
public class Cake {
private String name = "";
private double size = 0;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
}
在下面的厨师类里面我们增加了两个域和方法
一个是为了查询集合准备的cakes,放cake的对象集合
一个是为了投影元素准备的cakeNames,放cake的名称的集合
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21;
import java.util.ArrayList;
import java.util.Iterator;
public class Chief {
private Cake cake = null;
private ArrayList<Cake> cakes = null;
private ArrayList<String> cakeNames = null;
public ArrayList<String> getCakeNames() {
return cakeNames;
}
public void setCakeNames(ArrayList<String> cakeNames) {
this.cakeNames = cakeNames;
}
public ArrayList<Cake> getCakes() {
return cakes;
}
public void setCakes(ArrayList<Cake> cakes) {
this.cakes = cakes;
}
public void canMakeCakes() {
for (Iterator<Cake> iterator = cakes.iterator(); iterator.hasNext();) {
Cake cake = iterator.next();
System.out.println(name + " can make " + cake.getName());
}
}
public void nameOfMakeCakes() {
for (Iterator<String> iterator = cakeNames.iterator(); iterator.hasNext();) {
String name = iterator.next();
System.out.println(this.name + " can make " + name);
}
}
private String name = "";
public Cake getCake() {
return cake;
}
public String getName() {
return name;
}
public Cake makeOneCake() {
return cake;
}
public void setCake(Cake cake) {
this.cake = cake;
}
public void setName(String name) {
this.name = name;
}
}
2.测试类
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_21/ApplicationContext-test.xml" })
public class CakeTest {
@Autowired
private ApplicationContext applicationContext;
@Test
public void testChief() {
Chief jack = (Chief) applicationContext.getBean("jack");
jack.canMakeCakes();
Chief mike = (Chief) applicationContext.getBean("mike");
mike.nameOfMakeCakes();
Chief rose = (Chief) applicationContext.getBean("rose");
System.out.println("rose can make " + rose.getCake().getName());
}
}
3.配置文件(重点)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<util:list id="cakes">
<bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21.Cake"
p:name="blueberry cheese cake" p:size="5" scope="prototype" />
<bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21.Cake"
p:name="chocolate cheese cake" p:size="6" scope="prototype" />
<bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21.Cake"
p:name="banana oatmel mousse cake" p:size="7" scope="prototype" />
<bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21.Cake"
p:name="vanilla eclair" p:size="8" scope="prototype" />
<bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21.Cake"
p:name="ligueur perfumed triplet cake" p:size="5.5" scope="prototype" />
</util:list>
<bean id="jack"
class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21.Chief"
p:name="jack">
<property name="cakes" value="#{cakes.?[size > 6]}" />
</bean>
<bean id="mike"
class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21.Chief"
p:name="mike">
<property name="cake" value="#{cakes.^[size > 6]}" />
<property name="cakeNames" value="#{cakes.![name]}" />
</bean>
<bean id="rose"
class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21.Chief"
p:name="rose">
<property name="cake" value="#{cakes.$[size > 6]}" />
</bean>
</beans>
注意点:
(1)查询集合有三种符号
.?[ ] 返回一个集合
.^[ ]返回集合的第一个对象
.$[ ]返回集合的最后一个对象
(2)影射元素集合符号
.![ ] 返回一个集合
(3)在查询元素的时候,我们可以引入对比符来对比某种属性,例如:cakes.$[size > 6]
(4)影射元素集合的时候,中括号里面放入的是集合里面某个元素的某个属性,返回的就是集合里面所有元素的这个属性的集合
测试输出:
jack can make banana oatmel mousse cake
jack can make vanilla eclair
mike can make blueberry cheese cake
mike can make chocolate cheese cake
mike can make banana oatmel mousse cake
mike can make vanilla eclair
mike can make ligueur perfumed triplet cake
rose make vanilla eclair
总结:这一章节我们主要介绍了查询集合以及投影元素集合。
目录:http://blog.csdn.net/raylee2007/article/details/50611627
我的github:https://github.com/raylee2015/my_new_spring
以上是关于从头认识Spring-1.16 SpEl对集合的操作-查询集合以及投影元素集合的主要内容,如果未能解决你的问题,请参考以下文章
从头认识Spring-1.15 对SpEl的值的操作-数值运算
从头认识Spring-1.15 对SpEl的值的操作-对比值
从头认识Spring-1.15 对SpEl的值的操作-数值运算