1. 程式人生 > >記錄spring boot使用中遇到的問題

記錄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,引入的是

importorg.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");     //設定訪問的方式
}}

未完待續!