在idea搭建springboot工程记录

在idea搭建springboot工程记录

搭建步骤:
1、打开idea,new–>New Project,选择maven,点击Next
在这里插入图片描述

2、填写GroupId和ArtifactId,点击Next
在这里插入图片描述

3、选择工程存放路径,可改可不改,点击Finish
在这里插入图片描述
4、创建完成一个空的工程
在这里插入图片描述

5、引入springboot相关依赖

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

在这里插入图片描述
注:引入可能因为没有jar包报错,可以引入华为的中央仓库
配置Maven中央仓库路径的方法如下:
在Maven文件的conf目录中打开settings.xml文件 在文件中的servers节点和mirror节点中加入如下内容:

<server>
		<id>huaweicloud</id>
		<username>anonymous</username>
		<password>devcloud</password>
</server>
<mirror>
		<id>huaweicloud</id>
		<mirrorOf>*</mirrorOf>
		<url>https://mirrors.huaweicloud.com/repository/maven/</url>
</mirror>

6、开始创建第一个controller类

@Controller
@EnableAutoConfiguration //这个注解要加,如果没加运行main方法会报错,见下面错误1
public class FirstController {
    @RequestMapping("/")
    @ResponseBody String helloWorld() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(FirstController.class, args);
    }
}

直接点击运行该main函数,再浏览器链接栏,输入地址http://localhost:8080/,就可以看到打印的字符串”Hello World!”了。
另一种方法,可以在test下面编写测试类,进行测试
首先要引入测试包:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

错误集锦:

错误1:
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
在这里插入图片描述
解决办法:加入注解:@EnableAutoConfiguration

错误2:
Error starting ApplicationContext. To display the auto-configuration report re-run your application with ‘debug’ enabled.
2019-01-07 10:07:16.276 ERROR 9168 — [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
在这里插入图片描述
问题原因:我的Controller包和Application包处于平行位置
在这里插入图片描述
解决办法:Controller包必须在Application包里才能被扫描到,从而正常运行程序,所以只要把Controller包移动到Application包内
在这里插入图片描述

错误3:访问的时候报:This application has no explicit mapping for /error, so you are seeing this as a fallback.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Jan 07 10:47:51 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available
在这里插入图片描述
网上查了,主要三个原因,但都不是我这个的原因,我这个是因为,我注解用错了,我用的是@EnableAutoConfiguration,但是应该用的是@SpringBootApplication
在这里插入图片描述

错误4:我本地安装的mysql是8.0以上版本的(后悔没装5.*版本的),但是连接数据库的驱动用的还是5.*版本的,所以连不上。
mysql8.0和之前版本的区别:1、驱动换了,不是com.mysql.jdbc.Driver而是’com.mysql.cj.jdbc.Driver’;2、mysql8.0是不需要建立ssl连接的,你需要显示关闭;3、设置CST。
在这里插入图片描述
在这里插入图片描述
解决办法:
一种:把数据库改成5.*版本的
另一种:把对应的连接信息改成8.0以上版本的
1、使用8.0.11版本的mysql-connector-java,
2、spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

错误5:mysql的time zone 时区错误
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.
在这里插入图片描述
解决办法:使用root用户登录mysql

show variables like '%time_zone%';

在这里插入图片描述
SYSTEM为SQL默认美国时间,而我们中国要比他们迟8小时, 因此,采用+8:00格式。

set global time_zone='+8:00';
注:一般马上看应该都还是system,你可以退出去,重新开发查看

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值