记录spring boot使用中遇到的问题

问题1,mapper的方法找不到:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.baozun.roms.admin.mapper.ShopAdminMapper.list

这个问题是因为mybatis的数据库配置 ,没有加载到mapper文件,导致的,需要在application.yml中配置对应的数据库配置和数据库指向配置就可以了,很奇怪的是,在application.properties文件中配置无效。

application.yml配置:

spring:
    datasource:
        name: test
        url: "数据库链接"
        username: 数据库名
        password: "密码"
        # 使用druid数据源,也可以使用默认的,如果使用druid引入的包会报错。
#        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 10
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20

mybatis:
  mapperLocations: classpath:sqlmap/*.xml  //指向mapper.xml文件地址
  typeAliasesPackage: com.baozun.roms.admin.entity
关于spring.datasourse.type的设置,不设置就是默认数据源,如果设置druid数据源,引入jar包时会报错

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.1.2</version>
</dependency>
所有我注释了,使用的是默认的数据源。测试可以使用。


问题2:APPLICATION FAILED TO START

问题明细:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'actionGradeAdminController': Unsatisfied dependency expressed through field 'actionGradeAdminService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'actionGradeAdminServiceImpl': Unsatisfied dependency expressed through field 'actionGradeAdminMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.baozun.roms.admin.mapper.ActionGradeAdminMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

***************************
APPLICATION FAILED TO START
***************************

问题原因:mapper.java文件引入的注解错误,导致匹配不到,我这边引入的是@Repository,是错误的。

解决方案:把@Repository改为@Mapper,引入的是

import org.apache.ibatis.annotations.Mapper;



问题3:页面访问跨域问题:

解决方法,引用WebMvcConfigurerAdapter类,重写addCorsMappings方法。实例:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @Author: 
 * @Time: Created In 14:12 2018/1/30
 * @Author:
 */
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")   //设置访问的路径,斜杠加星星,表示无限制
        .allowedMethods("GET", "POST");     //设置访问的方式 
}}

未完待续!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值