1. 程式人生 > >SpringBoot 整合 SpringData 入門案例(一)

SpringBoot 整合 SpringData 入門案例(一)

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;

@SpringBootApplication
public 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=create
spring.datasource.url=jdbc:mysql://localhost:3306/spring
spring.datasource.username=xxxxxx
spring.datasource.password=xxxxxx

7).啟動springboot

這裡寫圖片描述

資料庫反向生成資料庫表,也插入了資料:

這裡寫圖片描述

10).一個小小的整合就成功了