Beanを作成
手順
-
リポジトリーツリービューで[Code] (コード) > [Global Beans] (グローバルBean)と展開し、Beansノードを右クリックします。コンテキストメニューで[Create Bean] (Beanを作成)を選択します。
- [New Bean] (新規Bean)ウィザードが開きます。[Name] (名前)フィールドにBeanの名前(このユースケースではCustomer)を入力します。
-
デザインワークスペースに次のコードを入力します。
package beans; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; /** * <p>Java class for customer complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType name="customer"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="firstName" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="lastName" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="city" type="{http://www.w3.org/2001/XMLSchema}string"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "customer", propOrder = { "firstName", "lastName", "city" }) @XmlRootElement(name = "customer") public class Customer { @XmlElement(required = true) protected String firstName; @XmlElement(required = true) protected String lastName; @XmlElement(required = true) protected String city; /** * Gets the value of the firstName property. * * @return * possible object is * {@link String } * */ public String getFirstName() { return firstName; } /** * Sets the value of the firstName property. * * @param value * allowed object is * {@link String } * */ public void setFirstName(String value) { this.firstName = value; } /** * Gets the value of the lastName property. * * @return * possible object is * {@link String } * */ public String getLastName() { return lastName; } /** * Sets the value of the lastName property. * * @param value * allowed object is * {@link String } * */ public void setLastName(String value) { this.lastName = value; } /** * Gets the value of the city property. * * @return * possible object is * {@link String } * */ public String getCity() { return city; } /** * Sets the value of the city property. * * @param value * allowed object is * {@link String } * */ public void setCity(String value) { this.city = value; } }
- Ctrl + Sを押し、Beanを保存します。
-
この操作を繰り返し、次のコードを使ってCustomersという別のBeanを作成します:
package beans; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * <p>Java class for anonymous complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="customer" type="{http://talend.org//services/esb/registry/soap/2012/09}customer" maxOccurs="unbounded" minOccurs="0"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.PROPERTY) @XmlRootElement(name = "customers") public class Customers { private Map<Long, Customer> customers = new HashMap<Long, Customer>(); private long currentId = 1; public static class Entry extends Customer { public Long id; } public Customers() { }; /** * Gets the value of the customer property. * * <p> * This accessor method returns a reference to the live list, * not a snapshot. Therefore any modification you make to the * returned list will be present inside the JAXB object. * This is why there is not a <CODE>set</CODE> method for the customer property. * * <p> * For example, to add a new item, do as follows: * <pre> * getCustomer().add(newItem); * </pre> * * * <p> * Objects of the following type(s) are allowed in the list * {@link Customer } * * */ @XmlElement(name="customer") public List<Entry> getCustomers() { List<Entry> adaptedMap = new ArrayList<Entry>(); for(Map.Entry<Long, Customer> mapEntry : customers.entrySet()) { Entry entry = new Entry(); entry.id = mapEntry.getKey(); entry.setFirstName(mapEntry.getValue().getFirstName()); entry.setLastName(mapEntry.getValue().getLastName()); entry.setCity(mapEntry.getValue().getCity()); adaptedMap.add(entry); } return adaptedMap; } public void setCustomers(List<Entry> adaptedMap) { for(Entry entry : adaptedMap) { customers.put(entry.id, entry); } } public Customer getCustomer(long id) { return customers.get(id); } public long addCustomer(Customer customer) { customers.put(currentId, customer); return currentId++; } public void updateCustomer(Long id, Customer customer) { customers.put(id, customer); } public void deleteCustomer(long id) { customers.remove(id); } }
JavaBeansの作成と使用の詳細は、Beansを使用をご覧ください。