今天朋友问到一个问题, mybatis update 返回为null的问题, 一直以为返回的是影响的行数, 影响的行数就是实际修改的行数。今天发现这个是错误的, 影响的行数是这个update语句匹配到的行数,并不是实际修改的行数。截图:
上面的截图中, 第一条sql语句, update t_foo set password = '123456' where id = 1;
匹配到1行数据, 修改了0条
第二条sql语句, update t_foo set password= '123456' where id = 2;
匹配到1行数据, 修改了1条
下面通过mybatis update 修改数据, 看下mybatis返回值, 返回的是否是Rows matched记录数
搭建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.xml
<?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.java
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版本问题?下次进一步验证吧