IDEA create SpringBoot, and implement, run a simple example

1. Open the IDEA, click + Create New Project. Start by creating a new project.

  

 

2. Locate and click on the Spring Initializr, click on the next menu on the left. Note that this idea is used by default https://start.spring.io online template provided, so it is necessary to ensure smooth network.

Of course, you can also choose to load the following Custom template from the specified link.

       At this time, the children have to ask. If no network is not on the GG, I still use eclipse it. Rest assured, IDEA can not be so dishes. How to build a spring Initializr server locally,

Your own Baidu. He would later write a tutorial, the first in a flag stand here now.

 

3. Fill in turn project information according to the actual situation. Wherein Type drop-down selection attribute can project or pom, Packaging property jar or drop-down selection war (Maven things it does not need to refer to it).

Click Next after completed.

 

4. The most exciting interface coming! ! ! You can choose categories from the left, and then check the dependence need in the middle of the window. On the right you can see the dependencies you have selected.

Top drop-down box to select the version of the Spring Boot, where the latest version 2.2.0 M4. Click Next when finished.

Here I chose the "Spring Web Starter" under the "Web" category, "Spring Data JPA" and "Mysql Driver" in the "Template" "Thymeleaf" and under the category "SQL" category.

      

 

 5. Finally, the last step. Setting the project name Project name and path to save the project Project location. When finished, click Finish.

 

 

 6. Wait IDEA build good project, project structure as shown in FIG. Depending on each person dependent on the choice Step 4, similar directory structure.

 

 7. Look at pom.xml, we need to rely on are lying quietly inside. Is not so easy, my mother no longer have to worry about me looking relied.

 

 

 8. Write a simple page to try the new project is good.

spring boot, start.

 1. .gitignore:分布式版本控制系统git的配置文件,意思为忽略提交
在 .gitingore 文件中,遵循相应的语法,即在每一行指定一个忽略规则。 如:.log、/target/、.idea
2. mvnw:全名是maven wrapper的文件
它的作用是在maven-wrapper.properties文件中记录你要使用的maven版本,当用户执行mvnw clean 命令时,发现当前用户的maven版本和期望的版本不一致,那么就下载期望的版本,然后用期望的版本来执行mvn命令,比如mvn clean命令。
3. mvn文件夹:存放mvnw相关文件
存放着maven-wrapper.properties和相关jar包以及名为MavenWrapperDownloader的java文件
4. mvn.cmd:执行mvnw命令的cmd入口
注:mvnw文件适用于Linux(bash),mv斜体样式*nw.cmd适用于Windows 环境。
5. .iml文件:intellij idea的工程配置文件
里面包含当前project的一些配置信息,如模块开发的相关信息,比如java组件,maven组件,插件组件等,还可能会存储一些模块路径信息,依赖信息以及一些别的信息。
6. .idea文件夹:存放项目的配置信息
包括数据源,类库,项目字符编码,历史记录,版本控制信息等。
7. pom.xml:项目对象模型(核心重要)
pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和licenses,以及其他所有的项目相关因素,是项目级别的配置文件。

      9.访问看看效果如何,http://localhost:8080 

启动项目
Spring Boot通常有一个名为*Application的入口类,入口类里有一个main方法,这个main方法其实就是一个标准的Java应用的入口方法。在main方法中使用SpringApplication.run(PlamanagerApplication.class, args),启动Spring Boot应用项目。

@SpringBootApplication
@SpringBootApplication是Spring Boot的核心注解它是一个组合注解:
@SpringBootApplication注解主要组合了
@Configuration
@EnableAutoConfiguration(让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置)
例如,添加了spring-boot-starter-web依赖,会自动添加Tomcat和SpringMVC的依赖,那么Spring Boot会对Tomcat和SpringMVC进行自动配置。
又如,添加了spring-boot-starter-data-jpa依赖,Spring Boot会自动进行JPA相关的配置。
Spring Boot会自动扫描@SpringBootApplication所在类的同级包以及下级包里的Bean。建议入口类放置的位置在groupId+arctifactID组合的包名下。
@ComponentScan主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中

springboot热部署

spring-boot-devtools是一个为开发者服务的一个模块,其中最重要的功能就是自动应用代码更改到最新的App上面去。
原理是在发现代码有更改之后,重新启动应用,但是速度比手动停止后再启动更快。
其深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader
,这样在有代码更改的时候,原来的restart ClassLoader被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。
即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机)

 

在Java的集成开发环境中,比如Eclipse、IDEA中,有常常有三种与编译相关的选项Compile、Make、Build三个选项。这三个选项最基本的功能都是完成编译过程。但又有很大的区别,区别如下:
1、Compile:只编译选定的目标,不管之前是否已经编译过。

2、Make:编译选定的目标,但是Make只编译上次编译变化过的文件,减少重复劳动,节省时间。(具体怎么检查未变化,这个就不用考虑了,IDE自己内部会搞定这些的)

3、Build:是对整个工程进行彻底的重新编译,而不管是否已经编译过。Build过程往往会生成发布包,这个具体要看对IDE的配置 了,Build在实际中应用很少,因为开发时候基本上不用,发布生产时候一般都用ANT等工具来发布。Build因为要全部编译,还要执行打包等额外工 作,因此时间较长。


开启idea自动make功能

CTRL + SHIFT + A --> 查找 make project automatically --> 选中

 

 CTRL + SHIFT + A --> 查找Registry --> 找到并勾选 compiler.automake.allow.when.app.running

 

 

  • 添加pom依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

  • 开启热部署

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--该配置必须-->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>

Spring Data JPA简介
JPA中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
Spring Data JPA是Spring Data的一个子项目,它通过提供基于JPA的Repository极大地减少了JPA作为数据访问方案的代码量。
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

修改配置文件
springboot创建时默认给了一个application.properties配置文件,这个properties格式的文件,好像要多写好多代码,所以改用yml后缀的文件
application.yml

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/hello?useUnicode=true&characterEncoding=utf-8
username: root
password: ******
jpa:
show-sql: true
database: mysql
hibernate:
ddl-auto: update
properties:
hibernate:
format_sql: true
只需要在pom.xml引入需要的数据库配置,就会自动访问此数据库,如果需要配置其他数据库,可以在application.yml
默认使用org.apache.tomcat.jdbc.pool.DataSource创建连接池

Guess you like

Origin www.cnblogs.com/dingjiaoyang/p/12090909.html