【发布时间】:2019-07-07 12:48:20
【问题描述】:
我有一个简单的系统(控制器、服务、存储库),但即使数据存在于数据库中,服务也会返回空值
数据
mysql> select * from customer;
+----+---------------+
| id | username |
+----+---------------+
| 4 | liparistudios |
+----+---------------+
域
@Data
@Entity
@Table(name = "customer")
public class Customer implements Serializable {
private static final long serialVersionUID = 201811031445L;
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE )
private Long id;
private String username;
控制器
Customer c = customerService.searchCustomerByUsername( usernameToFind );
服务
@Service
public class CustomerService {
@Autowired
private CustomerRepo repo;
public Customer searchCustomerByUsername( String username ) {
return repo.findAllByUsername( username );
}
存储库
@Repository
@Transactional
public interface CustomerRepo extends JpaRepository<Customer, Long> {
@Query(
value = "SELECT * FROM Customer c WHERE username = ':username' ORDER BY username ASC LIMIT 1",
nativeQuery = true
)
public Customer findAllByUsername(@Param("username") String username );
【问题讨论】:
-
你能上传你的示例项目吗?
-
@KedarJoshi 你是对的!
-
当然,这与 JPA API 无关,而与“Spring Data JPA”API 无关。完全不同
标签: sql spring-boot spring-data-jpa