Spring + MyBatis异常:Mapped Statements集合不包含值

时间:2014-08-18 07:32:08

标签: java spring mybatis mapper

我执行此应用时出现此异常:

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 at   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)     at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:39)     at $ Proxy6.getAllOrder(Unknown Source)at   MainApp.main(MainApp.java:21)

还有一件事,因为我对此很新,我的代码中是否有任何不良做法?还是我可以提高的一切?请随时发表评论,我真的很感激。

2 个答案:

答案 0 :(得分:2)

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

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,默认情况下,域包中的所有别名都是类名,第一个字母较小。

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

答案 1 :(得分:0)

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

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