Spring JDBC 为具有值的列返回空值
Spring JDBC returning null values for columns with values
我对 spring-boot-starter-data-jdbc:2.6.2
的体验很奇怪。
两个相似的查询返回不同的结果。当我在存储库中使用 @Query 时,如下所示,我返回了几列,但其余列返回为 null。
@Repository
public interface StaffRepository extends CrudRepository<Staff, Long> {
@Query(value = "select * FROM staff WHERE email = :email and deleted = false")
Staff findStaffByEmail(String email);
}
Returns:
Staff(id=2, firstName=null, middleName=null, lastName=null, email=geek@mail.com, password=a$sa.48mjYIYa/0Z9ck8J.ReYAxu8qzA062zUCUWZfYWs/h7BwTh28G, ntkenNo=null, phoneNumber=null, branch=Nairobi branch, roles=[], loginDisabled=false, emailVerified=false, deleted=false, failedLoginAttempts=0, createdAt=null, updatedAt=null)
但是当我使用 jdbcTemplate.queryForMap
实现它时,如下所示,我得到了正确的结果所有具有它们值的列
Map<String, Object> map = jdbcTemplate.queryForMap("select * FROM staff WHERE email = ? and deleted = false", email);
System.out.println(map);
返回:
{id=2, firstName=John, middleName=Doe, lastName=Doesto, email=geek@mail.com, password=a$sa.48mjYIYa/0Z9ck8J.ReYAxu8qzA062zUCUWZfYWs/h7BwTh28G, ntkenNo=1234, phoneNumber=+14255550100, branch=Nairobi branch, roles=[], deleted=false, createdAt=2021-12-27 19:32:36.0, updatedAt=null, loginDisabled=false, emailVerified=false, failedLoginAttempts=0}
我上面的代码可能有什么问题。
我正在使用 Mysql 数据库。
下面是 Staff entity
其映射不起作用
package com.myapp.models;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
@Data
@Table("staff")
@Builder(toBuilder = true, builderClassName = "Builder")
@NoArgsConstructor
@AllArgsConstructor
public class Staff {
@Id
Integer id;
Long bankId;
String firstName, middleName, lastName, email, password, ntkenNo, phoneNumber, branch, roles;
boolean loginDisabled, emailVerified, deleted;
int failedLoginAttempts;
String createdAt, updatedAt;
}
然而,这是一个具有相同定义和查询但工作正常的实体:
package com.myapp.models;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
@Data
@Table("sys_admins")
@Builder(toBuilder = true, builderClassName = "Builder")
@NoArgsConstructor
@AllArgsConstructor
public class SysAdmins {
@Id
Integer id;
String name, email, password, role;
}
存储库查询:
package com.rmauth.repositories;
import com.rmauth.models.SysAdmins;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface SysAdminsRepository extends CrudRepository<SysAdmins, Long> {
@Query(value = "select * FROM sys_admins WHERE email = :email and deleted = false")
SysAdmins findByEmail(String email);
}
我看不出为什么第一个不起作用。
如 M. Deinum 所述。然而,映射失败是个问题,因为我想继续利用 Lombok 来简化 POJOS 的工作。我决定直接使用 jdbcTemplate
和 BeanPropertyRowMapper
而不是使用 @Query
,这需要我实现 Mapper class 从而增加样板代码。我只需要简单而动态的方法。
Staff staff = jdbcTemplate.queryForObject("select * FROM staff WHERE email = ? and deleted = false", new BeanPropertyRowMapper<>(Staff.class), email);
我对 spring-boot-starter-data-jdbc:2.6.2
的体验很奇怪。
两个相似的查询返回不同的结果。当我在存储库中使用 @Query 时,如下所示,我返回了几列,但其余列返回为 null。
@Repository
public interface StaffRepository extends CrudRepository<Staff, Long> {
@Query(value = "select * FROM staff WHERE email = :email and deleted = false")
Staff findStaffByEmail(String email);
}
Returns:
Staff(id=2, firstName=null, middleName=null, lastName=null, email=geek@mail.com, password=a$sa.48mjYIYa/0Z9ck8J.ReYAxu8qzA062zUCUWZfYWs/h7BwTh28G, ntkenNo=null, phoneNumber=null, branch=Nairobi branch, roles=[], loginDisabled=false, emailVerified=false, deleted=false, failedLoginAttempts=0, createdAt=null, updatedAt=null)
但是当我使用 jdbcTemplate.queryForMap
实现它时,如下所示,我得到了正确的结果所有具有它们值的列
Map<String, Object> map = jdbcTemplate.queryForMap("select * FROM staff WHERE email = ? and deleted = false", email);
System.out.println(map);
返回:
{id=2, firstName=John, middleName=Doe, lastName=Doesto, email=geek@mail.com, password=a$sa.48mjYIYa/0Z9ck8J.ReYAxu8qzA062zUCUWZfYWs/h7BwTh28G, ntkenNo=1234, phoneNumber=+14255550100, branch=Nairobi branch, roles=[], deleted=false, createdAt=2021-12-27 19:32:36.0, updatedAt=null, loginDisabled=false, emailVerified=false, failedLoginAttempts=0}
我上面的代码可能有什么问题。
我正在使用 Mysql 数据库。
下面是 Staff entity
其映射不起作用
package com.myapp.models;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
@Data
@Table("staff")
@Builder(toBuilder = true, builderClassName = "Builder")
@NoArgsConstructor
@AllArgsConstructor
public class Staff {
@Id
Integer id;
Long bankId;
String firstName, middleName, lastName, email, password, ntkenNo, phoneNumber, branch, roles;
boolean loginDisabled, emailVerified, deleted;
int failedLoginAttempts;
String createdAt, updatedAt;
}
然而,这是一个具有相同定义和查询但工作正常的实体:
package com.myapp.models;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
@Data
@Table("sys_admins")
@Builder(toBuilder = true, builderClassName = "Builder")
@NoArgsConstructor
@AllArgsConstructor
public class SysAdmins {
@Id
Integer id;
String name, email, password, role;
}
存储库查询:
package com.rmauth.repositories;
import com.rmauth.models.SysAdmins;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface SysAdminsRepository extends CrudRepository<SysAdmins, Long> {
@Query(value = "select * FROM sys_admins WHERE email = :email and deleted = false")
SysAdmins findByEmail(String email);
}
我看不出为什么第一个不起作用。
如 M. Deinum 所述。然而,映射失败是个问题,因为我想继续利用 Lombok 来简化 POJOS 的工作。我决定直接使用 jdbcTemplate
和 BeanPropertyRowMapper
而不是使用 @Query
,这需要我实现 Mapper class 从而增加样板代码。我只需要简单而动态的方法。
Staff staff = jdbcTemplate.queryForObject("select * FROM staff WHERE email = ? and deleted = false", new BeanPropertyRowMapper<>(Staff.class), email);