PropertyReferenceException: No property getAll found for type Users...

Java Spring Boot 2.0连接 MongoDB 4.0时候出错。
抛出来一堆异常信息,最后找到问题根源,解决办法:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homeController': Unsatisfied dependency expressed through field 'usersRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property getAll found for type Users!

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at  

是Repository定义错误,没有getAll(),实体没有Allsh属性,所以一直报错。
实体类的定义:

@Document(collection = "Users")
public class Users {
 
    private int id;
 
    private String name;
 
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    private String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Repository改成规范的方法名就可以了

public interface UsersRepository extends MongoRepository<Users, Integer> {
    public List<Users> getUserByName(String name);
    public Users getUserByNameAndPassword(String name, String password);
    public Users getUserById(int id);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常出现在使用 MyBatis 进行数据库操作时,表示 MyBatis 找不到 cellphone 属性对应的 TypeHandler。 解决方法是为 cellphone 属性指定一个合适的 TypeHandler。TypeHandler 是 MyBatis 中用于处理 Java 对象与数据库类型之间转换的组件。你需要自定义一个 TypeHandler,实现 Java 对象与数据库类型之间的转换,并在 MyBatis 配置文件中将其注册。 以下是一个简单的 TypeHandler 示例: ```java public class CellphoneTypeHandler extends BaseTypeHandler<String> { @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, parameter); } @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { return rs.getString(columnName); } @Override public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return rs.getString(columnIndex); } @Override public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return cs.getString(columnIndex); } } ``` 这个 TypeHandler 可以处理 String 类型的 cellphone 属性。在 MyBatis 配置文件中注册这个 TypeHandler: ```xml <typeHandlers> <typeHandler handler="com.example.CellphoneTypeHandler" javaType="java.lang.String"/> </typeHandlers> ``` 注意要将 com.example.CellphoneTypeHandler 替换成你自定义的 TypeHandler 类的全限定名。此外,javaType 属性指定了该 TypeHandler 能够处理的 Java 类型,这里是 String。在你的实体类中,为 cellphone 属性加上 @TypeHandler 注解即可: ```java public class User { private Integer id; private String name; @TypeHandler(CellphoneTypeHandler.class) private String cellphone; // ... } ``` 这样 MyBatis 在操作数据库时就会使用你自定义的 TypeHandler 来处理 cellphone 属性了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值