Spring Data存储库区分大小写查询

时间:2015-07-03 09:06:19

标签: spring spring-data-jpa

我在存储库中创建了一个简单的查询:

public interface UserRepository extends JpaRepository<User, Integer> {
[...]
   @Query("SELECT user FROM User user "
        + "where user.userName= :userName "
        + "and user.password= :password "
        + "and  user.organization.name= :organizationName")
   User findByNamePasswordOrganization(@Param("userName") String userName,
        @Param("password") String password,
        @Param("organizationName") String organizationName);
[...]

但结果不区分大小写,因此如果userName =&#34; USER&#34;或者用户名=&#34;用户&#34;对于此查询是相同的。

我使用Spring 4.1.6.RELEASE,Spring Data 1.8.0.RELEASE,Hibernate 4.0.1.Final和MySql 5.6

编辑:

我已使用原生查询解决:

@Query(value = "SELECT * FROM USER user, ORGANIZATION organization "
        + "where user.USER_NAME= ?1 "
        + "and BINARY user.PASSWORD= ?2 "
        + "and organization.name= ?3 "
        + "and user.ORGANIZATION_ID=organization.ORGANIZATION_ID", nativeQuery = true)
User findByNamePasswordOrganizationSql(String userName, String password, String organizationName);

1 个答案:

答案 0 :(得分:2)

除非您进行二进制比较http://dev.mysql.com/doc/refman/5.0/en/charset-binary-op.html

,否则MySQL不区分大小写

顺便使用Spring数据你不需要@Query只使用接口方法定义

   User findByUserNameAndPasswordAndOrganizationName(@Param("userName") String userName,
    @Param("password") String password,
    @Param("organizationName") String organizationName)

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ 第4.3.2节。查询创建