java - 使用spring-boot-data-jpa数据访问执行自定义的sql
巴扎黑
巴扎黑 2017-04-18 10:50:26
0
1
469

求助,想在项目中使用spring-boot-data-jpa做数据访问,构建实体User,然后UserRepository extends JpaRepository这种方式没有问题,现在想着对于数据库中有些表中数据我仅仅需要查询一个字段,不想为此构建一个实体类和定义一个访问接口,
有没有相应的做法可以实现,执行一条sql语句然后返回一个对象(可以是Map,List等等..),类似的做法也行,初试jpa还望各位多多指点.

巴扎黑
巴扎黑

reply all(1)
巴扎黑

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
  User findByEmailAddress(String emailAddress);
}

见http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query

如果不想单独建一个repo,可以使用spring-jdbc
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!