我仍然是java和spring的初学者,我已经将表存储在名为Offers
的mysql中,我试图逐行获取where the Status == 0
数据,我的表如下所示:
-------------+------------+------------+------------+--------------+--------+--------+--------+--------+--------------+
| Msisdn | Entry_Date | Start_Date | End_Date | Service_Type | Status | Parm_1 | Parm_2 | Parm_3 | Process_Date |
+-------------+------------+------------+------------+--------------+--------+--------+--------+--------+--------------+
| 7777777777 | 2019-01-11 | 2019-02-15 | 2019-03-03 | 1 | 1 | 1 | 1 | 1 | 2019-10-15 |
| 7888888899 | 2019-01-11 | 2019-02-12 | 2019-03-03 | 1 | 0 | 1 | 1 | 1 | 2019-10-15 |
| 799999999 | 2019-01-11 | 2019-02-10 | 2019-03-03 | 1 | 0 | 1 | 1 | 1 | 2019-10-15 |
| 79111111111 | 2019-01-28 | 2019-02-27 | 2019-03-03 | 1 | 0 | 1 | 1 | 1 | 2019-10-15 |
+-------------+------------+------------+------------+--------------+--------+--------+--------+--------
当我尝试运行我的代码时
org.springframework.beans.factory.BeanCreationException:错误
在类路径中创建名称为“ entityManagerFactory”的bean
资源
[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]:
调用init方法失败;嵌套异常为
org.hibernate.AnnotationException:未为实体指定标识符:
com.example.accessingdatajpa.Offers
优惠
package com.example.accessingdatajpa;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Entity
public class Offers {
@GeneratedValue(strategy=GenerationType.AUTO)
private String Msisdn;
private String Entry_Date;
private String Start_Date;
private String End_Date;
private String Service_Type;
private String Status;
private String Parm_1;
private String Parm_2;
private String Parm_3;
private String Process_Date;
protected Offers() {}
public Offers(String Msisdn, String Entry_Date, String Start_Date, String End_Date, String Service_Type, String Status, String Parm_1 ,String Parm_2, String Parm_3, String Process_Date) {
this.Msisdn = Msisdn;
this.Entry_Date = Entry_Date;
this.Start_Date = Start_Date;
this.End_Date = End_Date;
this.Service_Type = Service_Type;
this.Status = Status;
this.Parm_1 = Parm_1;
this.Parm_2 = Parm_2;
this.Parm_3 = Parm_3;
this.Process_Date = Process_Date;
}
@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);
}
public String getMsisdn() {
return Msisdn;
}
public String getProcess_Date() {
return Process_Date;
}
public String getEntry_Date() {
return Entry_Date;
}
public String getStart_Date() {
return Start_Date;
}
public String getEnd_Date() {
return End_Date;
}
public String getService_Type() {
return Service_Type;
}
public String getStatus() {
return Status;
}
public String getParm_1() {
return Parm_1;
}
public String getParm_2() {
return Parm_2;
}
public String getParm_3() {
return Parm_3;
}
}
优惠信息库
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);
}
访问DataJpaApplication
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 by status =0
log.info("Offers found with findByStatus('0'):");
log.info("--------------------------------------------");
repository.findByStatus("0").forEach(on -> {
log.info(on.toString());
});
log.info("");
};
}
}
测试文件
package 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());
}
}
最佳答案
我在那看到很多错误:
第一
将@Id
批注添加到msisdn
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private String Msisdn;
第二名
将
@Repository
批注添加到OffersRepository
@Repository
public interface OffersRepository extends CrudRepository<Offers, String> {
List<Offers> findByStatus(String Status);
Offers findByMsisdn(String Msisdn);
}
第三名
将类型为
OffersRepository
的自动装配的bean添加到您的AccessingDataJpaApplication
类中,并从方法OffersRepository repository
中删除参数public CommandLineRunner demo(OffersRepository repository)
。@SpringBootApplication
public class AccessingDataJpaApplication {
private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);
@Autowired
private OffersRepository repository;
public static void main(String[] args) {
SpringApplication.run(AccessingDataJpaApplication.class);
}
@Bean
public CommandLineRunner demo() {
return (args) -> {
// fetch by status =0
log.info("Offers found with findByStatus('0'):");
log.info("--------------------------------------------");
repository.findByStatus("0").forEach(on -> {
log.info(on.toString());
});
log.info("");
};
}
}
第四名
如果要使用
CommandLineRunner
,则需要实现它。您可以通过非常简单的方式来实现,只需在bootstrap类中实现即可。访问DataJpaApplication.java
@SpringBootApplication
public class AccessingDataJpaApplication implements CommandLineRunner {
@Autowired
private OffersRepository repository;
private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);
public static void main(String[] args) {
SpringApplication.run(AccessingDataJpaApplication.class);
}
@Override
public void run(String...args) {
log.info("Offers found with findByStatus('0'):");
log.info("--------------------------------------------");
repository.findByStatus("0").forEach(on - >{
log.info(on.toString());
});
log.info("");
}
}