J2EE的核心框架SPRing! 而SpringBoot 和SpringData也是非常強大的,大大簡化了開發,使我們可以把精力專注于業務上!
看看兩者的功能:
SpringBoot

SpringData

環境: linux + myeclipse2015 + jdk1.7 + maven3.3.3 + SpringBoot 1.5.1+ SpringData + MySQL5.5
項目結構圖:

1).pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hsp.spring</groupId> <artifactId>SpringDataDemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringDataDemo Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1-b07</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- SpringBoot 里面包含spring mvc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 引進JPA--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- mysql連接的jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <finalName>SpringDataDemo</finalName> </build></project>2).User.java
package hello;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entity // 實體類,利用對象關系映射生成數據庫表public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; private String name; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; }}3).UserRepository.java
package hello;import org.springframework.data.repository.CrudRepository;import hello.User;// 該接口會自動被實現,springdata已經幫我們實現了基本的增刪改查// CRUD --> Create(增), Read(查), Update(改), Delete(刪)public interface UserRepository extends CrudRepository<User, Long> {}4).MainController.java
package hello;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import hello.User;import hello.UserRepository;@Controller // 表示這是一個Controller@RequestMapping(path="/spring") // 訪問url從application/spring/開始public class MainController { @Autowired//自動從spring容器中加載userRepository private UserRepository userRepository; @GetMapping(path="/add") // “/add”路徑映射到addNewUser方法上 public @ResponseBody String addNewUser (@RequestParam String name , @RequestParam String email) { // @ResponseBody 表示返回的string是一個回應(response),不是一個視圖 // @RequestParam 表示接收的參數可以是get或post User n = new User(); n.setName(name); n.setEmail(email); userRepository.save(n); return "Saved"; } @GetMapping(path="/all") public @ResponseBody Iterable<User> getAllUsers() { // 返回一個json類型的user return userRepository.findAll(); }}5).Application.java
package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { public static void main(String[] args) { /*Spring-boot已經集成了tomcat,main函數被執行時,SpringApplication引導應用啟動spring 進而啟動tomcat啟動應用*/ SpringApplication.run(Application.class, args); }}6).application.properties
spring.jpa.hibernate.ddl-auto=createspring.datasource.url=jdbc:mysql://localhost:3306/springspring.datasource.username=xxxxxxspring.datasource.passWord=xxxxxx7).啟動springboot

8).在瀏覽器輸入:http://localhost:8080/spring/add?name=First&email=someemail@someemailprovider.com
數據庫反向生成數據庫表,也插入了數據:

9).在瀏覽器輸入:http://localhost:8080/spring/all 
10).一個小小的整合就成功了
新聞熱點
疑難解答