2小时学会springboot
来源:互联网 发布:网络设置(2g) 编辑:程序博客网 时间:2024/06/10 20:07
一.什么是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.
具体步骤:
- 1
- 2
- 1
- 2
应用创建成功后,会生成相应的目录和文件。
其中有一个Application类,它是程序的入口:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
在resources文件下下又一个application.yml文件,它是程序的配置文件。默认为空,写点配置 ,程序的端口为8080,context-path为 /springboot:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
写一个HelloController:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
运行 Application的main(),呈现会启动,由于springboot自动内置了servlet容器,所以不需要类似传统的方式,先部署到容器再启动容器。只需要运行main()即可,这时打开浏览器输入网址:localhost:8080/springboot/hi ,就可以在浏览器上看到: hi you!!!
三.属性配置
在appliction.yml文件添加属性:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
在Java文件中,获取name属性,如下:
- 1
- 2
- 1
- 2
也可以通过ConfigurationProperties注解,将属性注入到bean中,通过Component注解将bean注解到spring容器中:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
另外可以通过配置文件制定不同环境的配置文,具体见源码:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
四.通过jpa方式操作数据库
导入jar ,在pom.xml中添加依赖:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
在appilication.yml中添加数据库配置:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
这些都是数据库常见的一些配置没什么可说的,其中ddl_auto: create 代表在数据库创建表,update 代表更新,首次启动需要create ,如果你想通过hibernate 注解的方式创建数据库的表的话,之后需要改为 update.
创建一个实体girl,这是基于hibernate的:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
创建Dao接口, springboot 将接口类会自动注解到spring容器中,不需要我吗做任何配置,只需要继承JpaRepository 即可:
- 1
- 2
- 3
- 1
- 2
- 3
创建一个GirlController,写一个获取所有girl的api和添加girl的api ,自己跑一下就可以了:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
如果需要事务的话,在service层加@Transaction注解即可。已经凌晨了,我要睡了.
源码;http://download.csdn.net/detail/forezp/9778235
- 2小时学会springboot
- 2小时学会springboot
- SpringBoot非官方教程 | 第二十五篇:2小时学会springboot
- SpringBoot非官方教程 | 第二十五篇:2小时学会springboot
- SpringBoot非官方教程 | 第二十五篇:2小时学会springboot
- SpringBoot非官方教程 | 第二十五篇:2小时学会springboot
- SpringBoot非官方教程 | 第二十五篇:2小时学会springboot
- 【SpringBoot】2小时学会SpringBoot学习笔记(第1章- 第2章)
- 【SpringBoot】2小时学会SpringBoot学习笔记(第3章 项目属性配置)
- 【SpringBoot】2小时学会SpringBoot学习笔记(第4章 Controller的使用)
- 【SpringBoot】2小时学会SpringBoot学习笔记(第5章 数据库操作 上 )
- 【SpringBoot】2小时学会SpringBoot学习笔记(第5章 数据库操作 下 )
- 【SpringBoot】2小时学会SpringBoot学习笔记( 第6章 事务管理 )
- 2小时学会Linux基础
- 2小时学会使用SVN
- 2个小时学会写makefile
- 2小时学会spring Root(慕课网)
- 2小时学会Spring Boot 学习笔记
- dbutils--2.搭建环境
- 5-12 功夫传人
- 字符流练习
- node.js安装
- python 发送post请求
- 2小时学会springboot
- 创建自己的CocoaPods库
- mysql 导入txt文件中文乱码
- iOS小技巧总结,绝对有你想要的(下)
- RPM包的制作
- NodeJs安装与使用入门
- Python安装与使用
- iOS 进阶(一)block 与 property
- 二叉树的遍历