记录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"); //设置访问的方式
}}
