• [Java Spring data] @Query @Param


    package com.example.university.repo;
    
    import com.example.university.domain.Staff;
    import com.example.university.domain.Student;
    import com.example.university.view.CourseView;
    import com.example.university.domain.Course;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.data.repository.query.Param;
    
    import java.util.List;
    
    /**
     * DataSource Management for the Courses at the University.
     *
     * Created by maryellenbowman
     */
    public interface CourseRepository extends CrudRepository<Course,Integer>{
    
        Course findByName(String name);
    
        List<Course> findByDepartmentChairMemberLastName(String chair);
        // or
        @Query("Select c from Course c where c.department.chair.member.lastName=:chair")
        List<Course> findByChairLastName(@Param("chair")String chairLastName);
        // or
        @Query("Select c from Course c where c.department.chair.member.lastname = ?1")
        List<Course> findByChairLastName(String chairLastName);
    
        @Query("Select c from Course c join c.prerequisites p where p.id = ?1")
        List<Course> findCourseByPrerequisite(int id);
    
        @Query("Select new com.example.university.view.CourseView" +
                "(c.name, c.instructor.member.lastName, c.department.name) from Course c where c.id=?1")
        CourseView getCourseView(int courseId) ;
    
        List<Course> findByCredits(@Param("credits") int credits);
    
        Page<Course> findByCredits(@Param("credits") int credits, Pageable pageable);
    
    //      Common Querying Mistake
    //      Uncomment to Debug.
    //
    //    Course findByDeptName(String deptName);
    //
    //    @Query("Select new com.example.university.view.CourseView" +
    //            "(c.name, c.instructor.member.lastName, c.department.name) from course c where c.name=?1")
    //    Course getCourseViewByName(String name);
    
    }
    

      

  • 相关阅读:
    JDK内置工具使用(jps、jstack、jmap、jstat)
    解决mybatis嵌套查询使用PageHelper分页不准确
    MySQL变量的使用
    java lambda求和最值
    解决ROS中PLUGINLIB_DECLARE_CLASS错误
    由crt和key文件生成keystore文件
    转载从java进程里dump出类的class文件的小工具--dumpclass
    oracle数据库创建实例
    广告 Spring Boot整合Jasypt增强应用安全
    PostgreSQL10基础(4)插件安装(转载)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14153045.html
Copyright © 2020-2023  润新知