Mybatis遇到的一些坑与小技巧

目录

一. insert返回查询的ID值

二. Mybatis查询出的数据部分丢失没有转换

 三.  时间查询区间

3.1 入参为Date型

3.2 入参为String(时间格式的format)

四 Mybatis分页插件使用(Springboot)

4.1 Maven引入

4.2 文档参考: https://gitee.com/free/Mybatis_PageHelper 

五 Mybatis传入参数类型为Map

六  解决查询深分页  limit A,B A特别大查询慢


 

一. insert返回查询的ID值

Mybtatis查询有时候需要返回插入的主键值

1 首先在Mapper.xml加入

keyProperty = id

order="AFTER"  代表在SQL执行之后返回ID这个字段

<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">

        SELECT LAST_INSERT_ID()

    </selectKey>

完整的如下

 

<insert id="insert" parameterType="com.domain.StudentEntity">

    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">

        SELECT LAST_INSERT_ID()

    </selectKey>
        …………..
</insert>

注意点

Long result = servcie.insert(xx) 这样是接收不到返回的ID的,这个返回的只有0和1,1是成功,0是失败

所以正解是 会把插入的Id SET到入参的StudentEntity的Id字段获取的方法是 StudentEntity.getId()

 

二. Mybatis查询出的数据部分丢失没有转换

有时候我们查询出的数据 部分字段有值 部门字段没值 很奇怪,产生原因的解释如下

改成这样 resultMap="BaseResultMap" 可以解决

BaseResultMap为Mapper.xml 配置的result的Id值

例如

<resultMap id="BaseResultMap" type="com..domain.StudentEntity">
        <id column="id" jdbcType="BIGINT" property="id" />

        .....
</resultMap>

 三.  时间查询区间

3.1 入参为Date型

代码片段如下 直接替换相应的字段名即可

<if test="startTime!=null">
    <![CDATA[   and DATE_FORMAT(start_time, '%Y-%m-%d')>=  DATE_FORMAT(#{ruleStartTime}, '%Y-%m-%d')   ]]>
</if>
<if test="endTime!=null">
    <![CDATA[  and DATE_FORMAT(end_time, '%Y-%m-%d') <= DATE_FORMAT(#{ruleEndTime}, '%Y-%m-%d')    ]]>
</if>

3.2 入参为String(时间格式的format)

<if test="start != null ">
  and CREATION_TIME >= #{start}
</if>
<if test="end != null">
  and CREATION_TIME &lt;= #{end}
</if>
 

&lt; 为小于号  解决Mybatis的Tag name expected的报错问题,xml里面不允许<字符 会被识别成标签
 

四 Mybatis分页插件使用(Springboot)

4.1 Maven引入

  <!--MyBatis分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
        </dependency>

代码的调用查询类中 加入如下代码即可 startrow代码页数,pageSize代码每页的大小 数据从前端传过来

PageHelper.startPage(XXX.getStartRow(), XXX.getPageSize(), true);

好处是不需要分页查询 都要自己在Mapper拼接limit  A,B分页查询,只需要传入相应的参数即可,因为Mybatis是责任链的模式可扩展,加了PageHelper会在执行前自动拼接上查询的分页参数 挺好用的 建议使用

4.2 文档参考: https://gitee.com/free/Mybatis_PageHelper 

五 Mybatis传入参数类型为Map

 

<select id="selectSomething" parameterType="java.util.Map" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from xxxx
 
</select>

关键点 设置 parameterType="java.util.Map"

Mapper接口配置

   List<XXX> selectObject(Map<String, Object> param);

六  解决查询深分页  limit A,B A特别大查询慢

采用根据主键自增长ID偏移的方式

Mabtis xml文件如下:

<select id="selectOrderList" parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from order_info
        <where>
          id > #{id}
          <if test="start != null ">
            and create_time >= #{start}
          </if>
          <if test="end != null">
            and create_time &lt;= #{end}
          </if>
        </where>
        ORDER BY id
        LIMIT #{pageSize}
    </select>

Mapper interface如下:

List<OrderInfo> selectOrderList(Map<String, Object> param);

关键点   where id>#{id}  order by id limit #{pageSize}

查询比当前id大,pagesize个订单数据,查询结果根据id排序,

下一页传入的id 为上一个最后一条数据的id值。

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值