UserMapper.xml内容为
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace: 当前Mapper动态sql映射文件的唯一标识名称: -->
<mapper namespace="mappers.UserMapper">
<select id="getUserById" resultType="com.hfxt.pojo.User" >
select
uid, username, user_pwd as userPwd
from sys_user_tb
where uid = 2;
</select>
<!-- 扩展: 查询所有用户. -->
<select id="getUserAll" resultType="com.hfxt.pojo.User">
select uid, username, user_pwd as userPwd
from sys_user_tb ;
</select>
<insert id="addUser">
insert into sys_user_tb (username,user_pwd)
values ('HelloKitty','111111');
</insert>
<update id="updateUserById">
update sys_user_tb set user_pwd = '888888'
where uid = 21 ;
</update>
<delete id="deleteUserById">
delete from sys_user_tb where uid = 22;
</delete>
</mapper>
数据库内容为
执行语句为
@Test
public void test2(){
String resource = "SqlMapConfig.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
int addUser = sqlSession.insert("mappers.UserMapper.addUser");
int updateUserById = sqlSession.update("mappers.UserMapper.updateUserById");
int deleteUserById = sqlSession.delete("mappers.UserMapper.deleteUserById");
System.out.println(addUser+"-"+updateUserById+"-"+deleteUserById);
sqlSession.commit();
sqlSession.close();
}
执行结果为
由数据库变化可知数据删除成功,但delete返回值为0
解决方案
勾选属性中的跳过测试即可
原因是Maven项目测试类的@Test注解会执行两次,所以delete的值总为零,在文件->项目结构->Maven->Runner中勾选skip test就可以解决