TypeAlias problem of loading Mybatis based on SpringBoot

  • 2021-10-27 07:16:02
  • OfStack

SpringBoot Loads TypeAlias of Mybatis

After springboot is typed into jar, running on linux will report that the entity class corresponding to type and alias cannot be found, which is the problem of springboot scanning packets.

In engineering, the DefaultVFS of Mybatis is used by default for scanning, but in the environment of springboot, there will be problems in the scanning package of DefaultVFS of Mybatis, so only VFS can be modified for clear visibility

Stick the code directly:


@Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        logger.info("load SpringBootVFS");
        //DefaultVFS After getting jar There is a problem on, use the springboot Can only be modified 
        VFS.addImplClass(SpringBootVFS.class);
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources1 = resolver.getResources("classpath*:/mybatis/*.xml");
        Resource[] resources2 = resolver.getResources("classpath*:/mysql/mapper/*.xml");
        Resource[] resources = (Resource[]) ArrayUtils.addAll(resources1,resources2);
        sqlSessionFactoryBean.setMapperLocations(resources);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.xxx.xx.entity");
        return sqlSessionFactoryBean.getObject();
    }

springboot Packaging Startup reported that typeAlias class name of mybatis could not be found

The springBoot project is normal on IDEA, but it runs in error after packaging. The error is roughly that mybatis can't find XX class when Mapper is parsed. xml

Label inside Mapper:

<select parameterType="XXClass" resultMap="XXMap">

This writing method runs no problem in IDEA, and there is a problem in packaging

Correct writing:

<select parameterType="cn.test.xxx.XXClass" resultMap="XXMap">

The parameterType in the code must use the full path, otherwise it will go wrong.


Related articles: