开发者社区> 问答> 正文

MyBatis Could not resolve type alias?报错

分成两个部分来说,第一部分我们先看报错信息第二部分看看代码逻辑

第一部分:报错信息:

大概意思是说找不到我这个GoShopping,好,然后我们看看代码

第二部分:代码逻辑:

1. 首先先看看我们的 Entity

package entity;

import java.util.Date;

public class GoShopping {
    private long shoppingId;
    private String shoppingName;
    private int shoppingNumber;
    private Date shoppingStartTime;
    private Date shoppingEndTime;
    private Date shoppingCreateTime;

    public long getShoppingId() {
        return shoppingId;
    }

    public void setShoppingId(long shoppingId) {
        this.shoppingId = shoppingId;
    }

    public String getShoppingName() {
        return shoppingName;
    }

    public void setShoppingName(String shoppingName) {
        this.shoppingName = shoppingName;
    }

    public int getShoppingNumber() {
        return shoppingNumber;
    }

    public void setShoppingNumber(int shoppingNumber) {
        this.shoppingNumber = shoppingNumber;
    }

    public Date getShoppingStartTime() {
        return shoppingStartTime;
    }

    public void setShoppingStartTime(Date shoppingStartTime) {
        this.shoppingStartTime = shoppingStartTime;
    }

    public Date getShoppingEndTime() {
        return shoppingEndTime;
    }

    public void setShoppingEndTime(Date shoppingEndTime) {
        this.shoppingEndTime = shoppingEndTime;
    }

    public Date getShoppingCreateTime() {
        return shoppingCreateTime;
    }

    public void setShoppingCreateTime(Date shoppingCreateTime) {
        this.shoppingCreateTime = shoppingCreateTime;
    }

    @Override
    public String toString() {
        return "GoShopping{" +
                "shoppingId=" + shoppingId +
                ", shoppingName='" + shoppingName + '\'' +
                ", shoppingNumber=" + shoppingNumber +
                ", shoppingStartTime=" + shoppingStartTime +
                ", shoppingEndTime=" + shoppingEndTime +
                ", shoppingCreateTime=" + shoppingCreateTime +
                '}';
    }
}

2. 然后我们写个 DAO 接口

package dao;

import entity.GoShopping;

import java.util.Date;
import java.util.List;

public interface GoShoppingDAO {

    int reduceShoppingNumber(long shoppingId, Date shoppingTime);


    GoShopping queryById(long shoppingId);


    List<GoShopping> queryAll(int offset, int limit);
}

3. 接着我们实现这个DAO 写个 Mapper

<?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="com.shopping.dao.GoShoppingDAO">

    <update id="reduceShoppingNumber">
    update shopping
      set shopping_number = shopping_number - 1
      and shopping_startTime <= #{shoppingTime}
      and shopping_endTime >= #{shoppingTime}
      and shopping_number > 0;
    </update>

    <select id="queryById" resultType="GoShopping" parameterType="long">
        select shopping_id, shopping_name, shopping_number, shopping_startTime, shopping_endTime, shopping_createTime
        from shopping
        where shopping_id = #{shoppingId}
    </select>

    <select id="queryAll" resultType="GoShopping">
        select shopping_id, shopping_name, shopping_number, shopping_startTime, shopping_endTime, shopping_createTime
        form shopping
        order by shopping_createTime desc
        limit #{offset}, #{limit}
    </select>

</mapper>

这里就有个问题了!

在 mapper 中的 namespace 中,并没有 shopping 这个路径存在 !!!WTF,我们原想的路径是 com.shopping.dao.GoShoppingDAO,BUT!!,其实这个 shopping 在这里根本就没有找到!好,我们看些MyBatis的相关配置文件以及整合的相关配置文件

4. MyBatis 全局配置

<?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>
    <!-- 配置全局属性 -->
    <settings>
        <!-- 使用 JDBC 的 getGeneratedKeys 获取数据库自增主键值 -->
        <setting name="useGeneratedKeys" value="true"/>

        <!-- 使用列别名替换别名 默认是 true-->
        <setting name="useColumnLabel" value="true"/>

        <!-- 开启驼峰命名转换 吧 user_Controller 通过驼峰命名规范转换成 userController -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

</configuration>

5. Spring和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>

 

好了,简单的DAO以及Mapper写完了,接下来我们用 Junit 来看下报的错

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

}

好的,接下来我们看看报错信息,和第一部分一样,这里直接把错误信息 Copy 过来

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

 

好了,现在我想的问题是,这个找不到GoShopping是不是因为 Mapper 那边的 namespace 中找不到 shopping 路径的问题 ?

该怎么解决呢 ?

展开
收起
爱吃鱼的程序员 2020-06-07 16:41:23 2659 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
                        <p>typealias和shopping的包不对应啊</p> 
    
     <property name="typeAliasesPackage" value="org.seckill.entity"/>
                            现在我把mapper namespace="dao.GoShoppingDAO" 和 property name="typeAliasesPackage" value="dao.GoShoppingDAO" 都统一了,但是还是这个错
                        
    
                        <p>这么牛逼的包名</p>
    
                    
    
                        <p>mapper里面的resultType不对啊,换成全类名试试</p>
    
                    
    
                        <pre>
    

    resultType="GoShopping"

    这个是不是不能找到?

    2020-06-07 16:41:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载
Apache Wicket User Guide - Ref 立即下载
低代码开发师(初级)实战教程 立即下载