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(微服务)的基础。java
能够在start.spring.io上建项目,也能够用idea构建。本案列采用idea.mysql
具体步骤:web
new prpject -> spring initializr ->{name :firstspringboot , type: mavenproject,packaging:jar ,..} ->{spring version :1.5.2 web: web } -> ....
应用建立成功后,会生成相应的目录和文件。spring
其中有一个Application类,它是程序的入口:sql
@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:api
@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文件添加属性:springboot
server: port: 8080 context-path: /springboot girl: name: B age: 18 content: content:${name},age:${age}
在java文件中,获取name属性,以下:app
@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
导入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注解便可。已经凌晨了,我要睡了.