Learn from the beginning Spring-1.16 spel operations on Collections (2)-query collection and projection element collection

Source: Internet
Author: User

In this chapter we will discuss the collection of queries and the collection of projection elements.

Let's use an example below to illustrate these two sections.

1.domain

Cake Category: (unchanged)

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_21;public class Cake {private String name = "";p rivate 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;}}


Chef class:

In the Chef class below we have added two fields and methods

One is to query the collection prepared by cakes, put cake object collection

A collection of cakenames that is prepared for the projection element, and the name of the 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 ArrayLi st<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 = "";p ublic 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. Test class

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 {@Autowiredprivate ApplicationContext applicationcontext;@ testpublic 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. configuration file (emphasis)

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-3.1.xsdhttP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/ Spring-jpa-1.0.xsdhttp://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>

Note the point:

(1) The query set has three kinds of symbols

.? [] Returns a collection

. ^[] Returns the first object of a collection

. $[] Returns the last object of the collection

(2) Mapping element set symbol

.! [] Returns a collection

(3) When querying elements, we can introduce the contrast to compare some properties, for example: Cakes.$[size > 6]

(4) When alluding to a collection of elements, the brackets inside the set is a property of an element in the collection, and the return is the collection of all elements in the collection of this property


Test output:

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


Summary: In this chapter we mainly introduce the query collection and the projection element collection.


Catalog: http://blog.csdn.net/raylee2007/article/details/50611627

My github:https://github.com/raylee2015/my_new_spring.



Learn from the beginning Spring-1.16 spel operations on Collections (2)-query collection and projection element collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.