各种问题处理方式
nullpointerexception异常BrandServlet为空
这是我遇到的最后一个问题,放到第一来讲,一切都以为修改完了最后还是要用Test类上加这两个注解
@RunWith(SpringRunner.class) //junit4
@SpringBootTest(classes = SoybeanProductApplication.class)
//最后如果有其它情况的话 没加注入你真的是小天才
@Autowired //<----------一定要加
BrandService brandService;
导包导的是这两个
import org.junit.Test;
import org.junit.runner.RunWith;
java.lang.IllegalStateException: Failed to load ApplicationContext问题
这个问题是由于SpringBoot 和SpringCloud 版本不兼容的问题 需要换成一致的,具体要去spring官方
SpringCloud | SpringBoot |
---|---|
Finchley | 2.0.x |
Finchley.SR1 | Spring Boot >=2.0.3.RELEASE and <=2.0.9RELEASE |
Finchley.SR4 | Spring Boot >=2.0.3.RELEASE and <=2.0.9RELEASE |
Greenwich | 2.1.x |
Hoxton | 2.2.x2.3.x (Starting with SR5) |
2020.0.x aka Ilford | 2.4.x |
我自己成功解决的版本为:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
packet不对的问题 xxxpacket
这个时候要对应一下测试类和启动类 好好检查报名是否一致 不是要拷贝到一起(是包名一致!!!)这样SpringBoot才能扫描到!!!
//这是启动类的包
package com.learn.soybean.product;
//测试类的包
package com.learn.soybean.product;
看到没 是一样的噢
其他问题
其他问题多多少少都是比较粗心导致的了
- 如 : 不小心启动类的注解删了 还有没加特定@MapperScan注解
- 下面是正确案例
@MapperScan("com.learn.soybean.product.dao") //扫描包 dao中有Mapper就不用了
@SpringBootApplication
public class SoybeanProductApplication {
public static void main(String[] args) {
SpringApplication.run(SoybeanProductApplication.class, args);
}
}
- 如2 yml文件中 配置写错 或者application
- 写成什么appilcetion apppissifxx啥乱七八糟的 自己注意一下
spring:
datasource:
username: root
password: root
url: jdbc:mysql://192.168.56.10:3306/gulimall_pms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
-
如3 :没service类
答:那你就赶紧在代码生成器那里给我分页个数打到最大再生成!!! -
如4:应该没了吧 最后是测试类一些小失误的代码吧
-
放源码!
@RunWith(SpringRunner.class) //junit4
@SpringBootTest(classes = SoybeanProductApplication.class)
public class SoybeanProductApplicationTests {
@Autowired
BrandService brandService;
@Test
public void contextLoads() {
BrandEntity brandEntity = new BrandEntity();
brandEntity.setName("小米");
brandEntity.setDescript("太下头了");
brandService.save(brandEntity);
System.out.println("保存成功...");
}
}