Java Spring + MyBatis 异常:映射语句集合不包含值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25358287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring + MyBatis exception: Mapped Statements collection does not contain value for
提问by user2327348
I got this exception when I executed this app:
执行此应用程序时出现此异常:
public class MainApp {
private static ApplicationContext context = null;
static SqlSession session = null;
public static void main(String[] args) throws IllegalArgumentException {
try {
context = new FileSystemXmlApplicationContext(
"src/main/webapp/WEB-INF/applicationContext.xml");
session = (SqlSession) context.getBean("sqlSession");
OrderMapper orderMapper = session.getMapper(OrderMapper.class);
int orderQuantity = orderMapper.getAllOrder().size();
System.out.println("Order quantity: " + orderQuantity);
session.commit();
session.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
Here is my OrderMapper interface:
这是我的 OrderMapper 界面:
public interface OrderMapper {
public List<Order> getAllOrder();
}
OrderMapper.xml
订单映射器.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.thonglm1.spring.mappers.OrderMapper">
<resultMap id="result" type="order">
<result property="orderId" column="cartid" />
<result property="type" column="type" />
<result property="saleId" column="saleId" />
<result property="status" column="status" />
<result property="info1" column="info1" />
<result property="info2" column="info2" />
<result property="info3" column="info3" />
</resultMap>
<select id="getAllOrder" resultMap="result">
select cartId, type,
info1
from TRAIN_ORDER;
</select>
</mapper>
applicationContext.xml
应用上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@172.21.8.62:1521:SHESVDEV" />
<property name="username" value="test" />
<property name="password" value="test" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="src/main/webapp/WEB-INF/mybatis-config.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.thonglm1.spring.mappers" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
mybatis-config.xml
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>
<typeAliases>
<typeAlias type="com.thonglm1.spring.domain.Order" alias="order" />
</typeAliases>
<mappers>
<package name="com.thonglm1.spring.mappers" />
</mappers>
</configuration>
And finally, the exception I got:
最后,我得到的例外:
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.thonglm1.spring.mappers.OrderMapper.getAllOrder at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:672) at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:507) at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:500) at org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:240) at org.apache.ibatis.binding.MapperMethod.(MapperMethod.java:71) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:39) at $Proxy6.getAllOrder(Unknown Source) at MainApp.main(MainApp.java:21)
java.lang.IllegalArgumentException:映射语句集合不包含 org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:672) 处 com.thonglm1.spring.mappers.OrderMapper.getAllOrder 的值.ibatis.session.Configuration.getMappedStatement(Configuration.java:507) at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:500) at org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java) :240) at org.apache.ibatis.binding.MapperMethod.(MapperMethod.java:71) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:39) at $Proxy6.getAllOrder(Unknown Source) at MainApp.main(MainApp.java:21)
And 1 more thing, since I quite new to this, is there any bad practices in my code ? Or anything I could improve ? Please feel free to comment, I'd really appreciate it.
还有一件事,因为我对此很陌生,我的代码中是否有任何不良做法?或者我可以改进什么?请随时发表评论,我真的很感激。
回答by jorrin
If the package cannot be read you can try adding the resource directly:
如果无法读取包,可以尝试直接添加资源:
?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>
<typeAliases>
<typeAlias type="com.thonglm1.spring.domain.Order" alias="order" />
</typeAliases>
<mappers>
<mapper resource="folderWhereIsXMLMapperIssaved/orderMapper.xml" />
</mappers>
</configuration>
回答by Karthik Prasad
Where have you specified the mapper XML location? You could add it in sqlSessionFactory's property mapperLocations
. Since you are using maven. I believe it is better to move the mapper XML file to a folder inside resources
directory. When You add more number of XML files it will be easier to maintain. I think the below folder structure would make sense as all the related stuff are grouped together with all your JDBC related stuff under one package with subpackages as domain, mappers, service interfaces and service interface implementation.
您在何处指定了映射器 XML 位置?您可以将其添加到 sqlSessionFactory 的 property 中mapperLocations
。由于您使用的是Maven。我相信将映射器 XML 文件移动到目录内的文件夹会更好resources
。当您添加更多的 XML 文件时,维护起来会更容易。我认为下面的文件夹结构是有意义的,因为所有相关的东西都与你所有的 JDBC 相关的东西组合在一个包下,子包作为域、映射器、服务接口和服务接口实现。
In this case since all the configurations go inside WEB-INF/classes directory during compile/packaging, which is also a classpath, this application configuration should hold good.
在这种情况下,由于在编译/打包期间所有配置都在 WEB-INF/classes 目录中,这也是一个类路径,因此该应用程序配置应该保持良好。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@172.21.8.62:1521:SHESVDEV" />
<property name="username" value="test" />
<property name="password" value="test" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:config/mybatis-config.xml" />
<property name="mapperLocations" value="classpath*:sqlmap/*.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.thonglm1.spring.mappers" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
And the mybatis-config.xml
, by default all the aliases in the domain package will be class name with the first letter being smaller.
而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>
<typeAliases>
<package name="com.thonglm1.spring.domain" />
</typeAliases>
</configuration>