1. 程式人生 > >Spring Boot 系統之四:Spring Boot 整合JPA

Spring Boot 系統之四:Spring Boot 整合JPA

上一篇我們講了Spring Boot 整合jbdcTemplate 來進行資料的持久化。

這篇我們來說下怎麼通過Spring Boot 整合JPA來實現資料的持久化。

一、程式碼實現

1、修改pom.xml,引入依賴。

        <!-- 引入jpa 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

2、修改application.properties,配置相關資訊。

# 配置JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true

3、建立實體類。

可以講上一篇中的User類直接進行修改;

package com.learn.spring.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_user")
public class User {

    @Id @GeneratedValue(strategy= GenerationType.AUTO)
    private Integer id;
    private String userName;
    private String password;

    public Integer getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

4、建立repository介面並繼承CrudRepository。

package com.learn.spring.repository;

import com.learn.spring.entity.User;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

/**
 * 注意:
 * 1.這裡這裡是interface,不是class
 *
 * 2.CrudRepository裡面的泛型,第一個是實體類,第二個是主鍵的型別
 *
 * 3.由於crudRepository 裡面已經有一些介面了,如deleteAll,findOne等, 我們直接呼叫即可
 *
 * 4.當然,我們也可以根據自己的情況來實現自己的介面,如下面的getUser()方法,jpql語句和hql語句差不多
 *
 * */
public interface UserRepository extends CrudRepository<User, Integer> {

    /**
     * 我們這裡只需要寫介面,不需要寫實現,spring boot會幫忙自動實現
     *
     * */

    @Query("from User where id =:id ")
    public User getUser(@Param("id") Integer id);
}

5、建立Service。

(1)介面

package com.learn.spring.service;

import com.learn.spring.entity.User;


public interface UserService {
    public User getUser(Integer id);
}

(2)實現

package com.learn.spring.service.impl;

import com.learn.spring.entity.User;
import com.learn.spring.repository.UserRepository;
import com.learn.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserRepository repository;
    
    @Override
    public User getUser(Integer id) {
        //有兩種方式:
        //1.呼叫crudRepository的介面
//        return repository.findOne(id);
        //2.呼叫我們自己寫的介面
        return repository.getUser(id);
    }

    
}

6、建立controller。

package com.learn.spring.controller;

import com.learn.spring.entity.User;
import com.learn.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserService service;
    
    @RequestMapping("/getUser/{id}")
    public User getUser(@PathVariable("id") Integer id){
        
        return service.getUser(id);
    }
}

7、執行,頁面以json格式顯示資料庫值。

 二、總結

關於Repository的知識點,可以參考的文章:https://segmentfault.com/a/1190000012346333