1. 程式人生 > >11-SpringBoot之資料庫(二)——JPA

11-SpringBoot之資料庫(二)——JPA

SpringBoot之資料庫(二)——JPA

1. 新增pom依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</
artifactId
>
</dependency>

2. JPA配置

在application.yml檔案中spring節點下配置jpa,如下:

 #JPA配置
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

3. 例項開發

3.1 建立實體類

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Date; /** * Created by HuangJun * 14:01 2018/11/12 */ @Data @NoArgsConstructor @AllArgsConstructor @Entity @Table(name="jpa_user") public class JpaUser { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(nullable = false) private String username;
@Column(nullable = false) private Date birthday; @Column(nullable = false) private String sex; @Column(nullable = false) private String address; public JpaUser(String username, Date birthday, String sex, String address) { this.username = username; this.birthday = birthday; this.sex = sex; this.address = address; } }

3.2 DAO

import com.springboot.web.model.JpaUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by HuangJun
 * 14:06 2018/11/12
 */
@Repository
public interface JpaUserRepository  extends JpaRepository<JpaUser, Integer> {
    List<JpaUser> findByUsername(String username);
}

3.3 Service

service介面:

import com.springboot.web.model.JpaUser;

import java.util.List;

/**
 * Created by HuangJun
 * 14:11 2018/11/12
 */
public interface JpaUserService {
    List<JpaUser> findAll();
    List<JpaUser> findByUsername(String username);
    JpaUser save(JpaUser jpaUser);
    void deleteById(Integer id);
}

service實現:

import com.springboot.web.dao.JpaUserRepository;
import com.springboot.web.model.JpaUser;
import com.springboot.web.service.JpaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by HuangJun
 * 14:11 2018/11/12
 */
@Service
public class JpaUserServiceImpl implements JpaUserService {

    @Autowired
    private JpaUserRepository jpaUserRepository;

    @Override
    public List<JpaUser> findAll() {
        return jpaUserRepository.findAll();
    }

    @Override
    public JpaUser save(JpaUser jpaUser) {
        return jpaUserRepository.save(jpaUser);
    }

    @Override
    public void deleteById(Integer id) {
        jpaUserRepository.deleteById(id);
    }

    @Override
    public List<JpaUser> findByUsername(String username) {
        return jpaUserRepository.findByUsername(username);
    }
}

3.4 Controller

import com.springboot.web.model.JpaUser;
import com.springboot.web.service.JpaUserService;
import com.springboot.web.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by HuangJun
 * 14:23 2018/11/12
 */
@RestController
@RequestMapping("/jpaUser")
public class JpaUserController {
    @Autowired
    private JpaUserService jpaUserService;

    @GetMapping("/list")
    public List<JpaUser> findAll() {
        return jpaUserService.findAll();
    }

    @GetMapping("/findByUsername")
    public List<JpaUser> findByUsername(@RequestParam("username") String username) {
        List<JpaUser> list = jpaUserService.findByUsername(username);
        return list;
    }

    @PostMapping("/add")
    public void add(@RequestParam("username") String username,@RequestParam("birthday") String birthday,
                    @RequestParam("sex") String sex,@RequestParam("address") String address) {

        JpaUser user = new JpaUser(username, DateFormatUtil.String2Date(birthday),sex,address);
        jpaUserService.save(user);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteUserByUserId(@PathVariable("id")  Integer id){
        jpaUserService.deleteById(id);
    }

}

3.5 測試

使用postman進行測試

測試add():
在這裡插入圖片描述

測試list():在這裡插入圖片描述

測試findByUsername():在這裡插入圖片描述

4. 原始碼下載

原始碼下載地址:https://download.csdn.net/download/huangjun0210/10789263