Search code examples
javajdbcmybatisibatis

ClassNotFound while using myBatis config


I am trying to connect to DB2 using mybatis. My problem is, mybatis-config is not able to find my class file for the data type I am using.

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();
    }
}

At first I thought there might be something wrong with how the config searches for the class file but after some tinkering I am pretty sure it does find the class file, but thinks its not the correct class file (not sure about it though). Any help?


Solution

  • ok so i changed

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

    to

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

    and that seemed to do the trick. maybe it really did fail in looking for the class file.