setting field on returned object to a constant, an enum, or a decrypted value

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

setting field on returned object to a constant, an enum, or a decrypted value

BillR
I am porting Spring JDBC code to mybatis. I haven't spotted docs to
cover these mapper (?) issues. Taking them one by one:

public enum CurrencyType {
    UNKNOWN("UNKNOWN"),
    AED("AED"),
    ...
}

Interface:

    ExchangeRate getRates(@Param("numeratorCurrency") CurrencyType
numeratorCurrency);

1) Constant; jdbc code:

    exchangeRate.setDenominatorCurrency(CurrencyType.USD);

    - Is there any way to do this in mybatis? Or do I have to wrap the
dao in another class to do it?


2) Translated enum:

 
exchangeRate.setNumeratorCurrency(CurrencyType.valueOf(rs.getString("currency")));

    Will the mapping <result column="currency"
property="numeratorCurrency"/> cause the enum to be matched from the
db string?

    I'm hoping this will work for the query:

         WHERE currency = #{numeratorCurrency.name}

3) Decrypted value: in my jdbc class, I have Spring supplying

        private Encryption<String, String> encryption;

    And after the query,

 
achNoc.setAccountNumber(encryption.decodeStringified(achNoc.getAccountNumber()));

    Is there any way to implement this in mybatis, or do I have to
e.g. have a wrapper class for the dao to do this?

Thanks,
Bill
Reply | Threaded
Open this post in threaded view
|

Re: setting field on returned object to a constant, an enum, or a decrypted value

Ray Sprinkle


On Apr 2, 2:06 pm, BillR <[hidden email]> wrote:

> I am porting Spring JDBC code to mybatis. I haven't spotted docs to
> cover these mapper (?) issues. Taking them one by one:
>
> public enum CurrencyType {
>     UNKNOWN("UNKNOWN"),
>     AED("AED"),
>     ...
>
> }
>
> Interface:
>
>     ExchangeRate getRates(@Param("numeratorCurrency") CurrencyType
> numeratorCurrency);
>
> 1) Constant; jdbc code:
>
>     exchangeRate.setDenominatorCurrency(CurrencyType.USD);
>
>     - Is there any way to do this in mybatis? Or do I have to wrap the
> dao in another class to do it?
>
> 2) Translated enum:
>
> exchangeRate.setNumeratorCurrency(CurrencyType.valueOf(rs.getString("currency")));
>
>     Will the mapping <result column="currency"
> property="numeratorCurrency"/> cause the enum to be matched from the
> db string?
>
>     I'm hoping this will work for the query:
>
>          WHERE currency = #{numeratorCurrency.name}
>
> 3) Decrypted value: in my jdbc class, I have Spring supplying
>
>         private Encryption<String, String> encryption;
>
>     And after the query,
>
> achNoc.setAccountNumber(encryption.decodeStringified(achNoc.getAccountNumber()));
>
>     Is there any way to implement this in mybatis, or do I have to
> e.g. have a wrapper class for the dao to do this?
>
> Thanks,
> Bill

You need to use a typeHandler.  Its pretty easy to do.  In many cases
you can get away with just putting the handler in your configuration
file and MyBatis will handle the rest.  At worst you will need to
specify the Java and Sql types in the parameter or result map.   I do
this quite extensively in my project.
Reply | Threaded
Open this post in threaded view
|

Re: setting field on returned object to a constant, an enum, or a decrypted value

Eduardo Macarron
Hi Bill, you can go the enum option. MyBatis supports enums out-of-the-box.

El día 6 de abril de 2012 01:24, rds <[hidden email]> escribió:

>
>
> On Apr 2, 2:06 pm, BillR <[hidden email]> wrote:
>> I am porting Spring JDBC code to mybatis. I haven't spotted docs to
>> cover these mapper (?) issues. Taking them one by one:
>>
>> public enum CurrencyType {
>>     UNKNOWN("UNKNOWN"),
>>     AED("AED"),
>>     ...
>>
>> }
>>
>> Interface:
>>
>>     ExchangeRate getRates(@Param("numeratorCurrency") CurrencyType
>> numeratorCurrency);
>>
>> 1) Constant; jdbc code:
>>
>>     exchangeRate.setDenominatorCurrency(CurrencyType.USD);
>>
>>     - Is there any way to do this in mybatis? Or do I have to wrap the
>> dao in another class to do it?
>>
>> 2) Translated enum:
>>
>> exchangeRate.setNumeratorCurrency(CurrencyType.valueOf(rs.getString("currency")));
>>
>>     Will the mapping <result column="currency"
>> property="numeratorCurrency"/> cause the enum to be matched from the
>> db string?
>>
>>     I'm hoping this will work for the query:
>>
>>          WHERE currency = #{numeratorCurrency.name}
>>
>> 3) Decrypted value: in my jdbc class, I have Spring supplying
>>
>>         private Encryption<String, String> encryption;
>>
>>     And after the query,
>>
>> achNoc.setAccountNumber(encryption.decodeStringified(achNoc.getAccountNumber()));
>>
>>     Is there any way to implement this in mybatis, or do I have to
>> e.g. have a wrapper class for the dao to do this?
>>
>> Thanks,
>> Bill
>
> You need to use a typeHandler.  Its pretty easy to do.  In many cases
> you can get away with just putting the handler in your configuration
> file and MyBatis will handle the rest.  At worst you will need to
> specify the Java and Sql types in the parameter or result map.   I do
> this quite extensively in my project.