一、寫在前面
最近一段時間在學習《Spring in action》這本書,在閑著無聊從後往前翻的時候發現了Spring Boot這一章,在書的最後作者有談到很有可能在下一版中一開始就會介紹Spring Boot,這也就引發了我對於Spring Boot的興趣。
為什麼要使用Spring。因為可以減少Java開發的複雜性。
為什麼要使用Spring Boot。為了簡化Spring應用的搭建和開發過程。 二、具體實踐
開發環境:IntelliJ IDEA 2016.2 (直接使用的較新版本)+MYSQL
項目結構:
項目使用了IDE中的基礎Maven模板,因為這個Demo示範的功能比較簡單,service類的功能沒有用到,就不列出了。
Maven配置(pom.xml):
PS:其中有一些是這個簡易Demo裡沒有用到的,我沒有刪除
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wufengqiyun</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.3.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <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> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>com.mangofactory</groupId> <artifactId>swagger-springmvc</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>compile</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
資料庫:
這裡用到的就是一個很簡單的名為springboot的MYSQL資料庫,資料表為user,裡面有id,name,password三個欄位。
CREATE TABLE `springboot`.`user` ( `id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(45) NULL, `password` VARCHAR(45) NULL, PRIMARY KEY (`id`));
主函數:
主函數非常的簡單,就是提供一個入口,和學習JavaSE時候非常類似,其中比較重要的是三個註解,也就是Spring的自動裝配。
ps:在新版本的Spring Boot中,使用了一個新的註解@SpringBootApplication來代替這三個的搭配
package com.wufengqiyun;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.core.annotation.Order;import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;@Configuration@ComponentScan@EnableAutoConfigurationpublic class Start { public static void main(String[] args) { SpringApplication.run(Start.class,args); }} Controller:
主要功能是根據id返回相關資訊
package com.wufengqiyun.controller;import com.wufengqiyun.domain.User;import com.wufengqiyun.repository.UserRepository;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.PropertySource;import org.springframework.core.env.Environment;import org.springframework.web.bind.annotation.*;@RestControllerpublic class UserController { private static final Logger LOGGER= LoggerFactory.getLogger(UserController.class); private UserRepository userRepository; @Autowired public UserController(UserRepository userRepository){ this.userRepository=userRepository; } @RequestMapping(value = "/user/{id}", method = RequestMethod.GET) @ResponseBody public User findByUserId(@PathVariable long id){ User user=userRepository.findOne(id); return user; }} 倉庫類UserRepository:
簡單的資料庫CRUD操作通過繼承CrudRepository即可完成
package com.wufengqiyun.repository;import com.wufengqiyun.domain.User;import org.springframework.data.repository.CrudRepository;import org.springframework.data.rest.core.annotation.RestResource;import java.util.Collection;@RestResource(exported = false)public interface UserRepository extends CrudRepository<User, Long>{ Collection<User> findAll(); User findByName(String name);} User實體類:
一個簡單的JavaBean,注意和資料庫相對應
package com.wufengqiyun.domain;import javax.persistence.*;@Entity@Table(name = "user")public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String password; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }} 設定檔application.properties:
這裡的資料庫相關資訊,需要按照自己實際情況修改
#資料庫配置資訊spring.datasource.url=jdbc:mysql://localhost/springboot?useUnicode=true&characterEncoding=UTF-8&autoReconnect=truespring.datasource.username=自行添加spring.datasource.password=自行添加
運行結果:
如果運行成功,是可以看到內建的tomcat的運行成功資訊的,無需另行配置,非常方便
在瀏覽器中輸入連結:http://localhost:8080/user/2 結果為: