网络

教育改变生活

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 104|回复: 0
打印 上一主题 下一主题

MyBatis常见问题合集

[复制链接]

397

主题

398

帖子

1625

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1625
跳转到指定楼层
楼主
发表于 2024-10-11 09:52:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 却尘 于 2024-10-11 09:53 编辑

MyBatis常见问题合集
总结一些基础的常见的Mybatis问题,方便自己,方便读者学习,内容不多
1、大于号、小于号在 sql 语句中的转换  
使用mybatis 时 sql 语句是写在 xml 文件中,如果 sql 中有一些特殊的字符的话,比如< ,<=,>,>=等符号,会引起 xml 格式的错误,需要替换掉,或者不被转义。 有两种方法可以解决:转义字符和标记 CDATA 块。
方式1、转义字符
1 <select id="searchByPrice" parameterType="Map" resultType="Product">
2     <!--
方式1、转义字符 -->
3     select * from Product where price &gt;= #{minPrice} and price &lt;= #{maxPrice}
4 </select>

方式2、标记CDATA
1 <select id="searchByPrice" parameterType="Map" resultType="Product">
2   <!--
方式2CDATA -->
3   <![CDATA[select * from Product where price >= #{minPrice} and price <= #{maxPrice} ]]>
4 </select>

转义字符表
        
转义
      
符号
  
      
&lt;
   
<

     
&gt;
   
>

     
&amp;
   
&

     
&apos;
   

     
&quot;
   
2、MyBatis 中的 resultType 和 resultMap
  网上的总结很多,简单而言,resultType 用于返回值只有一个字段的类型,resultMap 用于返回值有多个字段的类型。至于结果是 List 还是一个,则在 Mapper 中定义返回值是List还是单个。
使用 resultType:
1 <select id="count" resultType="java.lang.Integer">  
2         SELECT count(*) FROM USER  
3 </select>  

使用 resultMap:
1 <resultMap type="com.liulanghan.Blog" id="BlogResult">   
2     <id column="id" property="id"/>   
3     <result column="title" property="title"/>   
4     <result column="content" property="content"/>   
5     <result column="owner" property="owner"/>   
6 </resultMap>   
7   
8 <select id="selectBlog" parameterType="int" resultMap="BlogResult">   
9       select * from t_blog where id = #{id}   
10 </select>  

3、参数
Mapper 中需要用@Param("queryDate")定义参数名称,sql 中用#{queryDate}使用参数,字符串也不需要引号。
参数判断和if的用法:
1 <if test="queryDate != null">
2     and queryDate >= #{queryDate}
3 </if>

when otherwise 就是 ifelse
1 <choose>
2      <when test="isDelete != null and isDelete == 0">
3           isDelete=0
4       </when>
5       <otherwise>
6           isDelete=1
7        </otherwise>
8 </choose>

如果要判断的字符串,则需要加引号
1 <when test="gender != null and gender == 'MALE'">
2     gender='MALE'
3 </when>

4.传入参数参数为0查询条件失效4.1场景再现
场景是这样的,需要做一个对账单查询,可以按金额范围进行查询,页面参数写完之后进行条件,输入0测试了无数次均失效。
4.2原因解析
当页面参数为 0,传入到 mybatis 的 xml 中后,如果不是字符串,需指定数据类型,否则会被误认为 null
<if test="data.tatalAmount != null and data.totalAmount !='' ">and total_Amount=#{data.totalAmount}</if>
·        1·        2·        3
这种情况如果 totalAmount 为 0 时将被误认为是 null,里面的条件不会执行。正确的姿势如下
1.添加 0 判断
<if test="data.tatalAmount != null and data.totalAmount !='' or tatalAmount==0 ">and total_Amount=#{data.totalAmount}</if>
·        1·        2·        3
2.规定传入参数的类型
<if test="data.tatalAmount != null and data.totalAmount !='' ">and total_Amount=#{data.totalAmount,jdbc.Type=DECIMAL}</if>

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

WEB前端

QQ|手机版|小黑屋|金桨网|助学堂  咨询请联系站长。

GMT+8, 2024-10-22 21:22 , Processed in 0.033445 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表