1、整合Mybatis,启动提示“No MyBatis mapper was found in '[com.example.demo]' package. Please check your configuration.”
参考资料:https://blog.csdn.net/qinxian20120/article/details/80255976
我是在应用入口类:xxxApplication.java中加入@MapperScan("com.example.demo.dao")注解;
2、项目名设置
使用yml配置文件的时候,在其中添加
server:
port: 8080
servlet:
context-path: /fwmail #项目名
properties文件配置
server.port=8080
server.servlet.context-path=/xiangmuming
3、连接数据库后报错
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
提示系统时区出现错误,可以在mysql中执行命令: set global time_zone='+8:00'
或者在数据库驱动的url后加上serverTimezone=UTC参数
写代码的时候要注意,如果该参数是‘?’后的第一个,即
<property name="jdbcUrl"> jdbc:mysql://localhost:3306/exam?serverTimezone=UTC </property>
是没有问题的,但如果不是第一个,即
<property name="jdbcUrl"> jdbc:mysql://localhost:3306/exam?characterEncoding=utf8&serverTimezone=UTC </property>
这种写法是会报错的,会提示The reference to entity “serverTimezone” must end with the ‘;’ delimiter.
运行后控制台也会出现 对实体 “serverTimezone” 的引用必须以 ‘;’ 分隔符结尾。 的错误提示。
将代码改为
<property name="jdbcUrl"> jdbc:mysql://localhost:3306/exam?characterEncoding=utf8&serverTimezone=UTC </property>
即可。在xml的配置文件中 ;要用 & 代替。
4、关于注解@RestController与@Controller
使用@RestController会导致你想跳转网页无法进行
5、Servlet.service() for servlet [dispatcherServlet] in context with path [/fw] threw exception [Request processing failed; nested exception is java.lang.NullPointerException
应该是spring没有依赖注入:
一:可能是spring或springmvc中的配置文件没有扫描相应的控制器
二:没有使用注解依赖注入,比如controller层中使用service层 service层中使用 dao层,但是controller中的service没有依赖注入或者service中的dao没有依赖注入就会发生此类问题
6、自定义错误页面
在SpringBoot 1.x 中是使用ConfigurableEmbeddedServletContainer 来操作的
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401");
ErrorPage error405Page = new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED, "/405");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404");
ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500");
container.addErrorPages(error401Page,error405Page, error404Page, error500Page);
}
};
}
而在SpringBoot 2.x 中该接口方法已经不存在了,改用ErrorPageRegistry
@Component
public class ErrorConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/error400Page");
ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error401Page");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error404Page");
ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error500Page");
registry.addErrorPages(error400Page,error401Page,error404Page,error500Page);
}
}
7、SpringBoot整合sharding-jdbc时报错
Description:
The bean 'dataSource', defined in class path resource [io/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
Process finished with exit code 1
解决:https://blog.csdn.net/sunqingzhong44/article/details/84791074 第三种