mybatis-spring & spring boot - Could not resolve type alias issue

15.3k views Asked by At

I am using spring boot 1.5.4 with mybatis-spring 3.1. I am able to successfully run my application via eclipse. But when I used commandline (java -jar jarname), I get below exception. my standalone application is stopping. I want to move my app to deploy to different machine with the jar. Please help.

Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'MyClass'. Cause: java.lang.ClassNotFoundException: Cannot find class: MyCLass at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120) at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149) at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116) ... 36 more

3

There are 3 answers

1
Daniel Cosio On

I resolved the same issue by moving to autoconfigure(using spring properties) I originally had my db configuration(datasource, session factory) configured in a javaconfig class. I'm removed the config and moved my config to my application properties(yml format) .. Below is what I have

mybatis:
  typeAliasesPackage: com.wiredinformatics.preauth.domain
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/preauth?
    useSSL=false&serverTimezone=UTC
    username: myuser
    password: mypass
  dbcp2:
    driver: com.mysql.cj.jdbc.Driver
    initial-size: 2
    max-total: 10
    max-idle: 10
    min-idle: 5
    max-wait-millis: 30000
    validation-query: SELECT 1 

I haven't had time yet to figure out why having my own java config broke the scanning. It worked ok in eclipse, but failed when running from the command line

1
AudioBubble On

I solved this problem!

https://github.com/mybatis/mybatipse/issues/77
@deoxyseia

  1. remove sessionFactoryBean.setTypeAliasesPackage("com.your.packae.pojo")
  2. change resultType="MyClass" to resultType="com.your.packae.pojo.MyClass"
  3. repackage
0
Paul On

I had a similar problem while working in a Maven project, resulting in the same errors.

My situation is that I have a executable jar with Spring Boot which has a nested jar (which uses regular Spring) as dependency. While running in IntelliJ, there was absolutely no problem, due to the way that IntelliJ finds its classes.

While trying to start the jar locally with java -jar jarname.jar so that I could deploy it on a remote server, MyBatis had trouble scanning the typeAliases and typeHandlers packages.

Since I'm working on a legacy-project which mixes Spring Beans initialized in Java and xml, I had one hell of a time determining the root cause. A lot of answers said to change your resultType to the full classpath like this answer. This works. But in my case that would mean hundreds of changes to our DAO's.

Finally, I got on track following this external link from titanwolf.

Here's what you need to do:

  1. add the following dependency to your POM (the jar which contains your MyBatis setup)
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
  1. set the VFS property on your SqlSessionFactoryBean

xml:

    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="vfs" value="org.mybatis.spring.boot.autoconfigure.SpringBootVFS"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

or if you use regular Java for your bean initalizing:

@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
  SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
  factory.setDataSource(dataSource);
  factory.setVfs(SpringBootVFS.class);
  return factory;
}

And that's it. Works locally and on remote server. Hope I saved anyone else some headaches.

note: mybatis-config.xml contains my configuration including, but not limited to:

<configuration>
    <settings>
        <setting name="lazyLoadingEnabled" value="false"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <typeAliases>
        <package name="com.my.path.model"/> <!--wasn't being picked up before fix-->
    </typeAliases>

    <typeHandlers>
        <package name="com.my.path.mybatis"/> <!--wasn't being picked up before fix-->
    </typeHandlers>

    <mappers>
        <mapper resource="sql/my-dao.xml"/>
    <mappers>
</configuration>

You can trace your own errors better by enabling logging for MyBatis. Just add the following line to your application.properties file:

logging.level.org.mybatis=DEBUG