gpt4 book ai didi

java - Spring + MyBatis 异常: Mapped Statements collection does not contain value for

转载 作者:行者123 更新时间:2023-11-30 09:04:07 25 4
gpt4 key购买 nike

我在执行这个应用程序时遇到了这个异常:

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 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)

还有一件事,因为我对此很陌生,我的代码中是否有任何不良做法?或者有什么我可以改进的吗?请随时发表评论,我将不胜感激。

最佳答案

您在哪里指定映射器 XML 位置?您可以将其添加到 sqlSessionFactory 的属性 mapperLocations 中。由于您使用的是maven。我认为最好将映射器 XML 文件移动到 resources 目录内的文件夹中。当您添加更多数量的 XML 文件时,它将更容易维护。我认为下面的文件夹结构是有意义的,因为所有相关的东西都与所有 JDBC 相关的东西组合在一个包下,子包作为域、映射器、服务接口(interface)和服务接口(interface)实现。

enter image description here

在这种情况下,由于在编译/打包期间所有配置都在 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>

mybatis-config.xml,domain包中的别名默认都是首字母小的类名。

?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>

关于java - Spring + MyBatis 异常: Mapped Statements collection does not contain value for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25358287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com