This commit is contained in:
Keith Donald 2009-10-17 05:49:28 +00:00
parent 9584a81687
commit bd0356a712
1 changed files with 65 additions and 65 deletions

View File

@ -1510,11 +1510,11 @@ public class PersonDtoPersonMapper implements Mapper<PersonDto, Person> {
</para> </para>
</section> </section>
<section id="mapping.SpelMapper"> <section id="mapping.SpelMapper">
<title>General-purpose SpelMapper Implementation</title> <title>General-purpose Object Mapper Implementation</title>
<para> <para>
A general purpose object-to-object mapping system exists in the <classname>org.springframework.mapping.support</classname> package. A general purpose object-to-object mapping system exists in the <classname>org.springframework.mapping.support</classname> package.
Built on the Spring Expression Language (SpEL), this system is capable of mapping between a variety of object types, including JavaBeans, Arrays, Collections, and Maps. Built on the Spring Expression Language (SpEL), this system is capable of mapping between a variety of object types, including JavaBeans, Arrays, Collections, and Maps.
It can perform field-to-field, field-to-multi-field, multi-field to field, and conditional mappings. It can perform field-to-field, field-to-multi-field, multi-field-to-field, and conditional mappings.
It also can carry out type conversion and recursive mapping, which are often required with rich object models. It also can carry out type conversion and recursive mapping, which are often required with rich object models.
</para> </para>
<section id="mapping.SpelMapper-usage"> <section id="mapping.SpelMapper-usage">
@ -1610,28 +1610,29 @@ Mapper<PersonDto, Person> mapper =
.getMapper(); .getMapper();
]]> ]]>
</programlisting> </programlisting>
<section id="mapping.SpelMapper-Explicit-differentFieldNames"> </section>
<title>Mapping between two fields with different names</title> <section id="mapping.SpelMapper-Explicit-differentFieldNames">
<para> <title>Mapping between two fields with different names</title>
Suppose you need to map <literal>AccountDto.name</literal> to <literal>Account.fullName</literal>. <para>
Since these two field names are not the same, the default auto-mapping rule would not apply. Suppose you need to map <literal>AccountDto.name</literal> to <literal>Account.fullName</literal>.
Handle a requirement like this by explicitly registering a mapping rule: Since these two field names are not the same, the default auto-mapping rule would not apply.
</para> Handle a requirement like this by explicitly registering a mapping rule:
<programlisting language="java"><![CDATA[ </para>
<programlisting language="java"><![CDATA[
builder.addMapping("name", "fullName")]]> builder.addMapping("name", "fullName")]]>
</programlisting> </programlisting>
<para> <para>
In the example above, the <literal>name</literal> field will be mapped to the <literal>fullName</literal> field when the mapper is executed. In the example above, the <literal>name</literal> field will be mapped to the <literal>fullName</literal> field when the mapper is executed.
No default mapping will be performed for <literal>name</literal> since an explicit mapping rule has been configured for this field. No default mapping will be performed for <literal>name</literal> since an explicit mapping rule has been configured for this field.
</para> </para>
</section> </section>
<section id="mapping.SpelMapper-Explicit-singleFieldToMultipleField"> <section id="mapping.SpelMapper-Explicit-singleFieldToMultipleField">
<title>Mapping a single field to multiple fields</title> <title>Mapping a single field to multiple fields</title>
<para> <para>
Suppose you need to map <literal>PersonDto.name</literal> to <literal>Person.firstName</literal> and <literal>Person.lastName</literal>. Suppose you need to map <literal>PersonDto.name</literal> to <literal>Person.firstName</literal> and <literal>Person.lastName</literal>.
Handle a field-to-multi-field requirement like this by explicitly registering a mapping rule: Handle a field-to-multi-field requirement like this by explicitly registering a mapping rule:
</para> </para>
<programlisting language="java"><![CDATA[ <programlisting language="java"><![CDATA[
builder.addMapping("name", new Mapper<String, Person>() { builder.addMapping("name", new Mapper<String, Person>() {
public Person map(String name, Person person) { public Person map(String name, Person person) {
String[] names = name.split(" "); String[] names = name.split(" ");
@ -1640,19 +1641,19 @@ builder.addMapping("name", new Mapper<String, Person>() {
return person; return person;
} }
});]]> });]]>
</programlisting> </programlisting>
<para> <para>
In the example above, the first part of the <literal>name</literal> field will be mapped to the <literal>firstName</literal> field and the second part will be mapped to the <literal>lastName</literal> field. In the example above, the first part of the <literal>name</literal> field will be mapped to the <literal>firstName</literal> field and the second part will be mapped to the <literal>lastName</literal> field.
No default mapping will be performed for <literal>name</literal> since an explicit mapping rule has been configured for this field. No default mapping will be performed for <literal>name</literal> since an explicit mapping rule has been configured for this field.
</para> </para>
</section> </section>
<section id="mapping.SpelMapper-Explicit-multipleFieldsToField"> <section id="mapping.SpelMapper-Explicit-multipleFieldsToField">
<title>Mapping multiple fields to a single field</title> <title>Mapping multiple fields to a single field</title>
<para> <para>
Suppose you need to map <literal>CreateAccountDto.activationDay</literal> and <literal>CreateAccountDto.activationTime</literal> to <literal>Account.activationDateTime</literal>. Suppose you need to map <literal>CreateAccountDto.activationDay</literal> and <literal>CreateAccountDto.activationTime</literal> to <literal>Account.activationDateTime</literal>.
Handle a multi-field-to-field requirement like this by explicitly registering a mapping rule: Handle a multi-field-to-field requirement like this by explicitly registering a mapping rule:
</para> </para>
<programlisting language="java"><![CDATA[ <programlisting language="java"><![CDATA[
builder.addMapping(new String[] { "activationDay", "activationTime" }, new Mapper<CreateAccountDto, AccountDto>() { builder.addMapping(new String[] { "activationDay", "activationTime" }, new Mapper<CreateAccountDto, AccountDto>() {
public Account map(CreateAccountDto dto, Account account) { public Account map(CreateAccountDto dto, Account account) {
DateTime dateTime = ISODateTimeFormat.dateTime().parseDateTime( DateTime dateTime = ISODateTimeFormat.dateTime().parseDateTime(
@ -1661,36 +1662,35 @@ builder.addMapping(new String[] { "activationDay", "activationTime" }, new Mappe
return account; return account;
} }
});]]> });]]>
</programlisting> </programlisting>
<para> <para>
In the example above, the <literal>activationDay</literal> and <literal>activationTime</literal> fields are mapped to the single <literal>activationDateTime</literal> field. In the example above, the <literal>activationDay</literal> and <literal>activationTime</literal> fields are mapped to the single <literal>activationDateTime</literal> field.
No default mapping is performed for <literal>activationDay</literal> or <literal>activationTime</literal> since an explicit mapping rule has been configured for these fields. No default mapping is performed for <literal>activationDay</literal> or <literal>activationTime</literal> since an explicit mapping rule has been configured for these fields.
</para> </para>
</section> </section>
<section id="mapping.SpelMapper-Explicit-conditionalMappings"> <section id="mapping.SpelMapper-Explicit-conditionalMappings">
<title>Mapping conditionally</title> <title>Mapping conditionally</title>
<para> <para>
Suppose you need to map <literal>Map.countryCode</literal> to <literal>PhoneNumber.countryCode</literal> only if the source Map contains a international phone number. Suppose you need to map <literal>Map.countryCode</literal> to <literal>PhoneNumber.countryCode</literal> only if the source Map contains a international phone number.
Handle conditional mapping requirements like this by explicitly registering a mapping rule: Handle conditional mapping requirements like this by explicitly registering a mapping rule:
</para> </para>
<programlisting language="java"><![CDATA[ <programlisting language="java"><![CDATA[
builder.addConditionalMapping("countryCode", "international == 'true'");]]> builder.addConditionalMapping("countryCode", "international == 'true'");]]>
</programlisting> </programlisting>
<para> <para>
In the example above, the <literal>countryCode</literal> field will only be mapped if the international field is 'true'. In the example above, the <literal>countryCode</literal> field will only be mapped if the international field is 'true'.
<literal>international == 'true'</literal> is a boolean expression that must evaluate to true for the mapping to be executed. <literal>international == 'true'</literal> is a boolean expression that must evaluate to true for the mapping to be executed.
No default mapping is performed for <literal>countryCode</literal> since an explicit mapping rule has been configured for this field. No default mapping is performed for <literal>countryCode</literal> since an explicit mapping rule has been configured for this field.
</para> </para>
</section> </section>
<section id="mapping.SpelMapper-Explicit-forcing"> <section id="mapping.SpelMapper-Explicit-forcing">
<title>Forcing Explicit Mappings</title> <title>Forcing Explicit Mappings</title>
<para> <para>
You can require that all mapping rules be defined explicitly by disabling the "auto mapping" feature: You can require that all mapping rules be defined explicitly by disabling the "auto mapping" feature:
</para> </para>
<programlisting language="java"><![CDATA[ <programlisting language="java"><![CDATA[
builder.setAutoMappingEnabled(false);]]> builder.setAutoMappingEnabled(false);]]>
</programlisting> </programlisting>
</section>
</section> </section>
<section id="mapping.SpelMapper-CustomConverter"> <section id="mapping.SpelMapper-CustomConverter">
<title>Registering Custom Mapping Converters</title> <title>Registering Custom Mapping Converters</title>