package com.fc.SpringBoot.dao;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
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.transaction.annotation.Transactional;
import com.fc.SpringBoot.entity.Teacher;
//继承JpaRepository<Teacher, Integer> 泛型:类,主键类型
public interface TeacherRepository extends JpaRepository<Teacher, Integer>{
//根据年龄查询,查询命名语法要规范才能查出来
List<Teacher> findByAge(Integer age);
//参数索引(利用索引号查询):HQL语句,Teacher不是表名是类名,t
@Query("select t from Teacher t where t.name=?1 and t.age=?2")
List<Teacher> findByAgeAndNameTest(String name,Integer age);
//命名参数(用名称来匹配查询参数):
@Query("select t from Teacher t where t.name=:name")
List<Teacher> findByName(@Param("name") String name);
//更新查询,Modeifying和Query组合起来
@Modifying
@Transactional
@Query("update Teacher t set t.age =?2 where t.name=?1")
int updateTeacherByName(String name,Integer age);
//排序,sort 包 domain
List<Teacher> getByAge(Integer age,Sort sort);
//分页,返回的是page bao domain
Page<Teacher> getByName(String name,Pageable pageable);
}
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
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.transaction.annotation.Transactional;
import com.fc.SpringBoot.entity.Teacher;
//继承JpaRepository<Teacher, Integer> 泛型:类,主键类型
public interface TeacherRepository extends JpaRepository<Teacher, Integer>{
//根据年龄查询,查询命名语法要规范才能查出来
List<Teacher> findByAge(Integer age);
//参数索引(利用索引号查询):HQL语句,Teacher不是表名是类名,t
@Query("select t from Teacher t where t.name=?1 and t.age=?2")
List<Teacher> findByAgeAndNameTest(String name,Integer age);
//命名参数(用名称来匹配查询参数):
@Query("select t from Teacher t where t.name=:name")
List<Teacher> findByName(@Param("name") String name);
//更新查询,Modeifying和Query组合起来
@Modifying
@Transactional
@Query("update Teacher t set t.age =?2 where t.name=?1")
int updateTeacherByName(String name,Integer age);
//排序,sort 包 domain
List<Teacher> getByAge(Integer age,Sort sort);
//分页,返回的是page bao domain
Page<Teacher> getByName(String name,Pageable pageable);
}