异常捕获:
mybatis不鼓励捕捉异常,但在某些特定场景下需要捕捉,这个时候通过try catch是捕捉不到的,mybatis有自己的处理方式,它把异常映射成了DataAccessException,那么我们需要抛出异常并捕捉。
***Mapper可以throws DataAccessException;
sqlmap中大于、等于、小于符号的处理:
方法一:
方法二:用转移字符替换
传入混合参数:
方法一:
Public User selectUser(String name,String area);
<select id="selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{0} and user_area= #{1}
</select>
方法三:
mybatis不鼓励捕捉异常,但在某些特定场景下需要捕捉,这个时候通过try catch是捕捉不到的,mybatis有自己的处理方式,它把异常映射成了DataAccessException,那么我们需要抛出异常并捕捉。
***Mapper可以throws DataAccessException;
sqlmap中大于、等于、小于符号的处理:
方法一:
<if test="startDate != null and startDate.trim() != ''"> <![CDATA[ and leave_date >= #{startDate,jdbcType=VARCHAR}]]> </if>
方法二:用转移字符替换
Caused by: java.lang.NumberFormatException: For input string
原因:mybatis对<if test="index == 'A'">识别错误
修改方法:test="param eq 'A'.toString()"
或者 <if test="name == "A"">
或者 <if test='name == "A" '>
传入混合参数:
方法一:
Public User selectUser(String name,String area);
<select id="selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{0} and user_area= #{1}
</select>
方法二:
Public User selectUser(Map paramMap);
<select id=" selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>
方法三:
List<Leave> selectByAccounts(@Param("accounts") List<String> accounts, @Param("startDate") String startDate, @Param("endDate") String endDate);
where leave_date between #{startDate,jdbcType=VARCHAR} and #{endDate,jdbcType=VARCHAR} and user_account in <foreach collection="accounts" item="item" open="(" close=")" separator=","> #{item,jdbcType=VARCHAR} </foreach>
mybaits中使用sum做统计时,如果统计值为0,返回的值不是0而为null,定义mapper时用integer就会报错,此时可以使用函数 COALESCE(number1,number2) 来避免该问题。
例如:COALESCE(sum(amount), 0) ,当sum的值为null的时候返回0