转载请标明出处:

http://blog.csdn.net/forezp/article/details/61472783

本文出自方志朋的博客

一.什么是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(微服务)的基础。

二、搭建第一个sping 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(),呈现会启动,由于springboot自动内置了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: 123 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注解即可。已经凌晨了,我要睡了.

源码;http://download.csdn.net/detail/forezp/9778235

关注我的专栏《史上最简单的 SpringCloud 教程 》http://blog.csdn.net/column/details/15197.html




扫码关注公众号有惊喜

(转载本站文章请注明作者和出处 方志朋的博客

SpringBoot非官方教程 | 第二十五篇:2小时学会springboot的更多相关文章

  1. (转) SpringBoot非官方教程 | 第二十四篇: springboot整合docker

    这篇文篇介绍,怎么为 springboot程序构建一个Docker镜像.docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的 ...

  2. SpringBoot非官方教程 | 第二十六篇: sprinboot整合elk,搭建实时日志平台

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/sprinboot25-elk/ 本文出自方志朋的博客 这篇文章主要介绍 ...

  3. SpringBoot非官方教程 | 第二十四篇: springboot整合docker

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot24-docker/ 本文出自方志朋的博客 这篇文 ...

  4. SpringBoot非官方教程 | 第二十二篇: 创建含有多module的springboot工程

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springbot22-modules/ 本文出自方志朋的博客 这篇文 ...

  5. SpringBoot非官方教程 | 第十五篇:Springboot整合RabbitMQ

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot15-rabbitmq/ 本文出自方志朋的博客 这 ...

  6. SpringBoot非官方教程 | 第二十篇: 处理表单提交

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-form/ 本文出自方志朋的博客 这篇文件主要介 ...

  7. (转)SpringBoot非官方教程 | 第十二篇:springboot集成apidoc

    首先声明下,apidoc是基于注释来生成文档的,它不基于任何框架,而且支持大多数编程语言,为了springboot系列的完整性,所以标了个题. 一.apidoc简介 apidoc通过在你代码的注释来生 ...

  8. SpringBoot非官方教程 | 第十九篇: 验证表单信息

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot19/ 本文出自方志朋的博客 这篇文篇主要简述如何 ...

  9. SpringBoot非官方教程 | 第十八篇: 定时任务(Scheduling Tasks)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot18-scheduling/ 本文出自方志朋的博客 ...

随机推荐

  1. SimpleAdapter与listview,gridview的组合用法

    首先要明白SimpleAdapter构造方法的几个参数的含义: public SimpleAdapter(Context context, List<? extends Map<Strin ...

  2. jenkins对测试脚本的构建步骤

    使用Jenkins定时执行脚本 Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,所以可用于定时执行python脚本. 环境准备:jdk1.7及以上+Jenkins[+tom ...

  3. Erlang C 與M/M/N排隊模型

    一何谓排队模型 在现实生活中排队的现象可说是无处不在,如:买票.超商.百货公司…等.顾客总是在揣测"排在哪一个服务台会比较快?"或"到底还要排多久呢?"类似这样 ...

  4. OnDeserializedAttribute 不能作用于 Xml Serialization 上

    在做测试的时候习惯用xml serialization观察结果.想当然的认为OnDeserialized Attribute 可以同样的使用,但是其实Xml Serialization 并没有实现相对 ...

  5. Actor的一生

    Actor应该怎么去形容它呢?它是一段代码扮演的角色.它拥有自己的状态机,能根据外界的消息进行适当的反应.他有记忆能力,可以记住来自外界的多个消息并依次进行反应.Actor就像一个小的生命体,有自己的 ...

  6. 命令“mkdir "xxx" xcopy "xxx" "xxx" /S /E /C /Y”已退出,代码为 9009。

    前几天公司来了个新同事,使用的VS2013,但我们的所有项目都是使用VS2012创建的,我想用13打开应该没有什么问题.昨天新同事修改完代码提交后,我获取完成后无法编译成功,提示: 错误 3 命令“m ...

  7. Bootstrap学习笔记(四)

    四.JS插件 概述:与jQueryUI库类似,Bootstrap提供了十几个插件函数.有两种调用方法: (1) 传统JS变成方式 $(...).插件函数.(); (2) 使用data-*扩展属性(推荐 ...

  8. SublimeText插件autoprefixer : css 添加前续

    /* 使用前 */ body { background: linear-gradient(to bottom, #DADADA, #000); } p a { -webkit-border-radiu ...

  9. 使用ionic cordova build android --release --prod命令打包报错解决方法

    使用ionic cordova build android --release --prod命令打包报有如下错误及解决方法 只要把以下内容添加到build-extras.gradle或(build** ...

  10. MyEclipse内存溢出问题

    今天碰到的问题,先记录下来 Console报错: Java.lang.OutOfMemoryError: PermGen space 跟着步骤: 在这里加入:-Xms800m -Xmx800m -XX ...