我仍然是java和spring概念的新手,我以前没有使用spring boot的经验,这里我试图从MySQL逐行获取数据,其中status ==o
的表名为Offers
,但是我一直收到以下错误:
启动ApplicationContext时出错。显示条件报告
在启用“调试”的情况下重新运行您的应用程序。 2019-10-16 16:29:51.946
错误16984 --- [main] o.s.boot.SpringApplication
:应用程序运行失败
org.springframework.beans.factory.UnsatisfiedDependencyException:
创建名称为“ demo”的Bean时出错
com.example.accessingdatajpa.AccessingDataJpaApplication:不满意
通过方法'demo'参数0表示的依赖关系;嵌套的
异常是org.springframework.beans.factory.BeanCreationException:
创建名称为“ offersRepository”的bean时出错:init的调用
方法失败;嵌套的异常是java.lang.IllegalArgumentException:
无法为方法public abstract创建查询
com.example.accessingdatajpa.Offers
com.example.accessingdatajpa.OffersRepository.findByMsisdn(java.lang.String)!
在此上无法找到具有给定名称[msisdn]的属性
ManagedType [com.example.accessingdatajpa.Offers]位于
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769)
〜[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
提供:
@Entity
@Table(name = "Offers")
public class Offers {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "Msisdn")
private String Msisdn;
@Column(name = "Entry_Date")
private String Entry_Date;
@Column(name = "Start_Date")
private String Start_Date;
@Column(name = "End_Date")
private String End_Date;
@Column(name = "Service_Type")
private String Service_Type;
@Column(name = "Status")
private String Status;
@Column(name = "Parm_1")
private String Parm_1;
@Column(name = "Parm_2")
private String Parm_2;
@Column(name = "Parm_3")
private String Parm_3;
@Column(name = "Process_Date")
private String Process_Date;
//Setters and getters
@Override
public String toString() {
return String.format(
"Offers[Msisdn='%s', Entry_Date='%s', Start_Date='%s', End_Date='%s', Service_Type='%s', Status='%s', Parm_1='%s', Parm_2='%s', Parm_3='%s',Process_Date='%s']",
Msisdn, Entry_Date, Start_Date, End_Date, Service_Type, Status, Parm_1,Parm_2,Parm_3,Process_Date);
## then getter ..
}
优惠信息库:
package com.example.accessingdatajpa;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface OffersRepository extends CrudRepository<Offers, String> {
List<Offers> findByStatus(String Status);
Offers findByMsisdn(String Msisdn);
}
AccessingDataJpaApplication:
package com.example.accessingdatajpa;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class AccessingDataJpaApplication {
private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);
public static void main(String[] args) {
SpringApplication.run(AccessingDataJpaApplication.class);
}
@Bean
public CommandLineRunner demo(OffersRepository repository) {
return (args) -> {
// fetch customers where flag ==0
log.info("Offers found with findByStatus('0'):");
log.info("--------------------------------------------");
repository.findByStatus("0").forEach(on -> {
log.info(on.toString());
});
// for (Customer bauer : repository.findByLastName("Bauer")) {
// log.info(bauer.toString());
// }
log.info("");
};
}
}
OfferRepositoryTests:
ackage com.example.accessingdatajpa;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@DataJpaTest
public class OffersRepositoryTests {
@Autowired
private TestEntityManager entityManager;
@Autowired
private OffersRepository offer;
@Test
public void testFindByStatus() {
Offers Offer = new Offers();
entityManager.persist(Offer);
List<Offers> findByStatus = offer.findByStatus(Offer.getStatus());
assertThat(findByStatus).extracting(Offers::getStatus).containsOnly(Offer.getStatus());
}
}
最佳答案
将字段Msisdn
的名称更改为msisdn
。
实际上,我将重命名所有字段以遵循标准的Java命名约定。