I have query written in Oracle in MyBatis Mapper xml file like below:
<select id="getUserList" resultMap="userListResult" parameterType="org.user.UserNumber>
select * from user
WHERE (1=1)
<if test="num != null">AND rownum <= #{num}</if>
</select>
I need to make this Postgres Compliant. So, converted as below:
<select id="getUserList" resultMap="userListResult" parameterType="org.user.UserNumber>
select * from user
WHERE (1=1)
<if test="num != null">AND LIMIT #{num}</if>
</select>
But it is not working and throwing exception:
PSQLException: ERROR: syntax error at or near \"LIMIT\"\n
Can anyone help me please how to replace rownum here in Postgres?
Related
I am looking for a query where i can use either internal query returned value or direct SQL in when test= in Mybatis.
<foreach item="ID"
index="index"
collection="selectionIds"
separator=";">
UPDATE TABLE_1 SET
ACT_IND ='N', upd_by = 1234
WHERE SLCT_ID = #{ID}
AND rem= select REM from TABLE_1 where SLCT_ID=#{ID}
<choose>
<when test="rem == 3">
AND Bbsid=#{nsid}
</when>
<otherwise>
AND asid=#{asid}
</otherwise>
</choose>
</foreach>
From the above query, how can i get the rem value and use in when condition.
Thanks
I have a MyBatis query that looks like this:
<if test="userIdList != null and userIdList > 0">
AND galleries.id IN (
SELECT gallery_id
FROM gallery_users
WHERE gallery_id IN (
<foreach collection="userIdList" item="item" separator="," open="(" close=")">
#{item}
</foreach>
)
GROUP BY gallery_id HAVING COUNT(gallery_id) = ???
)
</if>
That part I'm stuck on is getting the collection size which will be dynamic. So how can I get the collection size so that I can properly fill in the '???' value?
You can invoke the Collection#size() method using OGNL expression. i.e.
GROUP BY gallery_id HAVING COUNT(gallery_id) = ${userIdList.size}
Note that #{userIdList.size} won't work here because the expression in #{} is parsed by MyBatis' internal expression parser and not by OGNL.
I am trying myBatis recently and I have been facing a problem when forming dynamic sqls.
When using myBatis to form dynamic sql like below, it works well with all the fields except when I send value in email field.
No data is fetched when emailId is passed. I have a doubt if the # symbol in emailId column is having a problem?
<select id = "getAllWithFilter" parameterType="java.util.Map" resultMap="result">
SELECT * FROM EMPLOYEE_LOOKUP
<where>
<if test = "firstName != null">
FIRST_NAME LIKE #{firstName}
</if>
<if test = "phoneNo != null">
AND PHONE LIKE #{phoneNo}
</if>
<if test = "emailId != null">
AND EMAIL LIKE #{emailId}
</if>
<if test = "analystGroup != null">
AND GROUP LIKE #{empGroup}
</if>
<choose>
<when test = "activeFlag != null">
AND EXPIRATION_DATE IS NULL
</when>
<when test = "inactiveFlag != null">
AND EXPIRATION_DATE IS NOT NULL
</when>
</choose>
</where>
</select>
This is the DEBUG Logs in myBatis - Log4J.
DEBUG [http-bio-9081-exec-1] - ==> Preparing: SELECT * FROM EMPLOYEE_LOOKUP WHERE EMAIL LIKE ? AND GROUP LIKE ? AND EXPIRATION_DATE IS NULL
DEBUG [http-bio-9081-exec-1] - ==> Parameters: abc(String), F(String)
Database, contains the following values
EMAIL | GROUP
----------------------
abc#gsf.com | F
You have simply forgotten the % surrounding the parameter value:
AND EMAIL LIKE '%' + #{emailId} + '%'
or
AND EMAIL LIKE '%' || #{emailId} || '%'
depending on DB vendor.
Alternatively, you can add the % in the param value after escaping those that might be in the param value.
otherwise it behaves as =
Following is my sql query that is used in mybatis mapper xml.
<select id="getData" fetchSize="30" resultType="java.util.HashMap" >
select * from table
where module='AB'
and rownum < 15
</select>
I am getting below exception while using rownum :
Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 130; columnNumber: 16; The content of elements must consist of well-formed character data or markup.
Below things I have tried:
ROWNUM<=15 AND <![CDATA[ ROWNUM <= 15 ]]>
But still it is not working.
Try this:
<select id="getData" fetchSize="30" resultType="java.util.HashMap" >
select * from table
where module='AB'
<![CDATA[ AND ROWNUM <= 15 ]]>
</select>
or ROWNUM <= 15 (with whitespaces after ROWNUM and before 15).
Are you sure you have tried the < or the <![CDATA[ ]]> on all the right places? (seems it's a large(r) file with perhaps multiple errors).
Since the code example you give is without the = and in the things you tried you add an =. And your error is on line 130 column 16 of your file, and we only see 5 lines and the < does not seem to be in column 16.
You could try to use: http://www.validome.org/xml/ and see if the entire configuration file is valid?
You can also read more about this on another question on Stack Overflow: https://stackoverflow.com/a/29136039/244748
I am using mybatis to retrieve data from DB, the data returned is containing duplicate entries.
Required result : Column Name , Value
Expected result is : column1 value A
But returned result is : COLUMN1 value A , column1 value A.
Hope able to clarify my doubt.
Can anybody tell me why it is happening?
<select id="getContentMap" resultType="map" parameterType="map">
select planId,location_qualifier from disclaimer_disclosure_content where
<choose>
<when test="plan_id != null">
plan_id = #{plan_id}
</when>
<when test="product_id != null">
product_id = #{product_id}
</when>
<otherwise>
issuer_id = #{issuer_id}
</otherwise>
</choose>
and effective_date >= #{effective_date}
and location_qualifier LIKE CONCAT('%' , #{location_qualifier} , '%')
</select>
The issue you are seeing is a bug in MyBatis 3 up until release 3.0.6: http://code.google.com/p/mybatis/issues/detail?id=303.
After that release you get the answer I outlined in my other answer (which was done with MyBatis 3.1.1).
You have four options:
Just ignore it and only grab the uppercase or lowercase entries
Upgrade to at least 3.0.6
Stop using map as resultType and move to a POJO domain object
Use the workaround below:
workaround for MyBatis < 3.0.6
Use full uppercase column aliases and they will only show up once (as uppercase) in your map:
<select id="getContentMap" resultType="map" parameterType="map">
select plan_id as PLAN_ID, location_qualifier as LOCATION_QUALIFIER from disclaimer_disclosure_content
where
<!-- SNIP: is the same as you had -->
</select>
This results in the output:
{PLAN_ID=2, LOCATION_QUALIFIER=Bar}
(or something similar depending on exactly how your select looks).
You will probably need to report more information, such as:
what database are you using?
what version of MyBatis 3 are you using (or are you still using iBATIS)?
what does your table structure look like?
In any case, I tried out a slightly simplified version of your query using MySQL 5.1 and MyBatis-3.1.1 and it worked fine - meaning that I only got back one entry of the column name in the result map. I provide my setup below so you can try to reproduce it or diagnose where your code may be wrong.
First, you have a error in your select statement. You have
SELECT planId
but then you have:
WHERE ... plan_id = #{plan_id}
so you probably meant SELECT plan_id in the SELECT clause.
Here is what worked for me.
My slightly simplified MyBatis select mapping is:
<select id="getContentMap" resultType="map" parameterType="map">
SELECT plan_id, location_qualifier FROM disclaimer_disclosure_content
WHERE
<choose>
<when test="plan_id != null">
plan_id = #{plan_id}
</when>
<otherwise>
product_id = #{product_id}
</otherwise>
</choose>
AND location_qualifier LIKE CONCAT('%' , #{location_qualifier} , '%')
</select>
Second, my MySQL table for this query:
mysql> select * from disclaimer_disclosure_content;
+---------+--------------------+------------+
| plan_id | location_qualifier | product_id |
+---------+--------------------+------------+
| 1 | Foo | 101 |
| 2 | Bar | 102 |
| 3 | Baz | 103 |
| 4 | Quux | 104 |
+---------+--------------------+------------+
4 rows in set (0.01 sec)
Third, my Java code to use the mapping:
#Test
public void testForSO() throws Exception {
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("plan_id", 2);
paramMap.put("location_qualifier", "Ba");
List<Map<String,Object>> lmap = session.selectList("getContentMap", paramMap);
assertNotNull(lmap);
Map<String,Object> m = lmap.get(0);
assertNotNull(m);
System.out.println(m.toString());
}
This passes and prints out:
{location_qualifier=Bar, plan_id=2}
I also tried it with
Map<String,Object> m = session.selectOne("getContentMap", paramMap);
and get the same expected result.