初识 Spring Boot

什么是spring boot

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。用我的话来理解,就是spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适)。

使用spring boot有什么好处

其实就是简单、快速、方便!平时如果我们需要搭建一个spring web项目的时候需要怎么做呢?

1)配置web.xml,加载spring和spring mvc

2)配置数据库连接、配置spring事务

3)配置加载配置文件的读取,开启注解

4)配置日志文件

配置完成之后部署tomcat 调试

现在非常流行微服务,如果我这个项目仅仅只是需要发送一个邮件,如果我的项目仅仅是生产一个积分;我都需要这样折腾一遍!

但是如果使用spring boot呢?

很简单,我仅仅只需要非常少的几个配置就可以迅速方便的搭建起来一套web项目或者是构建一个微服务!

 

初识 Spring Boot
一.新建 Spring Boot
1,使用 IntelliJ IDEA 新建项目 -> Spring Initalizr -> Web
2,修改 pom 文件

        <!-- add for Jasonote   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        

3,使用新配置文件yml
* application.yml

            spring:
              profiles:
                active: prod

              datasource:
                driver-clas-name: com.mysql.jdbc.Driver
                url: jdbc:mysql://127.0.0.1:3306/superboot
                username: root
                password:
              jpa:
                hibernate:
                  ddl-auto: update
                show-sql: true
        

* application-dev.yml

            server:
              port: 8081

            girl:
              cupSize: B
              age: 18
        

* application-prod.yml

            server:
              port: 8081

            girl:
              cupSize: C
              age: 24
        

4,新建控制器 GirlController

        package jasonote;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.*;

        import java.util.List;

        /**
         * Created by Jasonote on 2017/9/29.
         */

        @RestController
        public class GirlController {


            @Autowired
            private GirlRepository girlRepository;

            @Autowired
            private GirlService girlService;

            @GetMapping(value = "/girlList")
            public List<Girl> girlList() {
                return girlRepository.findAll();
            }

            @PostMapping(value = "/add")
            public Girl girlAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age) {
                Girl girl = new Girl();
                girl.setCupSize(cupSize);
                girl.setAge(age);
                System.out.println(girl);
                return girlRepository.save(girl);
            }

            @GetMapping(value = "/getId/{id}")
            public Girl getGirl(@PathVariable("id") Integer id) {
                return girlRepository.findOne(id);
            }

            @PostMapping(value = "/insertTwo")
            public void insertTwo(){
                girlService.insertTwo();
            }
        }
    

5,新建实体类 Girl

        package jasonote;
        import javax.persistence.Entity;
        import javax.persistence.GeneratedValue;
        import javax.persistence.Id;

        /**
         * Created by Jasonote on 2017/9/29.
         */

        @Entity
        public class Girl {

            @Id
            @GeneratedValue
            private Integer id;

            private String cupSize;

            private Integer age;

            public Integer getId() {
                return id;
            }

            public void setId(Integer id) {
                this.id = id;
            }

            public String getCupSize() {
                return cupSize;
            }

            public void setCupSize(String cupSize) {
                this.cupSize = cupSize;
            }

            public Integer getAge() {
                return age;
            }

            public void setAge(Integer age) {
                this.age = age;
            }
        }
    

6,使用类配置文件GirlProperties

        package jasonote;
        import org.springframework.boot.context.properties.ConfigurationProperties;
        import org.springframework.stereotype.Component;

        /**
         * Created by Jasonote on 2017/9/29.
         */

        @Component
        @ConfigurationProperties(prefix = "girl")
        public class GirlProperties {
            private String cupSize;
            private Integer age;

            public String getCupSize() {
                return cupSize;
            }

            public void setCupSize(String cupSize) {
                this.cupSize = cupSize;
            }

            public Integer getAge() {
                return age;
            }

            public void setAge(Integer age) {
                this.age = age;
            }
        }
    

7,使用数据仓库GirlRepository

        package jasonote;
        import org.springframework.data.jpa.repository.JpaRepository;

        /**
         * Created by Jasonote on 2017/9/29.
         */
        public interface GirlRepository extends JpaRepository<Girl, Integer>{
        }
    

8,Service GirlService

        package jasonote;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Component;
        import org.springframework.transaction.annotation.Transactional;

        /**
         * Created by Jasonote on 2017/9/29.
         */

        @Component
        public class GirlService {

            @Autowired
            private GirlRepository girlRepository;

            @Transactional
            public void insertTwo(){
                Girl girlA = new Girl();
                girlA.setCupSize("A");
                girlA.setAge(10);
                girlRepository.save(girlA);

                Girl girlB = new Girl();
                girlB.setCupSize("BB");
                girlB.setAge(20);
                girlRepository.save(girlB);
            }
        }
    

Jason.wang

When you find your talent can't afford to be ambitious, be quiet and study !

You may also like...