MyBatis Could not resolve type alias

divided into two parts, the first part, let"s take a look at the error message , the second part, look at the code logic

.

part I: error message:

clipboard.png

GoShopping

::

  1. Entity

  1. DAO

  1. DAO Mapper

clipboard.png

in namespace in mapper, there is no such path as shopping! WTF, we originally thought the path is com.shopping.dao.GoShoppingDAOMagneBUTQUTION! As a matter of fact, this shopping was not found here at all! OK, let"s take a look at some MyBatis-related configuration files and integrated configuration files

  1. MyBatis global configuration


    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

< configuration >

<!--  -->
<settings>
    <!--  JDBC  getGeneratedKeys  -->
    <setting name="useGeneratedKeys" value="true"/>

    <!--   true-->
    <setting name="useColumnLabel" value="true"/>

    <!--   user_Controller  userController -->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

< / configuration >

  1. Integration of Spring and MyBatis
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--  MyBatis  -->
    <!-- 1 -->
    <context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/>

    <!-- 2 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--  -->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!-- c3p0  -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <property name="autoCommitOnClose" value="false"/>

        <!--  -->
        <property name="checkoutTimeout" value="0"/>

        <!--  -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <!-- 3 SQLSessionFactory  -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--  -->
        <property name="dataSource" ref="dataSource"/>
        <!--  MyBaits  mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--  Entity  org.seckil.entity.Seckill -> Seckill-->
        <property name="typeAliasesPackage" value="org.seckill.entity"/>

        <!--  SQL :Mapper  -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>

    </bean>

    <!-- 4 DAO  DAO  Spring  -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--  SQLSessionFactory  -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--  DAO  -->
        <property name="basePackage" value="org.seckill.dao"/>
    </bean>

</beans>

OK, the simple DAO and Mapper are finished. Next, let"s use Junit to see the next reported error

.
package dao;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Date;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml"})
public class GoShoppingDAOTest {

    @Autowired
    private GoShoppingDAO goShoppingDAO;

    @Test
    public void reduceShoppingNumber() {
        long id = 1;
        Date date = new Date();
        int reduceShopping = goShoppingDAO.reduceShoppingNumber(id, date);
        System.out.println(reduceShopping);
    }

}

OK, let"s take a look at the error message. As in the first part, we Copy the error message directly here

org.springframework.beans.factory.BeanCreationException: Error creating bean with name "sqlSessionFactory" defined in class path resource [spring/spring-dao.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: "file [C:\Users\Sori\IdeaProjects\shopping\target\classes\mapper\GoShoppingMapper.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 "GoShopping".  Cause: java.lang.ClassNotFoundException: Cannot find class: GoShopping



Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: "file [C:\Users\Sori\IdeaProjects\shopping\target\classes\mapper\GoShoppingMapper.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 "GoShopping".  Cause: java.lang.ClassNotFoundException: Cannot find class: GoShopping
    at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:468)
    at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:343)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 39 more

OK, now the question I"m thinking about is, is this because the GoShopping can"t be found because the shopping path can"t be found in the namespace on the other side of the Mapper?
what should I do?


the package name of your dao interface GoShoppingDao is dao, that is, your dao full path is dao.GoShoppingDao, without com.shopping

Menu