why #{params.property} cannot get value in mybatis? - 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?

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>

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

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

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>

Migrate <procedure> from ibatis to mybatis

When i use ibatis2mybatis to migrate this sqlMap
<procedure id="correctPcsInvoiceInCn"
parameterClass="java.util.Map" >
exec correctPcsInvoice
<isNotNull property="insertingPersonId" >#insertingPersonId# </isNotNull> <isNull property="insertingPersonId" > null </isNull>
<isNotNull prepend="," property="contractServicePerfID" >#contractServicePerfID# </isNotNull> <isNull prepend="," property="contractServicePerfID" > null </isNull>
<isNotNull prepend="," property="chemotherapyInvoiceID" >#chemotherapyInvoiceID# </isNotNull> <isNull prepend="," property="chemotherapyInvoiceID" > null </isNull>
<isNotNull prepend="," property="tariff" >#tariff# </isNotNull> <isNull prepend="," property="tariff" > null </isNull>
<isNotNull prepend="," property="medicalProductDetailsID" >#medicalProductDetailsID# </isNotNull> <isNull prepend="," property="medicalProductDetailsID" > null </isNull>
<isNotNull prepend="," property="drugPackageUnitsCount" >#drugPackageUnitsCount# </isNotNull> <isNull prepend="," property="drugPackageUnitsCount" > null </isNull>
<isNotNull prepend="," property="drugPackagePrice" >#drugPackagePrice# </isNotNull> <isNull prepend="," property="drugPackagePrice" > null </isNull>
<isNotNull prepend="," property="invoiceNumber" >#invoiceNumber# </isNotNull> <isNull prepend="," property="invoiceNumber" > null </isNull>
<isNotNull prepend="," property="positionInInvoice" >#positionInInvoice# </isNotNull> <isNull prepend="," property="positionInInvoice" > null </isNull>
<isNotNull prepend="," property="taxPayerID" >#taxPayerID# </isNotNull> <isNull prepend="," property="taxPayerID" > null </isNull>
</procedure>
i get
<update id="correctPcsInvoiceInCn" parameterType="java.util.Map" statementType="CALLABLE">
exec correctPcsInvoice
<if test="insertingPersonId != null">#{insertingPersonId} </if> <if test="insertingPersonId == null"> null </if>
<if test="contractServicePerfID != null">,#{contractServicePerfID} </if> <if test="contractServicePerfID == null">, null </if>
<if test="chemotherapyInvoiceID != null">,#{chemotherapyInvoiceID} </if> <if test="chemotherapyInvoiceID == null">, null </if>
<if test="tariff != null">,#{tariff} </if> <if test="tariff == null">, null </if>
<if test="medicalProductDetailsID != null">,#{medicalProductDetailsID} </if> <if test="medicalProductDetailsID == null">, null </if>
<if test="drugPackageUnitsCount != null">,#{drugPackageUnitsCount} </if> <if test="drugPackageUnitsCount == null">, null </if>
<if test="drugPackagePrice != null">,#{drugPackagePrice} </if> <if test="drugPackagePrice == null">, null </if>
<if test="invoiceNumber != null">,#{invoiceNumber} </if> <if test="invoiceNumber == null">, null </if>
<if test="positionInInvoice != null">,#{positionInInvoice} </if> <if test="positionInInvoice == null">, null </if>
<if test="taxPayerID != null">,#{taxPayerID} </if> <if test="taxPayerID == null">, null </if>
</update>
ibatis2mybatis generate this but i don't know is it good or not? Can any1 validate this part of code? And if possible change to good one.
It's better to use bean instead of map in myBatis
public InvoiceBean {
private Integer insertingPersonId;
...
//getters, setters
}
and use it like this:
<update id="correctPcsInvoiceInCn" parameterType="your.packadge.InvoiceBean" statementType="CALLABLE">
exec correctPcsInvoice #{insertingPersonId, jdbcType=NUMERIC}, ...other parameters
</update>
adding jdbcType to parameter allows myBatis to set correct null value for it.

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.