Spring Data JPA存储库findBy返回null,即使DB

时间:2018-10-26 11:44:55

标签: spring oracle spring-boot spring-data-jpa

我正在使用spring数据JPA,面临的问题是,即使记录存在于DB中,结果也总是为NULL。

我尝试调试spring代码,并检查语句是否正确创建。语句创建正确,参数也正确填充。

配置:- 春季启动= 2.0.5.RELEASE

Oracle DB = Oracle Database 11g 11.2.0.2.0版-64位

POM

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0</version>
    </dependency>
</dependencies>

实体类:

@Entity()
public class User {

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "user_Sequence")
@SequenceGenerator(name = "user_Sequence", sequenceName = "USER_SEQ")
private Long id;

@Column(unique=true)
private String userName;

private String password;
//getter & setter

存储库类

 @Repository
public interface UserRepository extends 
org.springframework.data.repository.CrudRepository<User, Long>{

User findByUserName(String userName);

User save(User user);
}

服务等级

@Service("loginService")
public class LoginService implements ILoginService {

@Autowired
UserRepository userRepository;

@Override
public boolean isValidUser(String userName, String password) {
    User user = userRepository.findByUserName(userName);
    if(user != null && user.getUserName().equals(userName) && user.equals(password)){
        return true;
    } else {
        return false;
    }
}
}

调试屏幕截图:- Debug Screenshot parameter value is "admin" and SQL formed

SQL格式:-

select user0_."id" as id1_2_, user0_."password" as password2_2_, user0_."user_name" as user_name3_2_ from "user" user0_ where user0_."user_name"=?

如果我在DB中运行相同的查询,则结果为:- Result Screenshot

2 个答案:

答案 0 :(得分:0)

由于您没有将列名用作字段名,因此需要知道要映射到实体中的内容。

将您的字段更改为user_name或更改:

@Column(unique=true)
private String userName;

@Column(name="user_name", unique=true)
private String userName;

因此它将能够知道其引用的列;

https://www.concretepage.com/spring-5/spring-data-crudrepository-example

答案 1 :(得分:0)

在存储库中使用findByNameEqualsAndStatus