mybatis / spring-boot-starter

MyBatis integration with Spring Boot
Apache License 2.0
4.13k stars 1.79k forks source link

Spring Boot & mybatis-spring -org.apache.ibatis.type.TypeException: Could not resolve type alias #278

Closed gituser786 closed 5 years ago

gituser786 commented 7 years ago

I am using spring boot 1.5.4 & mybatis-spring 1.3.0 along with mybatis-spring-boot-starter. Whenever I am running my standalone application via eclipse I am able to run. when I run the executable jar file in command line, I am getting below error. Error creating bean with name 'classNameMapper' defined in class path resource [mybatis-context.xml]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [mybatis-context.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [com/../..//mapper/MyMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'MyClass'. Cause: java.lang.ClassNotFoundException: Cannot find class: MyCLass

Please help me

kazuki43zoo commented 7 years ago

Hi @gituser786 , Probably, this issue does not belong this project. Could you provide small reproduce project via GitHub repository? (same with https://github.com/mybatis/spring-boot-starter/issues/38#issuecomment-330018346)

RollsBean commented 7 years ago

Hi gituser786, I have a some issue like your's, have u solve this issue? My issue is that when i use idea to run my springboot project, it's running ok but when i run with jar, it fill to run

deoxyseia commented 6 years ago

I had a similar problem. I solved it changing my XML file, specifically field resultType="MyClass" to resultType="com.my.package.entities.MyClass" and removing sessionFactory.setTypeAliasesPackage("com.my.package.entities"); for avoid to duplicate entities in Mybatis context.

kazuki43zoo commented 6 years ago

@RollsBean and @deoxyseia , Could you provide a reproduce project on GitHub? If you run a spring boot application using executable jar, you should be used the SpringBootVFS. For details, please see mybatis/spring-boot-starter#38.

barrer commented 6 years ago

@kazuki43zoo @deoxyseia the answer is correct.

  1. remove sessionFactoryBean.setTypeAliasesPackage("com.your.packae.pojo")
  2. change resultType="MyClass" to resultType="com.your.packae.pojo.MyClass"
  3. repackage
iver3on commented 6 years ago

same problem. I define typeAliase com.. aliase xx in mybatis-config.xml

and in application.properties mybatis.config-locations=classpath:mybatis-config.xml why Could not resolve type alias xx???

kazuki43zoo commented 6 years ago

@iver3on Could you provide a reproduce project via GitHub? And I think that this issue does not belong this project. I will suggest creating a new issue on mybatis-spring-boot-starter if you cannot a resolve this issue.

kazuki43zoo commented 5 years ago

@gituser786 @RollsBean @deoxyseia @iver3on

This issue transferred from mybatis/mybatipse project. Could you feedback for this issue? (Could provide a small reproduce project?) If not feedback, I will close this issue.

kazuki43zoo commented 5 years ago

@gituser786 @RollsBean @deoxyseia @iver3on

I've closed this issue because no feedback. If there is problem, please reopen this.

chris-peng-1244 commented 5 years ago

I just created a mini project that reproduces a similar issue. Here is the git repo. https://github.com/chris-peng-1244/ibatis-resolve-type-alias

The project works fine in IntelliJ, but if I execute java -jar xxxx.jar it complained that BookDO couldn't be resolved.

The reason that this project followed some very unorthorgnal file structures and naming conventions is that:

  1. The real project has 4 data sources that need configuration
  2. It was developed in Spring 4 originally.
  3. I tried to port it to Spring boot 2.1

Hope the demo helps clarify the problems a little better.

kazuki43zoo commented 5 years ago

@chris-peng-1244

Please use the SpringBootVFS as follow:

SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setVfs(SpringBootVFS.class);
// ...
chris-peng-1244 commented 5 years ago

@kazuki43zoo It works, thank you so much!

doannguyenminhchi commented 5 years ago

Thank @kazuki43zoo

adrixgc commented 5 years ago

Thanks @kazuki43zoo, could you please elaborate why this is the solution? Couldn't find any documentation about it

kazuki43zoo commented 5 years ago

@adrixgc Please see http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/#Using_the_SpringBootVFS. Is OK?

ghost commented 4 years ago

@adrixgc Please see http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/#Using_the_SpringBootVFS. Is OK?

nothing changes.. spring boot can not find type alias but complete class name with package..

ghost commented 4 years ago

@adrixgc Please see http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/#Using_the_SpringBootVFS. Is OK?

nothing changes.. spring boot can not find type alias but complete class name with package..

oh! it works. i just put the package location in setTypeAliasesPackage method, not just in setTypeAliasesPackage method.

tinarooot commented 4 years ago

已经指定包名,全名称了 为什么还是会报错

<update id="updateGenTableColumn" parameterType="com.cainaer.gen.domain.GenTableColumn">

Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'GenTableColumn'. Cause: java.lang.ClassNotFoundException: Cannot find class: GenTableColumn

kazuki43zoo commented 4 years ago

@xiao-ma-nong

This issue is closed already. If you have a problem, please create a new issue with reproduce project via GitHub.