使用myBatis配置时ClassNotFound

时间:2016-12-06 08:48:19

标签: java jdbc mybatis ibatis

我正在尝试使用mybatis连接到DB2。我的问题是,mybatis-config无法找到我正在使用的数据类型的类文件。

ClientMapper.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE mapper PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN'
  'http://mybatis.org/dtd/mybatis-3-mapper.dtd'>

<mapper namespace='Client'> 
    <select id='getNumber' parameterType='String'
        resultType='org.TE1.Client'>
        SELECT
        number AS phone
        FROM client
        WHERE name = #{name}
    </select>
</mapper>

ClientMapperInterface.java

package org.TE1.Mapper;

public interface ClientMapperInterface {
    public String getNumber(String name);
}

Client.java

package org.TE1.Client;

public class Client {
    private int id;
    private String name;
    private String phone;

    //some getters and setters
}

的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>
    <properties resource='jdbc.properties' />
    <typeAliases>
        <typeAlias type='org.TE1.Client' alias='Client'></typeAlias>
    </typeAliases>
    <environments default='development'>
        <environment id='development'>
            <transactionManager type='JDBC' />
            <dataSource type='POOLED'>
                <property name='driver' value='com.ibm.db2.jcc.DB2Driver' />
                <property name='url' value='jdbc:as400://localhost:50000/dir' />
                <property name='username' value='root' />
                <property name='password' value='admin' />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource='org/TE1/Mapper/ClientMapper.xml' />
    </mappers>
</configuration>

tester java file。

public class testGetPhone {
    public static void main(String[] args) {
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader("mybatis-config.xml");
        } catch (IOException e) {
            System.out.println("MyBatis config not found!");
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = sqlSessionFactory.openSession();

        int number = session.selectOne("Client.getNumber", "TEST");
        System.out.println("number:" + number);
        session.commit();
        session.close();
    }
}

起初我以为配置如何搜索类文件可能有问题,但经过一些修补后我很确定它确实找到了类文件,但认为它不是正确的类文件(不确定它虽然)。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

好的,所以我改变了

<typeAlias type='org.TE1.Client' alias='Client'></typeAlias>

<typeAlias type='org.TE1.Client.Client' alias='Client'></typeAlias>

这似乎可以解决问题。也许它确实在查找类文件时失败了。