Question

Let's say I have a query getUser with two parameters - userName and password. I'd like to have a mapper method looking like this:

public UserBean getUser(String userName, String password);

Is there any way I can achieve something like that? Or maybe I should pass in my mapper method map of parameters (and some parameterMap in my xml mapper)?

public UserBean getUser(Map<String, Object> paramMap);

I'm looking forward for some hints and explanations.

Was it helpful?

Solution

Without any special configuration, you can refer to the first and second parameter as #{1} and #{2}, respectively.

If you would like to name the parameters, rather than referring to them numerically, do the following: In the XML mapping for your SELECT statement, set parameterType="map", and in the interface file, annotate the parameters with @Param. For example, public UserBean getUser( @Param( "user_name" String userName, @Param( "password" ) String password); would allow you to refer to the username and password, in the XML mapping, as #{user_name#} and #{password}, respectively.

OTHER TIPS

You shouldn't change the signature of your DAO method, the only issue to consider is how you build your mapping. iBatis support just one input parameter, and you must decide your class (attribute parameterType) to pack your two original parameters in one.

In this scenario you might (among other options) place the two parameters in a Map (HashMap, typically), or (if the parameters correspond to properties of the UserBean class) pass a dummy UserBean with those two properties set.

In both cases the packing (building the HashMap or the dummy UserBean which hold the two parameters) would be done inside your public UserBean getUser(String userName, String password) method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top