mybatis update 返回值爲影響行數,仍是實際修改行數的問題

今天朋友問到一個問題, mybatis update 返回爲null的問題, 一直覺得返回的是影響的行數, 影響的行數就是實際修改的行數。今天發現這個是錯誤的, 影響的行數是這個update語句匹配到的行數,並非實際修改的行數。截圖:java

上面的截圖中, 第一條sql語句, update t_foo set password = '123456' where id = 1;mysql

匹配到1行數據, 修改了0條spring

第二條sql語句, update t_foo set password= '123456' where id = 2;sql

匹配到1行數據, 修改了1條 mybatis

下面經過mybatis update 修改數據, 看下mybatis返回值, 返回的是不是Rows matched記錄數app

搭建ssm框架, 忽略, 主要代碼截圖以下:框架

FooMapper.java測試

package com.mxsoft.ssmdemo.mapper;

import com.mxsoft.ssmdemo.model.Foo;

import java.util.List;

/**
 * @author zhangyingxuan
 */
public interface FooMapper {
    int insert(Foo foo);
    int update(Foo foo);
    int delete(Long id);

    List<Foo> findAll();

    Foo findById(Long id);
}

FooMaper.xmlspa

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mxsoft.ssmdemo.mapper.FooMapper">

    <!-- 通用查詢映射結果 -->
    <resultMap id="BaseResultMap" type="com.mxsoft.ssmdemo.model.Foo">
        <id column="id" property="id"/>
        <result column="version" property="version"/>
        <result column="create_date" property="createDate"/>
        <result column="create_by" property="createBy"/>
        <result column="update_date" property="updateDate"/>
        <result column="update_by" property="updateBy"/>
        <result column="is_delete" property="isDelete"/>
        <result column="memo" property="memo"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
    </resultMap>

    <!-- 通用查詢結果列 -->
    <sql id="Base_Column_List">
       id,version,create_date,create_by,username,password,update_date,update_by,is_delete,memo
    </sql>

    <select id="findById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM t_foo
        WHERE id = #{id, jdbcType=INTEGER}
    </select>

    <select id="findAll" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM t_foo
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.mxsoft.ssmdemo.model.Foo">
        INSERT INTO t_foo (create_date, create_by, username, password) values (now(), #{createBy}, #{username}, #{password});
    </insert>

    <update id="update" parameterType="com.mxsoft.ssmdemo.model.Foo">
        UPDATE t_foo set password = #{password} WHERE id = #{id, jdbcType=INTEGER}
    </update>

    <delete id="delete">
        DELETE FROM t_foo WHERE id = #{id, jdbcType=INTEGER}
    </delete>
</mapper>

測試用例FooMapperTestCase.javacode

package com.mxsoft.ssmdemo.mapper;

import com.mxsoft.ssmdemo.model.Foo;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath*:/spring/applicationContext.xml",
        "classpath*:/mybatis/mybatis-config.xml",})
public class FooMapperTestCase {
    @Autowired
    FooMapper fooMapper;

    @Test
    public void testUpdate() {
        Foo foo = new Foo();
        foo.setId(1l);
        foo.setPassword("123456");

        int update = fooMapper.update(foo);

        Assert.assertEquals(1, update);

    }

}

運行測試用例, 證實mapper返回的確實是匹配的行數。

看到網上, 有人說, 能夠返回實際修改的記錄數, 須要在jdbcUrl上加上

useAffectedRows=true

我是沒有作成功這個實驗, 多是mysql版本問題?下次進一步驗證吧

相關文章
相關標籤/搜索