JPA Entity
In the folder Model create the class
- Add the annotation @Entity above the class
- import javax.persistence.Entity;
- Add the annotation @Id to create the primary key
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
- Specify a column name for a reserved word
@Column(name="`condition`")
String condition;
- Add the annotation @Entity above the class
- import javax.persistence.Entity;
- Add the annotation @Id to create the primary key
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
- Specify a column name for a reserved word
@Column(name="`condition`")
String condition;
Repository (Queries)
- Create a folder name repository in
- scr/main/java
- <project>
- repository
- Create an interface named <Entity>Repository.java
- Extends from JpaRepository
- For customize queries
- Add the annotation @Query algon your query
- Define the parameters
- The table name should be the class name that is using @Entity
public interface ShipwreckRepository extends JpaRepository<Shipwreck, Long>{
@Query("SELECT p FROM PerformanceLevel p WHERE p.js_id = :js_id")
public List<PerformanceLevel> findJs( @Param("js_id") Integer js_id);
}
Wire Repository with Controller
To use in the controller the DB thorugh the Repository
- Add to Controller the Repository
- Use Repository to do execute the queries
RestController- scr/main/java
- <project>
- repository
- Extends from JpaRepository
- Add the annotation @Query algon your query
- Define the parameters
- The table name should be the class name that is using @Entity
public interface ShipwreckRepository extends JpaRepository<Shipwreck, Long>{
public List<PerformanceLevel> findJs( @Param("js_id") Integer js_id);
}
Wire Repository with Controller
- Add to Controller the Repository
- Use Repository to do execute the queries
@RequestMapping("api/v1")
public class PerformanceLevelController {
@Autowired
private PerformanceLevelRepository performanceLevelRepository;
@RequestMapping(value = "performanceLevel", method = RequestMethod.GET)
public List<PerformanceLevel> list() {
return performanceLevelRepository.findAll();
}
}
Comentarios
Publicar un comentario