项目场景:
- MyBatis操作数据库
问题描述:
在使用MyBatis配置文件操作Mapper.xml文件的时候,有时候由于配置原因、或者方法名使用不当,不能找到对应的文件或者对应的方法,则会在控制台报错: Mapped Statements collection does not contain value
原因分析:
MyBatis使用配置文件的过程
- 首先使用
SqlSessionFactory
创建SqlSessionFactory
此时需要读取MyBatis配置文件
public class MyBatisUtils {
private static SqlSessionFactory sqlSessionFactory = null;
// 初始化SqlSessionFactory对象
static {
try {
// 使用MyBatis提供的Resources类加载MyBatis的配置文件
Reader reader =
Resources.getResourceAsReader("mybatis-config.xml");
// 构建SqlSessionFactory工厂
sqlSessionFactory =
new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
// 获取SqlSession对象的静态方法
public static SqlSession getSession() {
return sqlSessionFactory.openSession();
}
}
- 在
mybatis-config.xml
文件中的<mappers>
标签子元素<mapper>
中读取<mapper resource="*Mapper.xml"></mapper>
<mappers>
<mapper resource="com/hlq/ch9/dao/IdCardMapper.xml"></mapper>
<mapper resource="com/hlq/ch9/dao/IPersonMapper.xml"></mapper>
<mapper resource="com/hlq/ch9/dao/IUserMapper.xml"></mapper>
<mapper resource="com/hlq/ch9/dao/IOrdersMapper.xml"></mapper>
<mapper resource="com/hlq/ch9/dao/IProductMapper.xml"></mapper>
</mappers>
mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties" />
<settings>
<!-- 延迟加载的开关 -->
<setting name="lazyLoadingEnabled" value="false" />
<!-- 设置积极加载(true)或按需加载(false) -->
<setting name="aggressiveLazyLoading" value="true" />
<setting name="mapUnderscoreToCamelCase" value="true" />
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<typeAliases>
<package name="com.hlq.ch9.dao" />
</typeAliases>
<!--1.配置环境 ,默认的环境id为mysql -->
<environments default="mysql">
<!--1.2.配置id为mysql的数据库环境 -->
<environment id="mysql">
<!-- 使用JDBC的事务管理 -->
<transactionManager type="JDBC" />
<!--数据库连接池 -->
<dataSource type="POOLED">
<!-- 数据库驱动 -->
<property name="driver" value="${jdbc.driver}" />
<!-- 连接数据库的url -->
<property name="url" value="${jdbc.url}" />
<!-- 连接数据库的用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 连接数据库的密码 -->
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<!--2.配置Mapper的位置 -->
<mappers>
<mapper resource="com/hlq/ch9/dao/IdCardMapper.xml"></mapper>
<mapper resource="com/hlq/ch9/dao/IPersonMapper.xml"></mapper>
<mapper resource="com/hlq/ch9/dao/IUserMapper.xml"></mapper>
<mapper resource="com/hlq/ch9/dao/IOrdersMapper.xml"></mapper>
<mapper resource="com/hlq/ch9/dao/IProductMapper.xml"></mapper>
</mappers>
</configuration>
- 在
<mapper namespace="name"></mapper>
中使用命名空间中的名字操作该Mapper文件
<mapper namespace="IProductMapper">
.......
</mapper>
sqlSession.selectOne("IProductMapper.findProductByPrimaryKeyNestedResultOrders", 1);
- 使用
<mapper><select id="methodName"></select></mapper>
中的id="methodName"
方法进行数据库操作
<select id="findProductByPrimaryKey" parameterType="INTEGER" resultMap="BaseResultMap">
SELECT <include refid="BaseColumnList"></include>
FROM product
WHERE pid=#{pid, jdbcType=INTEGER}
</select>
问题出现:
- 使用JUnit进行测试:
@Test
public void findProductByPrimaryKeyNestedResultOrders() {
SqlSession sqlSession = MyBatisUtils.getSession();
try {
Product product = sqlSession.selectOne("IProductMapper.findProductByPrimaryKeyNestedResultOrders", 1);
System.out.println(product);
} finally {
sqlSession.close();
}
}
- 如果
配置
出错、命名空间
和方法名字
使用错误都会导致报错
解决方案:
- 在
mybatis-config.xml
配置文件中,正确配置文件路径<mapper resource="com/hlq/ch9/dao/IProductMapper.xml"></mapper>
- 正确使用命名空间和方法
sqlSession.selectOne("IProductMapper.findProductByPrimaryKeyNestedResultOrders", 1);