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>
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
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?
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>
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.