用@Alias注解给类起别名,然后在Mybatis配置中使用,项目启动报错!why?

pojo类:

package cn.kane.springboot04.shopping.pojo;




import org.apache.ibatis.type.Alias;

import java.io.Serializable;

/**
 * 〈一句话功能简述〉<br>
 * 〈ss〉
 * --cn.kane.springboot04.shopping.pojo.ProductPo
 * @author Kane
 * Date:   2019/6/5 11:18
 * @since 1.0.0
 */
@Alias("u")
public class ProductPo implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String productName;
    private int stock;
    private double price;
    private int version;
    private String note;

    public ProductPo(Long id, String productName, int stock, double price, int version, String note) {
        this.id = id;
        this.productName = productName;
        this.stock = stock;
        this.price = price;
        this.version = version;
        this.note = note;
    }

    public ProductPo() {
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    @Override
    public String toString() {
        return "ProductPo{" +
                "id=" + id +
                ", productName='" + productName + '\'' +
                ", stock=" + stock +
                ", price=" + price +
                ", version=" + version +
                ", note='" + note + '\'' +
                '}';
    }
}

mapper接口类:

package cn.kane.springboot04.shopping.dao;


import cn.kane.springboot04.shopping.pojo.ProductPo;
import org.apache.ibatis.annotations.Param;

/**
 * 〈一句话功能简述〉<br>
 * 〈ss〉
 *----cn.kane.springboot04.shopping.dao.ProductMapper
 * @author Kane
 * @create 2019/6/5
 * @since 1.0.0
 */
public interface ProductMapper {
    //
    int insertNewProduct(ProductPo productPo);
    //获取
    ProductPo getProduct(Long id);
    //-
    int decreaseProduct(@Param("id") Long id, @Param("quantity") int quantity);

}

mybatis xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="cn.kane.springboot04.shopping.dao.ProductMapper">
    
    <!--插入新产品-->
    <insert id="insertNewProduct" parameterType="cn.kane.springboot04.shopping.pojo.ProductPo">
        insert into t_product
                (product_name,stock,price,version,note)
        values
               (#{productName},#{stock},#{price},#{version},#{note})
    </insert>
    <!--根据id查询商品-->
    <select id="getProduct" parameterType="long" resultType="u">
        select
            id,product_name as productName,stock,price,version(),note
        from
             t_product
        where
            id = #{id}
    </select>
    
    <!--减库存-->
    <update id="decreaseProduct">
        update
            t_product
        set
            stock = stock - #{quantity}
        where
            id = #{id}
    </update>
</mapper>

返回值类型设为别名,可以用ctrl点击这个"u"进入到"u"代表的pojo类,然后启动spring-boot项目,报错。

2019-06-05 16:05:34 INFO  (StartupInfoLogger.java:50)- Starting ShoppingApplication on DESKTOP-5EMO5NE with PID 9664 (started by dell in D:\学习笔记\work\shopping)
2019-06-05 16:05:34 INFO  (SpringApplication.java:675)- No active profile set, falling back to default profiles: default
2019-06-05 16:05:34 INFO  (PostProcessorRegistrationDelegate.java:330)- Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$7928ca94] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-06-05 16:05:34 INFO  (AbstractAddressingEndpointMapping.java:236)- Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
2019-06-05 16:05:35 INFO  (TomcatWebServer.java:90)- Tomcat initialized with port(s): 8989 (http)
2019-06-05 16:05:35 INFO  (DirectJDKLog.java:173)- Initializing ProtocolHandler ["http-nio-8989"]
2019-06-05 16:05:35 INFO  (DirectJDKLog.java:173)- Starting service [Tomcat]
2019-06-05 16:05:35 INFO  (DirectJDKLog.java:173)- Starting Servlet engine: [Apache Tomcat/9.0.19]
2019-06-05 16:05:35 INFO  (DirectJDKLog.java:173)- Initializing Spring embedded WebApplicationContext
2019-06-05 16:05:35 INFO  (ServletWebServerApplicationContext.java:296)- Root WebApplicationContext: initialization completed in 920 ms
2019-06-05 16:05:35 WARN  (AbstractApplicationContext.java:557)- Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productMapper' defined in file [D:\学习笔记\work\shopping\target\classes\cn\kane\springboot04\shopping\dao\ProductMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\学习笔记\work\shopping\target\classes\mappers\ProductMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\学习笔记\work\shopping\target\classes\mappers\ProductMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'u'.  Cause: java.lang.ClassNotFoundException: Cannot find class: u
2019-06-05 16:05:35 INFO  (DirectJDKLog.java:173)- Stopping service [Tomcat]
2019-06-05 16:05:35 INFO  (ConditionEvaluationReportLoggingListener.java:142)- 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-05 16:05:35 ERROR (SpringApplication.java:858)- Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productMapper' defined in file [D:\学习笔记\work\shopping\target\classes\cn\kane\springboot04\shopping\dao\ProductMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\学习笔记\work\shopping\target\classes\mappers\ProductMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\学习笔记\work\shopping\target\classes\mappers\ProductMapper.xml]'. Cause: **org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'u'.  Cause: java.lang.ClassNotFoundException: Cannot find class: u**
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1515)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:824)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
	at cn.kane.springboot04.shopping.ShoppingApplication.main(ShoppingApplication.java:12)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\学习笔记\work\shopping\target\classes\mappers\ProductMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\学习笔记\work\shopping\target\classes\mappers\ProductMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'u'.  Cause: java.lang.ClassNotFoundException: Cannot find class: u
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:607)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1248)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1168)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1500)
	... 17 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\学习笔记\work\shopping\target\classes\mappers\ProductMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\学习笔记\work\shopping\target\classes\mappers\ProductMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'u'.  Cause: java.lang.ClassNotFoundException: Cannot find class: u
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
	... 30 common frames omitted
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\学习笔记\work\shopping\target\classes\mappers\ProductMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\学习笔记\work\shopping\target\classes\mappers\ProductMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'u'.  Cause: java.lang.ClassNotFoundException: Cannot find class: u
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:534)
	at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:424)
	at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:554)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.sqlSessionFactory(MybatisAutoConfiguration.java:150)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$76556c5.CGLIB$sqlSessionFactory$2(<generated>)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$76556c5$$FastClassBySpringCGLIB$$897e84db.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$76556c5.sqlSessionFactory(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
	... 31 common frames omitted
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\学习笔记\work\shopping\target\classes\mappers\ProductMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'u'.  Cause: java.lang.ClassNotFoundException: Cannot find class: u
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:122)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:94)
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:532)
	... 44 common frames omitted
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'u'.  Cause: java.lang.ClassNotFoundException: Cannot find class: u
	at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:118)
	at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:102)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:137)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:130)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:120)
	... 46 common frames omitted
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'u'.  Cause: java.lang.ClassNotFoundException: Cannot find class: u
	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)
	... 50 common frames omitted
Caused by: java.lang.ClassNotFoundException: Cannot find class: u
	at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200)
	at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
	at org.apache.ibatis.io.Resources.classForName(Resources.java:261)
	at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:116)
	... 52 common frames omitted

Process finished with exit code 1

说是给"productMapper"初始化失败了,后面讲到"u"这个类型找不到,嘛回事吗?
把@Alias注解去了,返回值类型改成完整类名,欧克了,启动成功。。
问题原因是缺少Mybatis别名扫描路径设置了:

mybatis.type-aliases-package=cn.kane.springboot04.shopping.pojo
2019-06-05 16:16:32 INFO  (StartupInfoLogger.java:50)- Starting ShoppingApplication on DESKTOP-5EMO5NE with PID 2960 (started by dell in D:\学习笔记\work\shopping)
2019-06-05 16:16:32 INFO  (SpringApplication.java:675)- No active profile set, falling back to default profiles: default
2019-06-05 16:16:32 INFO  (PostProcessorRegistrationDelegate.java:330)- Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$7667668] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-06-05 16:16:32 INFO  (AbstractAddressingEndpointMapping.java:236)- Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
2019-06-05 16:16:32 INFO  (TomcatWebServer.java:90)- Tomcat initialized with port(s): 8989 (http)
2019-06-05 16:16:33 INFO  (DirectJDKLog.java:173)- Initializing ProtocolHandler ["http-nio-8989"]
2019-06-05 16:16:33 INFO  (DirectJDKLog.java:173)- Starting service [Tomcat]
2019-06-05 16:16:33 INFO  (DirectJDKLog.java:173)- Starting Servlet engine: [Apache Tomcat/9.0.19]
2019-06-05 16:16:33 INFO  (DirectJDKLog.java:173)- Initializing Spring embedded WebApplicationContext
2019-06-05 16:16:33 INFO  (ServletWebServerApplicationContext.java:296)- Root WebApplicationContext: initialization completed in 973 ms
2019-06-05 16:16:33 INFO  (ExecutorConfigurationSupport.java:171)- Initializing ExecutorService 'applicationTaskExecutor'
2019-06-05 16:16:33 INFO  (DirectJDKLog.java:173)- Starting ProtocolHandler ["http-nio-8989"]
2019-06-05 16:16:33 INFO  (TomcatWebServer.java:204)- Tomcat started on port(s): 8989 (http) with context path ''
2019-06-05 16:16:33 INFO  (StartupInfoLogger.java:59)- Started ShoppingApplication in 1.68 seconds (JVM running for 2.406)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值