Mybatis 碰到的一些问题
1. SQL语句参数无法获取:nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'order_ids' in 'class java.lang.String'
XML映射文件配置:
1 <select id="getTransferOrderListByOrderIds" parameterType="String" resultType="HashMap"> 2 select * 3 from wp_transfer_order 4 WHERE ORDER_SN IN (${order_ids}) 5 </select>
解决方法: 将parameterType="String"参数改为传一个自定义实体对象或者HashMap来封装这个id参数,就可以在自定义实体对象或者HashMap中找到这个id属性
参考地址:http://www.cnblogs.com/sishang/p/6555176.html
2. SQL语句参数错误:java.lang.RuntimeException: org.mybatis.spring.MyBatisSystemException:
nested exception is org.apache.ibatis.builder.IncompleteElementException:
not find parameter map com.paycloud.interfaces.dao.AdminUserDao.AdminUserResultMap
(1) XML配置部分:
1 <resultMap id="AdminUserResultMap" type="com.paycloud.interfaces.dto.AdminUserDto"> 2 <id column="MUID" jdbcType="INTEGER" property="muId" /> 3 <result column="ROLEID" jdbcType="INTEGER" property="roleId" /> 4 <result column="USERNAME" jdbcType="VARCHAR" property="userName" /> 5 <result column="PASSWORD" jdbcType="VARCHAR" property="passWord" /> 6 <result column="BINDIP" jdbcType="VARCHAR" property="bindIp" /> 7 <result column="LASTIP" jdbcType="VARCHAR" property="lastIp" /> 8 <result column="LASTLOGIN" jdbcType="TIMESTAMP" property="lastLogin" /> 9 <result column="SALTS" jdbcType="CHAR" property="salts" /> 10 <result column="STATUS" jdbcType="INTEGER" property="status" /> 11 <result column="GOOGLECODE" jdbcType="VARCHAR" property="googleCode" /> 12 </resultMap>
(2) SQL语句部分
错误案例:
1 <!-- 添加用户 --> 2 <insert id="addAdminUser" parameterMap="AdminUserResultMap" > 3 insert into sys_admin_user 4 (ROLEID,USERNAME,PASSWORD,SALTS,STATUS,LASTLOGIN, GOOGLECODE) 5 values 6 (#{roleId},#{userName},#{passWord},#{salts},#{status},#{lastLogin}, #{googleCode}) 7 </insert>
修改后:
1 <!-- 添加用户 --> 2 <insert id="addAdminUser" parameterType="com.paycloud.interfaces.dto.AdminUserDto" > 3 insert into sys_admin_user 4 (ROLEID,USERNAME,PASSWORD,SALTS,STATUS,LASTLOGIN, GOOGLECODE) 5 values 6 (#{roleId},#{userName},#{passWord},#{salts},#{status},#{lastLogin}, #{googleCode}) 7 </insert>
3. 注释搞的鬼:java.lang.RuntimeException: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException:
Could not set parameters for mapping: ParameterMapping{property='pageIndex', mode=IN, javaType=class java.lang.Integer, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}.
Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #4 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property.
Cause: java.sql.SQLException: Parameter index out of range (4 > number of parameters, which is 3).
1 <!-- 交易统计报表 --> 2 <select id="getTradeDayStatistics" resultType="Hashmap"> 3 SELECT SUM(t.ORDER_AMOUNT) as order_amount 4 FROM wp_trade_order t 5 WHERE t.ORDER_STATUS=1 6 <if test="tranType != null and tranType != ''"> and t.TRAN_TYPE = #{tranType} </if> 7 <if test="channelCode != null and channelCode != ''"> and t.CHANNEL_CODE = #{channelCode} </if> 8 <if test="startTime != null and startTime != ''"> and t.FINISH_TIME >= #{startTime} </if> 9 <if test="endTime != null and endTime != ''"> and t.FINISH_TIME <= #{endTime} </if> 10 <if test="minPayMoney != null and minPayMoney != ''"> and t.ORDER_AMOUNT >= #{minPayMoney} </if> 11 <if test="maxPayMoney != null and maxPayMoney != ''"> and t.ORDER_AMOUNT <= #{maxPayMoney} </if> 12 /*limit #{pageIndex}, #{pageSize}*/ 13 </select>
有点粗心,没太在意注释的方式,因为颜色肯定是变灰了,所以出现这个错误。之后把注释去掉就好了。
4.插入时间出现的问题 : Error querying database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
1 <!-- 生成财务流水 --> 2 <insert id="insertBillings" parameterType="com.paycloud.interfaces.dto.BillingsDto"> 3 INSERT INTO wp_billings 4 <trim prefix="(" suffix=")" prefixOverrides=","> 5 <if test="partnerId != null and partnerId != ''">, PARTNER_ID </if> 6 <if test="tradeTime != null and tradeTime != ''">, TRADE_TIME </if> 7 </trim> 8 <trim prefix="VALUES (" suffix=")" prefixOverrides=","> 9 <if test="partnerId != null and partnerId != ''">, #{partnerId} </if> 10 <if test="tradeTime != null and tradeTime != '' ">, #{tradeTime} </if> 11 </trim>
12 </insert>
这是mybatis 3.3.0中对于时间参数进行比较时的一个bug. 如果拿传入的时间类型参数与空字符串''进行对比判断则会引发异常.
所以 and tradeTime != ' ' 删除,只保留非空判断就正常了