Commit 30782061 authored by Muhammad Suleman's avatar Muhammad Suleman

used-querydsl

parent 738a421e
...@@ -111,6 +111,24 @@ ...@@ -111,6 +111,24 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.4.1</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
...@@ -145,6 +163,22 @@ ...@@ -145,6 +163,22 @@
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
......
package com.school.project.controller;
import com.querydsl.core.types.Predicate;
import com.school.project.model.Student;
import com.school.project.service.student.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/query")
public class QueryDslController {
@Autowired
private StudentService studentService;
@GetMapping
public Iterable<Student> getStudent(@QuerydslPredicate(root = Student.class) Predicate predicate) {
return studentService.getStudentByPredicate(predicate);
}
}
...@@ -2,14 +2,16 @@ package com.school.project.repository; ...@@ -2,14 +2,16 @@ package com.school.project.repository;
import com.school.project.model.Student; import com.school.project.model.Student;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import java.util.List;
public interface StudentRepository extends JpaRepository<Student, Long> { public interface StudentRepository extends JpaRepository<Student, Long>, QuerydslPredicateExecutor<Student> {
Student findByStudentId(Long id); Student findByStudentId(Long id);
Student findByStudentName(String name); Student findByStudentName(String name);
Student findByRollNumber(String rollNumber); Student findByRollNumber(String rollNumber);
} }
...@@ -2,12 +2,16 @@ package com.school.project.repository.userRegistration; ...@@ -2,12 +2,16 @@ package com.school.project.repository.userRegistration;
import com.school.project.model.userRegistration.User; import com.school.project.model.userRegistration.User;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslPredicateBuilder;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional; import java.util.Optional;
@Repository @Repository
public interface UserRepository extends JpaRepository<User, Long> { public interface UserRepository extends JpaRepository<User, Long>, QuerydslPredicateExecutor<User> {
Optional<User> findUserByEmail(String email); Optional<User> findUserByEmail(String email);
User findOneByUserId(Long id); User findOneByUserId(Long id);
} }
...@@ -49,7 +49,7 @@ public class OAuth2ServerConfiguration { ...@@ -49,7 +49,7 @@ public class OAuth2ServerConfiguration {
.and() .and()
.authorizeRequests() .authorizeRequests()
.antMatchers("/register/**").permitAll() .antMatchers("/register/**").permitAll()
.antMatchers("/student/**").hasAuthority("STUDENT") .antMatchers("/student/**") .hasAuthority("STUDENT")
.antMatchers("/department/**", "/course/**","/admin/**").hasAuthority("ADMIN"); .antMatchers("/department/**", "/course/**","/admin/**").hasAuthority("ADMIN");
} }
} }
......
package com.school.project.service.student; package com.school.project.service.student;
import com.querydsl.core.types.Predicate;
import com.school.project.dto.student.StudentCoursesDTO; import com.school.project.dto.student.StudentCoursesDTO;
import com.school.project.dto.student.StudentViewDataDTO; import com.school.project.dto.student.StudentViewDataDTO;
import com.school.project.model.Student;
public interface StudentService { public interface StudentService {
...@@ -10,4 +12,6 @@ public interface StudentService { ...@@ -10,4 +12,6 @@ public interface StudentService {
public Long getUserId(String username); public Long getUserId(String username);
public StudentCoursesDTO viewCourses(Long id); public StudentCoursesDTO viewCourses(Long id);
Iterable<Student> getStudentByPredicate(Predicate predicate);
} }
package com.school.project.service.student; package com.school.project.service.student;
import com.querydsl.core.types.Predicate;
import com.school.project.dto.student.StudentCoursesDTO; import com.school.project.dto.student.StudentCoursesDTO;
import com.school.project.dto.student.StudentViewDataDTO; import com.school.project.dto.student.StudentViewDataDTO;
import com.school.project.mapper.StudentCoursesMapper; import com.school.project.mapper.StudentCoursesMapper;
import com.school.project.mapper.StudentViewDataMapper; import com.school.project.mapper.StudentViewDataMapper;
import com.school.project.model.Student;
import com.school.project.model.userRegistration.User; import com.school.project.model.userRegistration.User;
import com.school.project.repository.StudentRepository; import com.school.project.repository.StudentRepository;
import com.school.project.repository.userRegistration.UserRepository; import com.school.project.repository.userRegistration.UserRepository;
...@@ -47,6 +49,11 @@ public class StudentServiceImpl implements StudentService { ...@@ -47,6 +49,11 @@ public class StudentServiceImpl implements StudentService {
return studentCoursesMapper.modelToDto(studentRepository.findByStudentId(id)); return studentCoursesMapper.modelToDto(studentRepository.findByStudentId(id));
} }
@Override
public Iterable<Student> getStudentByPredicate(Predicate predicate) {
return studentRepository.findAll(predicate);
}
} }
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/school-portal spring.datasource.url=jdbc:postgresql://localhost:5432/school-portal
spring.datasource.username=root spring.datasource.username=postgres
spring.datasource.password=charlie123 spring.datasource.password=root
spring.jpa.database=postgresql
spring.jpa.properties.hibernate.default_schema=public
spring.security.user.name= sulemantalpur6@gmail.com #spring.sql.init.mode=always
spring.jpa.defer-datasource-initialization=true
spring.datasource.data=classpath:data.sql
spring.security.user.name=sulemantalpur6@gmail.com
spring.security.user.password=charlie spring.security.user.password=charlie
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
spring.mvc.view.prefix=/static spring.mvc.view.prefix=/static
server.error.include-message=always server.error.include-message=always
spring.datasource.data=classpath:data.sql
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment