【发布时间】:2014-10-11 01:41:12
【问题描述】:
我在执行这个应用程序时遇到了这个异常:
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();
}
}
}
这是我的 OrderMapper 界面:
public interface OrderMapper {
public List<Order> getAllOrder();
}
OrderMapper.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 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
?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>
最后,我得到的异常:
java.lang.IllegalArgumentException: Mapped Statements 集合确实 不包含价值 com.thonglm1.spring.mappers.OrderMapper.getAllOrder 在 org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:672) 在 org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:507) 在 org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:500) 在 org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:240) 在 org.apache.ibatis.binding.MapperMethod.(MapperMethod.java:71) 在 org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:39) 在 $Proxy6.getAllOrder(Unknown Source) 在 MainApp.main(MainApp.java:21)
还有一件事,因为我对此很陌生,我的代码中有什么不好的做法吗?或者有什么我可以改进的?请随时发表评论,我将不胜感激。
【问题讨论】:
-
三个名字必须匹配 - stackoverflow.com/a/60616498/715269
标签: java spring mybatis mapper