Spring Boot框架——快速入門
Spring Boot是Spring 全家桶非常重要的一個模組,通過 Spring Boot 可以快速搭建一個基於 Spring 的 Java 應用程式,Spring Boot 對常用的第三方庫提供了配置方案,可以很好地和 Spring 進行整合,MyBatis、Spring Data JPA 等,可以一鍵式搭建功能完備的 Java 企業級應用。
Spring Boot 的優勢
- 不需要任何 XML 配置檔案。
- 內嵌 Web 伺服器,可以直接啟動。
- 預設支援 JSON 資料,不需要做額外配置。
- 支援 RESTful 風格
- 使用一個配置檔案(非 XML、propertis、YAML)可以配置所有的個性化資訊
Spring Boot 就是一個可以用很少的配置快速搭建 Spring 應用的框架,並且可以自動整合主流的 Java 技術棧。
Spring Boot有兩種建立方式
- 線上建立工程
- 手動建立工程
這裡演示一下線上建立
1、啟動idea,點選Create New Project
2、選擇Spring Initializr--Default: https://start.spring.io--next
3、輸入Group、Artifact等--點選next
4、選擇web--勾選Spring Web--點選next
5、選擇路徑--點選finish
6、OK,spring boot專案建立成功了,如果是第一次建立spring boot專案的話,需要等待一會,下載pom依賴
7、新增pom.xml依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.14</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> <!-- mybaits --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> </dependencies>
8、配置application.properties檔案
注意:Spring Boot支援 .properties / .yml兩種格式的配置檔案,如果兩種都存在時,以第一種優先。這裡我修改為了.yml字尾的(便於書寫,建議使用此格式的)
為了演示例子,這裡只做最簡單配置
server: port: 7777 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver username: root password: root url: jdbc:mysql://127.0.0.1:3306/sunjian2?&useSSL=false&serverTimezone=UTC
9、建立實體類
package com.sunjian.demo.entity; import lombok.Data; /** * @author sunjian * @date 2020/3/24 23:20 */ @Data public class Person { private Integer id; private String name; private String age; private String gender; private String email; private String city; }
10、建立dao層介面
package com.sunjian.demo.dao; import com.sunjian.demo.entity.Person; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; /** * @author sunjian * @date 2020/3/24 23:25 */ @Mapper public interface UserDao { @Select("select * from person where id = #{id}") Person findById(Integer id); }
11、建立service層介面及實現類
package com.sunjian.demo.service; import com.sunjian.demo.entity.Person; /** * @author sunjian * @date 2020/3/24 23:29 */ public interface PersonService { public Person findById(Integer id); }
package com.sunjian.demo.service.impl; import com.sunjian.demo.dao.UserDao; import com.sunjian.demo.entity.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * @author sunjian * @date 2020/3/24 23:30 */ @Service public class PersonServiceImpl { @Autowired private UserDao userDao; public Person findById(Integer id){ return userDao.findById(id); } }
12、建立controller檢視層類
package com.sunjian.demo.controller; import com.sunjian.demo.entity.Person; import com.sunjian.demo.service.PersonService; import com.sunjian.demo.service.impl.PersonServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author sunjian * @date 2020/3/24 23:32 */ @RestController @RequestMapping("/person") public class PersonController { @Autowired private PersonServiceImpl personServiceImpl; @GetMapping("/findById/{id}") public Person findById(@PathVariable("id") Integer id){ Person person = personServiceImpl.findById(id); System.out.println(person); return person; } }
13、啟動專案(Spring Boot內建了Tomcat web伺服器,直接執行DemoApplication啟動類檔案即可啟動專案),訪問
OK.