当我使用juit测试时,Spring Mybatis选择用户找到了这个。
UserMapper.xml命名空间是正确的
sqlSessionFactory是对的
但无法找到问题。
我的目录结构是:
import com.f.dao.UserMapper;
import com.f.dao.impl.UserDaoImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUserMapper {
@Test
public void addUser() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
UserMapper userMapper = (UserDaoImpl) context.getBean("userDaoImpl");
System.out.println(userMapper.getUserById(1));
}
}
我在sqlSession.selectOne("mapping/UserMapper.getUserById", id);
上使用调试程序跟踪
捕获错误:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mapping/UserMapper.getUserById
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mapping/UserMapper.getUserById
这是摘要配置文件
spring-context.xml
Datasource
配置似乎是对的
UserMapper.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.f.dao.UserMapper">
<insert id="insertUser" parameterType="User">
INSERT INTO users(user_name, passwd, email)
VALUES (#{userName}, #{password}, #{email})
</insert>
<resultMap id="getUserByIdUserResult" type="User">
<id property="id" column="id"/>
<result property="email" column="email"/>
<result property="userName" column="user_name"/>
<result property="password" column="passwd"/>
</resultMap>
<select id="getUserById" parameterType="int" resultMap="getUserByIdUserResult">
SELECT id, user_name, passwd, email FROM users
WHERE id = #{id}
</select>
</mapper>
UserMapper.java
public interface UserMapper {
public int insertUser(User user);
public User getUserById(@Param("id") int id);
}