mybatis use input param as argument in constructor - mybatis

In mybatis, can i use an input #Param object in the resultMap constructor?
int test(#Param("param1") someObj obj, #Param("str") String str);
<resultMap id="testResultMap" type="com.test.someOtherObject">
<constructor>
<idArg column="id" javaType="String"/>
<arg column="<use the input param obj of type someObj>" javaType="com.test.someObj"/>

No: Parameters and result maps are 2 distinct things. You cannot feed the result map directly from parameter.
If you want the input parameter to be in the result Map, then it must be in the result set, so you must either select the corresponding column if you filter on that column: SELECT col FROM t WHERE col = #{param}or add it as pseudo column: SELECT '${param}' AS "colName" FROM t and map it as usual with parameterized constructor if you like.
If the constructor argument is a complex type then use <arg resultMap="anotherResultMap" /> instead of <arg column="col" />.

Related

How to write dynamic attributes

I need to write a dynamic Attribute name instead of hardcode of Name attribute in dataweave 2.0 mulesoft 4 in anypointstudio
<?xml version="1.0" encoding="UTF-8"?>
<iGoApplicationData>
<UserData>
<Data Name="UpdateUserProfile">True</Data>
<Data Name="Action">??</Data>
</iGoApplicationData>
So in order to generate an XML like yours the DW structure will look like
{
iGoApplicationData: {
UserData: {
Data #(Name: payload.foo): "True",
Data #((var.attributeName): "Action"): "??"
}
}
}
So in this example I show how to specify a value in the attribute or a dynamic attribute name. For the dynamic attribute value just type the expression on the value side of the attribute (the part that goes after the :)
For dynamic attribute name you need to wrap the expression between parenthesis. When the name is wrapped between parenthesis it is considered dynamic. This applies to object keys and attributes names

SelectField renders unicode syntax

When dynamically creating choices for SelectField in WTForms, I get (u'Choice',) rendered in the dropdown list.
I suspect its something to do with unicode, but no idea how to get the correct string.
for example
form.group_id_name.choices = [(row, row) for row in db.session.query(entry.group_id_name).distinct()]
In my forms I have
group_id_name = SelectField('group_id_name')
I would like it to render
<select id="group_id_name" name="group_id_name"><option value="Choice1">Choice1</option><option value="Choice2">Choice2</option></select>
Instead I get
<select id="group_id_name" name="group_id_name"><option value="(u'Choice1',)">(u'Choice1',)</option><option value="(u'Choice2',)">(u'Choice2',)</option></select>
It's not anything to do with Unicode.
query() returns a sequence of column values for each row. For a query with only one column in it you get a length-1-tuple.
When you implicitly convert a tuple to a string as part of the template you get the Python code representation of the tuple, which looks like (somevalue,).
You want to include the string value of the column itself in the template, so you should access the first element of the sequence, eg:
form.group_id_name.choices = [(row[0], row[0]) for row in db.session.query(entry.group_id_name).distinct()]
or using unpacking assignment:
form.group_id_name.choices = [(name, name) for (name,) in db.session.query(entry.group_id_name).distinct()]

How to send list of string values as "LIST" object in Dozer (custom-converter-param)?

Currently the list is passed in the custom-converter-param as "aa,bb,cc,dd" etc., but the parameter is passed as string and we need to again split the string with comma and finally save it as a list for processing.
Is there is a way to pass the list of string object as "List" as a parameter?
Thanks,
Kathir
In every custom converter the value of the property custom-converter-param is passed to the field parameter of type String in org.dozer.DozerConverter<A, B> class, and its only getter method contract is like this:
public String getParameter()
So No, is not possible to retrieve the value as a List. The simplest/best thing to do here is to create a method getParameterAsList in your own converter to split the String value.

How to pass a byte[] parameter type in a sql query in MyBatis?

I need to match against an encrypted column in the DB. I need to pass the encrypted value for matching as a byte[]. The hashcode of the byte[] is passed instead of the actual value stored in the byte[]. Because the hash code is passed, it does not match the value correctly. Below is my query and the function call in the Mapper.java.
AccBalNotificationBean selectAccBalNotificationBean(#Param("acctIdByteArray") byte[] acctIdByteArray);
SELECT toa.accounts_id from tbl_transactions_other_accounts toa WHERE other_account_number = #{acctIdByteArray}
Thank you for your help.
I assume the datatype of your other_account_number column is of type string (char, varchar etc). Mybatis will use the StringDataTypeHandler by default and call the .toString() method of your byte array. Give MyBatis a hint that you want the content of your array to be used, by specifying the typeHandler.
.. WHERE other_account_number = #{acctIdByteArray, typeHandler=org.apache.ibatis.type.ByteArrayTypeHandler}

How can I pass 2 or more parameters to SqlSession insert or update method without creating a class?

I wonder is it possible to pass two or more parameters to mybatis SqlSession insert method without creating a new class just for that. I know that resultType="hashmap" could be used to return data from select, but how about passing data? Also just wonder why not to use a varargs for insert method for more than one parameter.
Thanks in advance
Remis B
There is a few different approaches.
Mapper
If your using a Mapper class, which I would recommend, you could do something like this.
interface Mapper
{
void insertSomeObject(#Param("a") Integer a, #Param("b") Integer b);
}
Then in your xml mapper you can use #{a} and #{b} to reference your parameters.
HashMap
You could wrap your parameters in a HashMap.
HashMap map = new HashMap();
map.put("a", 1);
map.put("b", 2);
Then pass the hash map to the SQL session insert, and use key value to reference parameters.
session.insert("myInsertStatment", map);
Any Collection
I'm not certain of your exact needs, but if you have an arbitrary amount of integers to pass to the insert statement, just pass a List, or any Collection.
interface Mapper
{
void insertSomeObject(#Param("integers") Collection<Integer> integers);
}
Then in your xml, you can use the for each to generate dynamic xml to fit your needs.
<foreach collection="integers" item="integer" open="(" close=")" separator=",">
#{integer}
</foreach>