一. 什么是 Spring Boot

Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.

—— 摘自官网

翻译:采纳了建立生产就绪 Spring 应用程序的观点。 Spring Boot 习惯优先于配置的惯例,旨在让您尽快启动和运行。

Spring Boot 致力于简洁,让开发者写更少的配置,程序能够更快的运行和启动。它是下一代 JavaWeb 框架,并且它是 Spring Cloud(微服务)的基础。

二、搭建第一个 Spring Boot 程序

可以在 start.spring.io 上建项目,也可以用 idea 构建。本案列采用 idea.

具体步骤:

new prpject -> spring initializr ->{name :firstspringboot , type: mavenproject,packaging:jar ,..}  ->{spring version :1.5.2  web: web } -> ....

应用创建成功后,会生成相应的目录和文件。

其中有一个 Application 类, 它是程序的入口:

@SpringBootApplication
public class FirstspringbootApplication { public static void main(String[] args) {
SpringApplication.run(FirstspringbootApplication.class, args);
}
}

在 resources 文件下下又一个 application.yml 文件,它是程序的配置文件。默认为空,写点配置 , 程序的端口为 8080,context-path 为 /springboot:

server:
port: 8080
context-path: /springboot

写一个 HelloController:

@RestController     //等同于同时加上了@Controller和@ResponseBody
public class HelloController { //访问/hello或者/hi任何一个地址,都会返回一样的结果
@RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
public String say(){
return "hi you!!!";
}
}

运行 Application 的 main(), 程序会启动,由于 Spring Boot 自动内置了 servlet 容器,所以不需要类似传统的方式,先部署到容器再启动容器。只需要运行 main() 即可,这时打开浏览器输入网址:localhost:8080/springboot/hi ,就可以在浏览器上看到: hi you!!!

三. 属性配置

在 appliction.yml 文件添加属性:

server:
port: 8080
context-path: /springboot girl:
name: B
age: 18
content: content:${name},age:${age}

在 java 文件中,获取 name 属性,如下:

@Value("${name}")
private String name;

也可以通过 ConfigurationProperties 注解,将属性注入到 bean 中,通过 Component 注解将 bean 注解到 spring 容器中:

@ConfigurationProperties(prefix="girl")
@Component
public class GirlProperties { private String name;
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}

另外可以通过配置文件制定不同环境的配置文,具体见代码:

spring:
profiles:
active: prod

四. 通过 jpa 方式操作数据库

导入 jar ,在 pom.xml 中添加依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

在 appilication.yml 中添加数据库配置:

spring:
profiles:
active: prod datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/dbgirl?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
username: root
password: root jpa:
hibernate:
ddl-auto: create
show-sql: true

这些都是数据库常见的一些配置没什么可说的,其中 ddl_auto: create 代表在数据库创建表,update 代表更新,首次启动需要 create , 如果你想通过 hibernate 注解的方式创建数据库的表的话,之后需要改为 update.

创建一个实体 girl,这是基于 hibernate 的:

@Entity
public class Girl { @Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age; public Girl() {
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getCupSize() {
return cupSize;
} public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
}

创建 Dao 接口, SpringBoot 将接口类会自动注解到 spring 容器中,不需要我吗做任何配置,只需要继承 JpaRepository 即可:

// 其中第二个参数为Id的类型
public interface GirlRep extends JpaRepository<Girl,Integer>{
}

创建一个 GirlController,写一个获取所有 girl 的 api 和添加 girl 的 api ,自己跑一下就可以了:

@RestController
public class GirlController { @Autowired
private GirlRep girlRep; /**
* 查询所有女生列表
* @return
*/
@RequestMapping(value = "/girls",method = RequestMethod.GET)
public List<Girl> getGirlList(){
return girlRep.findAll();
} /**
* 添加一个女生
* @param cupSize
* @param age
* @return
*/
@RequestMapping(value = "/girls",method = RequestMethod.POST)
public Girl addGirl(@RequestParam("cupSize") String cupSize,
@RequestParam("age") Integer age){
Girl girl = new Girl();
girl.setAge(age);
girl.setCupSize(cupSize);
return girlRep.save(girl);
} }

如果需要事务的话,在 service 层加 @Transaction 注解即可。

2 小时学会 Spring Boot的更多相关文章

  1. 2小时学会Spring Boot(IDE:eclipse)

    一:安装STS插件 官网下载:点此下载STS 注意:STS版本必须与eclipse版本对应 安装教程:http://blog.csdn.net/cryhelyxx/article/details/53 ...

  2. 2小时学会spring boot 以及spring boot进阶之web进阶(已完成)

    1:更换Maven默认中心仓库的方法 <mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirr ...

  3. 一分钟学会Spring Boot多环境配置切换

    一. 问题由来 开发环境.测试环境.生产环境--------我们的软件在不同的环境中,系统参数和配置可能会不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置,那每次我们将软件部署到 ...

  4. Spring Boot 学习方法论-如何正确的入门 Spring Boot

    想要入门 Spring Boot,那么什么样的教程是符合初学者学习的(没有太多的Java基础但有一些程序基础或者软件编程知识). 这恰好能够勾出很多问题,比如是文章图文教程适合还是视频教程适合零基础初 ...

  5. Spring Boot 集成 Swagger2 教程

    上篇讲过 Spring Boot RESTful api ,这篇简单介绍下 SwaggerUI 在 Spring Boot 中的应用. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可 ...

  6. 5分钟学会如何创建spring boot项目

    上一篇博客说了如何创建spring boot项目,但是有些同学会觉得有点麻烦,有没有什么快速学会能快速创建spring boot项目的方法,答案是肯定的.接下来我们就一起来快速创建一个spring b ...

  7. 认识并学会使用spring boot

    1,什么是SpringBoot SpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品,用一些固定的方式来构建生产级别的spri ...

  8. spring boot 整合 ehcache

    1. 该说的话 每个人都应当学会独立地去思考.去寻找答案,而不是一味地伸手向他人索取所谓的标准答案. 首先,别成为"拿来主义"者,其次远离"拿来主义"的人. 2 ...

  9. 基于Centos7.4搭建prometheus+grafana+altertManger监控Spring Boot微服务(docker版)

    目的:给我们项目的微服务应用都加上监控告警.在这之前你需要将 Spring Boot Actuator引入 本章主要介绍 如何集成监控告警系统Prometheus 和图形化界面Grafana 如何自定 ...

随机推荐

  1. Google Tango初学者教程

    Getting Started with the Tango Java API In this tutorial, we'll go through setting up your build env ...

  2. java并发编程实战:第十二章---并发程序的测试

    并发程序中潜在错误的发生并不具有确定性,而是随机的. 安全性测试:通常会采用测试不变性条件的形式,即判断某个类的行为是否与其规范保持一致 活跃性测试:进展测试和无进展测试两方面,这些都是很难量化的(性 ...

  3. Oracle E-Business Suite并发处理机制(Current Processing)

    2012年写过一篇关于Oracle E-Business Suite并发管理器的文章,回头看之前总结的内容还是比较单薄,很多点没说到,最近在看这块的内容,索性再写一篇稍微完整的文章来. Oracle ...

  4. Java Socket实现基于TCP和UDP多线程通信

    一.通过Socket实现TCP编程 1.1 TCP编程 TCP协议是面向连接,可靠的,有序的,以字节流的方式发送数据.基于TCP协议实现网络通信的类有客户端的Socket类和服务器端的ServerSo ...

  5. Solr 使用自定义 Query Parser(短语查询,精准查询)

    原文出处:http://blog.chenlb.com/2010/08/solr-use-custom-query-parser.html 由于 Solr 默认的 Query Parser 生成的 Q ...

  6. FIM控制同步用户

     C:\Program Files\Microsoft Office Servers\15.0\Synchronization Service\UIShell 这个路径下,你如果懂FIM,可以进去看看 ...

  7. Asp.net Core IIS上安装部署及502.5错误解决

    总结: 安装Microsoft Visual C++ 2015 Redistributable(https://www.microsoft.com/en-us/download/details.asp ...

  8. 「HNOI 2016」 序列

    \(Description\) 给你一个序列,每次询问一个区间,求其所有子区间的最小值之和 \(Solution\) 这里要用莫队算法 首先令\(val\)数组为原序列 我们考虑怎么由一个区间\([l ...

  9. c语言博客作业06-文件

    1.本章总结  1.1思维导图  1.2本章学习体会 这周学了结构体和文件,结构体作为一种数据的归类方式,相比数组或变量更具有整体全面性,例如一个数组只可以放一些按照元素顺序存放的单元变量,并且我们用 ...

  10. kolla 安装

    下载 kolla-ansible 和 kolla 源码: git clone http://git.trystack.cn/openstack/kolla-ansible -b stable/quee ...