【问题标题】:Spring + MyBatis exception: Mapped Statements collection does not contain value forSpring + MyBatis 异常:映射语句集合不包含值
【发布时间】: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)

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

【问题讨论】:

标签: java spring mybatis mapper


【解决方案1】:

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

在这种情况下,由于在编译/打包期间所有配置都在 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,默认情况下,域包中的所有别名都是类名,首字母较小。

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

【讨论】:

  • 对不起,我看不到你的照片,你能把它上传到别的地方,然后把链接发给我。非常感谢!
  • 我将 xml 映射器文件与映射器类存储在同一个包中。我认为它应该自动加载。
  • 我没有任何分享链接...我相信你可以使用不同的浏览器来显示图像。如果您需要与该接口位于同一位置的 xml 文件,在这种情况下,请检查一次,如果 xml 在编译时被复制。在 maven 中有配置选项,它允许您仅将 .class 复制到 classes 目录。因此,在您的情况下,xml 可能已被排除。
【解决方案2】:

如果无法读取包,可以尝试直接添加资源:

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

【讨论】:

  • 感谢您的评论,但我尝试使用:"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 1970-01-01
  • 2015-06-19
  • 2017-12-20
  • 2016-05-17
  • 1970-01-01
  • 2013-10-07
相关资源
最近更新 更多