Spring DATA JPA 數據庫視圖映射

SQL:java

CREATE VIEW my_view AS SELECT my_view_id, my_view_name FROM my_table;

建立java實體類spring

@Entity
@Table(name = "my_view")
public class MyView implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "my_view_id")
	private Long myViewId;

	@NotNull
	@Column(name = "my_view_name")
	private String myViewName;

}

建立Repositorysql

import org.springframework.data.repository.CrudRepository;

public interface MyViewRepository extends CrudRepository<MyView, Long> {
}

此時則能夠測試使用該Repository測試

@Autowired
private MyViewRepository myViewRepository;
// ...
long count = myViewRepository.count();
相關文章
相關標籤/搜索