springboot整合JPA(简单整理,待续---)

整合步骤

引入依赖:

<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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencie>

配置数据库连接

spring.datasource.url=jdbc:mysql://localhost:3306/ssmweb?useSSL=false&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

编写与表对应的实体类

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "dept")
public class Dept implements Serializable{

    @Id
    @GeneratedValue
    @Column(name = "dept_id")
    private Integer deptId;
    @Column(name = "dept_name")
    private String deptName;

   ......

    @Override
    public String toString() {
        return "Dept{" +
                "deptId=" + deptId +
                ", deptName='" + deptName + '\'' +
                '}';
    }
}

编写业务过程(controller,service,repository)

  repository:

import com.example.demo.entity.Dept;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DeptRepository extends JpaRepository<Dept,Integer>{

}

  service:

import java.util.List;

@Service
public class DeptService {
    @Autowired
    DeptRepository deptRepository;

    public List<Dept> getAll(){
        return deptRepository.findAll();
    }
}

  controller:

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    DeptService deptService;

    @GetMapping("/depts")
    public List<Dept> getAll(){
        return deptService.getAll();
    }
    
}

简单测试

import com.example.demo.entity.Dept;
import com.example.demo.repository.DeptRepository;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootjpademoApplicationTests {

    @Autowired
    DeptRepository deptRepository;

    @Test
    public void test01() {
        Sort sort = new Sort(Sort.Direction.DESC,"deptId");
        List<Dept> all = deptRepository.findAll(sort);
        for (Dept dept :all) {
            System.out.println(dept);
        }
    }
}

JPA接口

接口间关系

简单测试

主要三个接口用法都相似,这里主要用JpaRepository测试。

1.JpaRepository接口中List<T> findAllById(Iterable<ID> var1)(批量查询)

 @Test
public void test02() {
    List<Integer> idList = new ArrayList<>();
    idList.add(1);
    idList.add(3);
    List<Dept> all = deptRepository.findAllById(idList);
    for (Dept dept :all) {
        System.out.println(dept);
    }
}

2.PagingAndSortingRepository接口中Page<T> findAll(Pageable var1),JpaRepository继承PagingAndSortingRepository,所以可以使用父类的方法(分页排序查询)

@Test
    public void test03() {
        Sort sort = new Sort(Sort.Direction.DESC,"deptId");
        Pageable pageable = PageRequest.of(0,2,sort);
        Page<Dept> all = deptRepository.findAll(pageable);
        for (Dept dept :all) {
            System.out.println(dept);
        }
    }

猜你喜欢

转载自www.cnblogs.com/liu-sheng/p/10097826.html