<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 + '\'' + '}'; } }
repository:java
import com.example.demo.entity.Dept; import org.springframework.data.jpa.repository.JpaRepository; public interface DeptRepository extends JpaRepository<Dept,Integer>{ }
service:mysql
import java.util.List; @Service public class DeptService { @Autowired DeptRepository deptRepository; public List<Dept> getAll(){ return deptRepository.findAll(); } }
controller:web
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); } } }
主要三個接口用法都類似,這裏主要用JpaRepository測試。spring
1.JpaRepository接口中List<T> findAllById(Iterable<ID> var1)(批量查詢)sql
@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); } }