Spring Boot 其实就是Spring,学过Spring的同学一定都知道,即使是一个很简单的程序,Spring都需要大量的配置。而Spring Boot将我们从配置文件中解放了出来,Spring Boot提供了大量的默认配置,我们只需要少数的配置甚至不配置,就可以建立起来一个Spring Boot应用,简直就是Java程序员的福音。
SpringBoot官网:Spring Projects
Spring Boot官方推荐使用Maven或Gradle来构建项目本篇将使用maven来构建项目。
这里就以helloWolrd为例子
springboot提供两种创建的项目的方式,本文章通过springboot的官网来创建第一个springboot项目地址如下。
https://start.spring.io/start.spring.io
以maven构建项目 - > Generator Project创建第一个springboot项目
导入代码到eclipse - > maven install构建项目
的的pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>创建一个控制器类位于例子的子包下HelloController中:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class HelloController { @RequestMapping("hello") String hello() { return "Hello World!"; } }运行主方法(该方法自动生成不需要手写)
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }(需要安装STS工具来运行)
Springboot的启动需要eclipse插件sts下载方式帮助 - > marketplace - >热门 选择springbootTools工具 - >安装安装sts插件STS安装步骤
springboot会使用内嵌的tomcat的服务器来启动,启动成功后
浏览器访问:http:// localhost:8080 / hello
即可看到我们的欢迎页面了。
在资源目录下的application.xml中里定义。
在application.xml添加以下配置文件
datasource:url:jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=trueusername: root password: root driverClassName: com.mysql.jdbc.Driver #mybatis mybatis.type-aliases-package=com.example.demo.pojo mybatis.mapper-locations=classpath:mapper/*.xml #mapper #mappers mapper.mappers=com.example.utils.MyMapper mapper.not-empty=false mapper.identity=MYSQL更多的配置信息可以参照官网 SpringBoot官网:Spring Projects。
错误页面的代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1 style="color:red">发生异常</h1> <div th:text="${e}"> </div> </body> </html>springboot 支持 FreeMark、Thymeleaf等模板引擎 来开发前后分离的项目。同时也支持jsp等模板引擎。
整合Thymeleaf
相关的pom文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>在application,xml中配置thymeleaf 的基本格式
spring.thymeleaf.prefix = classpath:/ templates / spring.thymeleaf.suffix = .html spring.thymeleaf.mode = HTML5 spring.thymeleaf.encoding = UTF-8 spring.thymeleaf.servlet.content-type = text / html; charset = utf-8 spring.thymeleaf.cache = false spring.mvc.static-path-pattern = / static / ** spring.mvc.static-path-pattern = / static / **表示映射的thymeleaf的路径在静目录下
thymeleaf的基本语法参考
http://link.zhihu.com/?target=https%3A//blog.csdn.net/RAVEEE/article/details/83378445