jpa 查询

master
bseayin 2019-08-17 20:34:41 +08:00
parent 7c373c5654
commit 26ec8d8c4b
3 changed files with 51 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -22,5 +23,16 @@ public class StudentController {
return studentService.findBySubjectOrderByScoreDesc(sub);
}
//测试地址http://localhost:9081/b/student/pagesub/数学/1/3
@RequestMapping("pagesub/{name}/{start}/{limit}")
public Page<Student> showPageBySub(@PathVariable("name") String sub,@PathVariable("start") String start,@PathVariable("limit") String limit){
return studentService.findBySubjectOrderByScoreDesc(sub, start, limit);
}
@RequestMapping("showone/{name}/{tid}")
public Student showPageBySub(@PathVariable("name") String name,@PathVariable("tid") String tid){
return studentService.getStudent(Integer.parseInt(tid), name);
}
}

View File

@ -2,7 +2,10 @@ package com.zz.repository;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import com.zz.entity.Student;
@ -13,5 +16,26 @@ public interface StudentRepository extends JpaRepository<Student,Integer>{
public List<Student> findBySubjectOrderByScoreDesc(String sub);
public Page<Student> findBySubjectOrderByScoreDesc(String sub,Pageable pageable);
//默认使用JPQL操作的是对象所以里面的列名字必须和entity的属性名字一样
@Query("select s from Student s where s.teamId=?1 and s.name=?2")
public Student getStudentsssdfds(int tid,String name);
//ativeQuery=true 表示使用SQL
//操作的是数据库表,所以里面的列名字必须和数据库里面列的名字一样
@Query(value="select * from student where team_id=?1 and name=?2",nativeQuery=true)
public Student getStudentsdfsdfnative(int tid,String name);
@Query(value="select subject,count(*) from student group by subject",nativeQuery=true)
public List<Object[]> getSubGroup();
@Query(value="select u.name,uh.homework_id from user u left join user_homework uh on (u.id=uh.user_id and uh.homework_id=?1)",nativeQuery=true)
public List<Object[]> selectHomeWork(String hid);
}

View File

@ -2,9 +2,14 @@ package com.zz.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.zz.entity.Student;
import com.zz.entity.User;
import com.zz.repository.StudentRepository;
@Service
public class StudentService {
@ -15,4 +20,13 @@ public class StudentService {
return studentRepository.findBySubjectOrderByScoreDesc(sub);
};
public Page<Student> findBySubjectOrderByScoreDesc(String sub,String startpage,String limit){
Pageable pageable = PageRequest.of(Integer.parseInt(startpage), Integer.parseInt(limit));
return studentRepository.findBySubjectOrderByScoreDesc(sub,pageable);
};
public Student getStudent(int tid,String name){
return studentRepository.getStudentsssdfds(tid, name);
};
}