MyBatis: invalid comparison: java.util.Date and java.lang.String - mybatis

I have a issues as below while comparing date in MyBatis as below
Caused by: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
Both of java 'applyDate' and Postgres 'org_info.apply_date' type are Date
Here is my configuration:
<sql id="searchCriteriaSql">
<where>enter code here
<if test="applyDate != null && applyDate != '' ">
<![CDATA[org_info.apply_date <= #{applyDate}]]>
</if>
<if test="ogrNm != null && ogrNm != '' ">
<bind name="ogrNmKey"
value="'%' + ogrNm + '%'" />
AND ( upper(org_info.org_nm) LIKE upper(#{ogrNmKey}))
</if>
</where>
</sql>
The root cause comes from
<![CDATA[org_info.apply_date <= #{applyDate}]]>
Please help to give answer

Related

Parameter 'updateBy' not found. Available parameters are [0, createBy, param1, param2]

I'd like to execute a "SELECT" command with only one parameter: createBy (actually I don't know how does the dao.xml manage the first generics parameter, so in my opinion, there is only one parameter), why does Mybatis throw the exception: parameter 'updateBy' not found? with the if condition: <if test="updateBy != null and updateBy.id != null and updateBy.id != ''">
The dao.java is:
public List<T> findList(T entity, #Param("createBy") User createBy);
And the select sql in dao.xml is:
<select id="findList" resultType="ChenminIndicator">
SELECT
<include refid="chenminIndicatorColumns"/>
FROM chenmin_indicator a
<include refid="chenminIndicatorJoins"/>
<where>
a.del_flag = #{DEL_FLAG_NORMAL}
<if test="createBy != null and createBy.id != null and createBy.id != ''">
AND a.create_by = #{createBy.id}
</if>
<if test="updateBy != null and updateBy.id != null and updateBy.id != ''">
AND a.update_by = #{updateBy.id}
</if>
</where>
<choose>
<when test="page !=null and page.orderBy != null and page.orderBy != ''">
ORDER BY ${page.orderBy}
</when>
<otherwise>
ORDER BY a.update_date DESC
</otherwise>
</choose>
</select>

why #{params.property} cannot get value in mybatis?

My daoService is
int insertSelective(#Param("series") Series series, #Param("table") Map<String, String> table);
My mapper.xml is
<insert id="insertSelective" statementType="STATEMENT">
insert into ${table.seriesTable}
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="series.seriesName != null" >
series_name,
</if>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="series.seriesName != null" >
'${series.seriesName}',
</if>
</trim>
When I user '${series.seriesName}', I can get the value,
but #{series.seriesName} cannot. Why?

mybatis builder exception The expression 'room' evaluated to a null value

gomap_controller.java
List<String> room = new ArrayList<String>();
Map searchConditions = new HashMap<>();
searchConditions.put("room", room);
List<Map> rList = rd.getRoomList(searchConditions);
room-mapper.xml
<select id="getRoomList" resultType="java.util.HashMap"
parameterType="java.util.HashMap">
<choose>
<when
test="underground != '' or low_floor != '' or mid_floor != '' or high_floor != ''">
B_FLOOR IN
<foreach collection="room" item="item" index="index"
separator="," open="(" close=")AND">
#{item}
</foreach>
</when>
</choose>
and now I getting error as ### Error querying database. Cause: org.apache.ibatis.builder.BuilderException: The expression 'room' evaluated to a null value.
Nowhere in your test are you evaluating for a null collection. It looks like you're executing validations on keys in the map? Based on the small snippet of code you have there I would expect to see this evaluation instead of what you have...
SELECT ...
FROM ...
<choose>
<when test="room" != 'null'>
WHERE B_FLOOR IN
<foreach collection="room" item="item" index="index" separator="," open="(" close=")AND">
#{item}
</foreach>
</when>
</choose>

MyBatis if clause

I tried following if clause in MyBatis and got following exception please help me to identify the issue here..
public class Student{
private Integer studId;
private String name;
private String email;
private Date dob;
}
Mapping
<select id="searchStudent" parameterType="hashmap" resultMap="StudentResult">
<![CDATA[
SELECT * FROM STUDENTS
WHERE 1 = 1
<if test="studId != null">
AND STUD_ID= #{studId}
</if>
<if test="name != null">
AND NAME like #{name}
</if>
]]>
</select>
Exception I get:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test="studId != null">
AND STUD_ID= 1
</if>
<if test="name != null">
' at line 4
### The error may exist in StudentMapper.xml
### The error may involve com.maventest.mytest.StudentMapper.searchStudent-Inline
### The error occurred while setting parameters
### SQL: SELECT * FROM STUDENTS WHERE 1 = 1 <if test="studId != null"> AND STUD_ID= ? </if> <if test="name != null"> AND NAME like ? </if>
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test="studId != null">
AND STUD_ID= 1
</if>
<if test="name != null">
' at line 4
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
Remove <![CDATA[ and ]]> as this escapes all the query and mybatis doesn't process it at all so if is passed verbatim as part of the query to database.
<select id="searchStudent" parameterType="hashmap" resultMap="StudentResult">
SELECT * FROM STUDENTS
WHERE 1 = 1
<if test="studId != null">
AND STUD_ID= #{studId}
</if>
<if test="name != null">
AND NAME like #{name}
</if>
</select>
CDATA sections are used to escape blocks of text containing characters that would otherwise be regarded as markup [ORACLE definition].
Sometimes, we need it, specially when we have a where condition with such markups : <, > , <> , etc.
<![CDATA[ SELECT * FROM STUDENTS WHERE 1 = 1 ]]>
<if test="studId != null">
<![CDATA[ AND STUD_ID= #{studId} ]]>
</if>
<if test="name != null">
**<![CDATA[ AND COD_PER <> 'ASSU' ]]>**
</if>
Add it just on the line with the markup problem also works.

Check value in MyBatis mapper

I have a strange error in a MyBatis mapper where I can check if a parameter is null but I can't check the value of the same parameter;
Here is an excerpt from my mapper:
<parameterMap id="OUTOCriteria" type="Map">
<parameter property="name" javaType="String" />
<parameter property="parentOU" javaType="Long" />
<parameter property="type" javaType="Integer" />
<parameter property="statusTypeCurrent" javaType="Integer" />
</parameterMap>
Then later I do this check:
<if test="parentOU != null">
<if test="parentOU.compareTo(new Long(1))">
AND OU_STRC_ID ${parentOU.operator.sql} #{parentOU.value}
</if>
</if>
The first check doesn't throw any error, but the nested check gives me this error:
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression '!parentOU.compareTo(new Long(1))'. Cause: org.apache.ibatis.ognl.MethodFailedException: Method "compareTo" failed for object parentOUEQUAL1 [java.lang.NoSuchMethodException: compareTo(java.lang.Long)] ### The error may exist in com/clearstream/iam/query/sqlmaps/OU.xml ### The error may involve ou.iamouDefaultQuery ### The error occurred while executing a query ### Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression '!parentOU.compareTo(new Long(1))'. Cause: org.apache.ibatis.ognl.MethodFailedException: Method "compareTo" failed for object parentOUEQUAL1 [java.lang.NoSuchMethodException: compareTo(java.lang.Long)]
What would be the correct syntax to test the value of my Long parameter?
Try something like this:
<if test="parentOU != null and parentOU.longValue() == 1">
AND OU_STRC_ID ${parentOU.operator.sql} #{parentOU.value}
</if>